41. 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.
鏈接: ? http://leetcode.com/problems/first-missing-positive/
題解:
找到第一個missing positive,要求O(n)。首先就想到了像sort colors一樣的swap方法。把invalid case都swap到array的后部,兩次pass就可以找到first mission positive。?
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {public int firstMissingPositive(int[] nums) {if(nums == null || nums.length == 0)return 1;int lo = 0, hi = nums.length - 1;while(lo <= hi) {if(nums[lo] <= 0 || nums[lo] > hi) //invalid caseswap(nums, lo, hi--);else if (nums[lo] == lo + 1)lo++;else if(nums[lo] < lo + 1) // duplicates in lower indexswap(nums, lo, hi--);else {if(nums[lo] == nums[nums[lo] - 1]) // duplicates in high indexswap(nums, lo, hi--);elseswap(nums, lo, nums[lo] - 1); // swap to correct position }}for(int i = 0; i < nums.length; i++)if(nums[i] != i + 1)return i + 1;return nums.length + 1;}private void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;} }?
更簡潔的寫法是
public class Solution {public int firstMissingPositive(int[] nums) {if(nums == null || nums.length == 0)return 1;for(int i = 0; i < nums.length; i++) {if(nums[i] > 0 && nums[i] < nums.length && nums[i] != nums[nums[i] - 1]) {swap(nums, i, nums[i] - 1);i--; // recheck newly swaped value at position i } } for(int i = 0; i < nums.length; i++)if(nums[i] != i + 1)return i + 1;return nums.length + 1;}private void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;} }?
二刷:
二刷依然寫得比較復(fù)雜, reference里面有很多很簡潔的寫法,要加以學(xué)習(xí)。思路是利用排除法和雙指針夾逼,使用類似于sort colors里面的swap()來在O(n)的時間和O(1)的space遍歷數(shù)組,我們有一下幾種case:
可以簡化的地方有很多很多很多。希望三刷的時候能理清邏輯,大大簡化。
Java:
Time Complexity - O(n), Space Complexity - O(1)
?
public class Solution {public int firstMissingPositive(int[] nums) {if (nums == null || nums.length == 0) {return 1;}int lo = 0, hi = nums.length - 1;while (lo <= hi) {if(nums[lo] <= 0 || nums[lo] > hi) {swap(nums, lo, hi--);} else if (nums[lo] == lo + 1) {lo++;} else if (nums[lo] < lo + 1) {swap(nums, lo, hi--);} else if (nums[lo] == nums[nums[lo] - 1]) {swap(nums, lo, hi--);} else {swap(nums, lo, nums[lo] - 1);}}for (int i = 0; i < nums.length; i++) {if (nums[i] != i + 1) {return i + 1;}}return nums.length + 1;}private void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;} }?
把很多條件都放在了一起,少了幾行。 還可以設(shè)置一個index變量來避免第二次循環(huán)的查找,留給三刷了。
public class Solution {public int firstMissingPositive(int[] nums) {if (nums == null || nums.length == 0) {return 1;}int lo = 0, hi = nums.length - 1;while (lo <= hi) {if (nums[lo] == lo + 1) {lo++;} else if(nums[lo] <= 0 || nums[lo] > hi || nums[lo] < lo + 1 || nums[lo] == nums[nums[lo] - 1]) {swap(nums, lo, hi--);} else {swap(nums, lo, nums[lo] - 1);}}for (int i = 0; i < nums.length; i++) {if (nums[i] != i + 1) {return i + 1;}}return nums.length + 1;}private void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;} }?
?
題外話:
1/23/2016,現(xiàn)在我家已經(jīng)徹底被大雪封住了。有生以來從來沒見過這么大的雪,還在繼續(xù)下著。我家的門在一個走廊里,整個走廊現(xiàn)在都是30厘米左右高的雪。出去走廊走到外面的話,雪都累積到了大腿根部。幸好之前屯了不少糧食,現(xiàn)在還可以在家里吃鴛鴦火鍋(小肥羊辣湯/海底撈酸菜魚湯)。 據(jù)說有20萬戶斷電,希望大家都平安吧。
?
三刷:
用得二刷邏輯。
Java:
public class Solution {public int firstMissingPositive(int[] nums) {if (nums == null || nums.length == 0) return 1;int lo = 0, hi = nums.length - 1;while (lo <= hi) {if (nums[lo] > hi || nums[lo] <= 0) swap(nums, lo, hi--);else if (nums[lo] == lo + 1) lo++;else if (nums[lo] < lo + 1) swap(nums, lo, hi--);else if (nums[lo] == nums[nums[lo] - 1]) swap(nums, lo, hi--);else swap(nums, lo, nums[lo] - 1);}for (int i = 0; i < nums.length; i++) {if (nums[i] != i + 1) return i + 1;}return nums.length + 1;}private void swap(int[] nums, int i, int j) {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;} }?
Reference:
https://leetcode.com/discuss/20385/concise-o-n-solution
https://leetcode.com/discuss/32761/clear-java-solution
https://leetcode.com/discuss/60525/100%25-fast-elegant-java-index-based-solution-with-explanation
https://leetcode.com/discuss/28531/o-1-space-java-solution
https://leetcode.com/discuss/24013/my-short-c-solution-o-1-space-and-o-n-time
?
總結(jié)
以上是生活随笔為你收集整理的41. First Missing Positive的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里巴巴天池大数据竞赛黄金联赛全面开战,
- 下一篇: php的系统常量