Algorithms—41.First Missing Positive

原创 2015年07月08日 09:47:48

思路:要求时间复杂度线性,空间复杂度常量。不能使用计数排序。只能遍历查看然后交换位置。找到每个值正确的位置,然后再遍历查询看到第一个不对应的就是缺省值

public class Solution {
    public int firstMissingPositive(int[] nums) {
    	for (int i = 0; i < nums.length; i++) {
			if (nums[i]!=i+1) {
				if (nums[i]<=nums.length&&nums[i]>0&&nums[nums[i]-1]!=nums[i]) {
					int k=nums[nums[i]-1];
					nums[nums[i]-1]=nums[i];
					nums[i]=k;
					i--;
				}
			}
		}
    	for (int i = 0; i < nums.length; i++) {
			if (nums[i]!=i+1) {
				return i+1;
			}
		}
    	return nums.length+1;
    
        
    }
}


耗时:312ms,中游水平



版权声明:本文为博主原创文章,未经博主允许不得转载。

[LeetCode - 桶排序] 41. First Missing Positive

1 问题 Given an unsorted integer array, find the first missing positive integer. For example, ...

leetcode题解-41. First Missing Positive

题目:Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,...

LeetCode 41. First Missing Positive

1. 题目要求 Given an unsorted integer array, find the first missing positive integer. For example...

41. First Missing Positive

Task: Given an unsorted integer array, find the first missing positive integer. For example, Giv...

41. First Missing Positive\48. Rotate Image

First Missing Positive 题目描述 代码实现 Rotate Image 题目描述 代码实现41. First Missing Positive题目描述Given an unsort...

[leetcode] 41.First Missing Positive

题目: Given an unsorted integer array, find the first missing positive integer.For example, Given [1...

[leetcode-41]First Missing Positive(java)

问题描述: Given an unsorted integer array, find the first missing positive integer.For example, Given ...
  • zdavb
  • zdavb
  • 2015年07月10日 22:18
  • 474

LeetCode41/19 First Missing Positive/Remove Nth Node From End of List ****

leetcode41 First Missing Positive 题目: Given an unsorted integer array, find the first missing posi...

41. First Missing Positive 找到第一个缺失的正整数

给一个未排序的整数序列:找出第一个缺失的正整数。Given [1,2,0] return 3, and [3,4,-1,1] return 2. 分析:我们可以忽略到那些 这道题如果是不重复的序列的话...

DAY28:leetcode #41 First Missing Positive

Given an unsorted integer array, find the first missing positive integer. For example, Given [1,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Algorithms—41.First Missing Positive
举报原因:
原因补充:

(最多只允许输入30个字)