leetcode:41. First Missing Positive (Java)

原创 2016年03月13日 15:28:32

转载请注明出处:z_zhaojun的博客
原文地址
题目地址
First Missing Positive

Given an unsorted integer array, find the first missing positive integer.

For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.

Your algorithm should run in O(n) time and uses constant space.

代码实现(Java):

public class Solution {
    public int firstMissingPositive(int[] nums) {
        int l = nums.length;
        int swap;
        for (int i = 0; i < l;) {
            swap = nums[i++];
            while (swap > 0 && swap <= l && swap != nums[swap - 1]) {
                int tmp = nums[swap - 1];
                nums[swap - 1] = swap;
                swap = tmp;
            }
        }
        for (int i = 0; i < l;) {
            if (nums[i] != ++i) {
                return i;
            }
        }
        return l + 1;
    }
}
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list

LeetCode(41) First Missing Positive

题目如下: A peak element is an element that is greater than its neighbors. Given an input array where nu...

[LeetCode41] First Missing Positive

Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0...
  • sbitswc
  • sbitswc
  • 2014年06月17日 23:51
  • 2434

[Leetcode] 41. First Missing Positive

41. First Missing PositiveGiven an unsorted integer array, find the first missing positive integer.F...

LeetCode --- 41. First Missing Positive

题目链接:First Missing Positive Given an unsorted integer array, find the first missing positive intege...
  • makuiyu
  • makuiyu
  • 2015年02月08日 23:49
  • 955

First Missing Positive -- LeetCode

原题链接: http://oj.leetcode.com/problems/first-missing-positive/  这道题要求用线性时间和常量空间,思想借鉴到了Counting sort中的...

[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
  • 475

[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...

[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)
举报原因:
原因补充:

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