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:

使用类似桶排序的方法:

将值放在它应该在的位置,最后再扫描一次得出哪个位置有缺少值。

引自:

http://m.blog.csdn.net/blog/hellobinfeng/17348055

http://n00tc0d3r.blogspot.com/2013/03/find-first-missing-positive.html

http://www.cnblogs.com/AnnieKim/archive/2013/04/21/3034631.html

A few quick thoughts:

  • Sort all numbers and iterate through to find the first missing integer? No, most sorting algorithms take time at least O(nlogn).
  • How about linear sorting algorithm? No, bucket sort requires O(n) space.
  • Mapping all positive integers to a hash table and iterate from 1 to the length of the array to find out the first missing one? No, hash table requires O(n) space.

Then, how to solve this?

Let‘s take another look at the problem. It is asking for the first missing POSITIVE integer.
So, given a number in the array,

  • if it is non-positive, ignore it;
  • if it is positive, say we have A[i] = x, we know it should be in slot A[x-1]! That is to say, we can swap A[x-1] with A[i] so as to place x into the right place.

We need to keep swapping until all numbers are either non-positive or in the right places. The result array could be something like [1, 2, 3, 0, 5, 6, ...]. Then it‘s easy to tell that the first missing one is 4 by iterate through the array and compare each value with their index.

解1:

 1 public int firstMissingPositive1(int[] A) {
 2         // bug 3: when length is 0, return 1;
 3         if (A == null) {
 4             return 0;
 5         }
 6
 7         for (int i = 0; i < A.length; i++) {
 8             // BUG 1: TLE , should judge when A[i] - 1 == i;
 9             while (A[i] - 1 != i && A[i] > 0) {
10                 // bug 2: cant exchange a same node: A[A[i] - 1] != A[i]
11                 if (A[i] - 1 < A.length && A[A[i] - 1] != A[i]) {
12                     swap(A, i, A[i] - 1);
13                 } else {
14                     // when the number is out of range, delete it.
15                     A[i] = 0;
16                 }
17             }
18         }
19
20         for (int i = 0; i < A.length; i++) {
21             if (A[i] <= 0) {
22                 return i + 1;
23             }
24         }
25
26         return A.length + 1;
27     }
28
29     public void swap(int[] A, int l, int r) {
30         int tmp = A[l];
31         A[l] = A[r];
32         A[r] = tmp;
33     }

简化后,解2:

其实交换的条件就是3个:

1: A[i] is in the range;
2: A[i] > 0.
3: The target is different; (如果不判断这个,会造成死循环,因为你交换过来一个一样的值)

 1 // SOLUTION 2:
 2     public int firstMissingPositive(int[] A) {
 3         // bug 3: when length is 0, return 1;
 4         if (A == null) {
 5             return 0;
 6         }
 7
 8         for (int i = 0; i < A.length; i++) {
 9             // 1: A[i] is in the range;
10             // 2: A[i] > 0.
11             // 3: The target is different;
12             while (A[i] <= A.length && A[i] > 0 && A[A[i] - 1] != A[i]) {
13                 swap(A, i, A[i] - 1);
14             }
15         }
16
17         for (int i = 0; i < A.length; i++) {
18             if (A[i] != i + 1) {
19                 return i + 1;
20             }
21         }
22
23         return A.length + 1;
24     }

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/FirstMissingPositive.java

时间: 2024-10-29 19:11:25

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

【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),空间复杂度为

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