【LeetCode】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.

【解析】

题意:给定一个数组,找出第一个缺失的正数,要求时间复杂度为O(n),空间复杂度为O(1)。

这道题为LeetCode上的一道Hard类型的题,第一次看没想出来怎么做,放了两天,然后某个空当突然就想到了解题思路:

public class Solution {
    public int firstMissingPositive(int[] A) {
        if (A == null || A.length < 1) return 1;

        //求数组中正数的个数cnt
        int cnt = 0;
        for (int i = 0; i < A.length; i++) {
            if (A[i] > 0) {
                cnt++;
            }
        }

        //用一个数组标记从0到cnt的正数是否出现过
        boolean[] flag = new boolean[cnt + 1];
        Arrays.fill(flag, false);
        for (int i = 0; i < A.length; i++) {
            if (A[i] > 0 && A[i] <= cnt) {
                flag[A[i]] = true;
            }
        }

        //从0到cnt第一个没有出现的正数就是要返回的结果
        for (int i = 1; i <= cnt; i++) {
            if (!flag[i]) return i;
        }

        return cnt + 1;
    }
}

===白高兴一场,上面解法好像不是O(1)===

正确解法如下,把正数n放在第n-1个位置上,这样从第一个位置开始,如果位置上的数不等于位置号,那么就是第一个缺失的正数:

public class Solution {
    public int firstMissingPositive(int[] A) {
        if (A == null || A.length < 1) return 1;

        //把小于等于A.length的正数A[i]放到第A[i]-1个位置上
        for (int i = 0; i < A.length; i++) {
            while (A[i] > 0 && A[i] <= A.length && A[A[i] - 1] != A[i]) {
                int tmp = A[A[i] - 1];
                A[A[i] - 1] = A[i];
                A[i] = tmp;
            }
        }

        for (int i = 0; i < A.length; i++) {
            if (A[i] != i + 1) {
                return i + 1;
            }
        }

        return A.length + 1;
    }
}
时间: 2024-08-01 22:49:36

【LeetCode】First Missing Positive 解题报告的相关文章

LeetCode: First Missing Positive 解题报告

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. SOLUTION 1: 使用类似桶排序的方法: 将值放在它

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[leetcode]First Missing Positive

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. 算法分析: 将1 ~ a.length的数字放到与下标相应

leetcode First Missing Positive hashset简单应用

1 public class Solution { 2 public int firstMissingPositive(int[] A) { 3 HashSet<Integer> hash=new HashSet<Integer>(); 4 int count=0; 5 int sum=0; 6 7 for(int i:A) 8 { 9 if(i>0) 10 { 11 hash.add(i); 12 } 13 } 14 15 int beg=1; 16 while(hash.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

[LeetCode] [First Missing Positive 2012-03-08]

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. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Leetcode:First Missing Positive 第一个缺失的正数 桶排序

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. 题解分析: 如果是T(n)的时间 T(n)的空间,则

LeetCode: First Missing Positive [040]

[题目] 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. [题意] 给定一个数组,找出第一个缺失的正数.时间复杂度O(n) [思路] 假设所给数组的

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m