LeetCode OJ First Missing Positive
来源:互联网 发布:淘宝专柜代购5折可信吗 编辑:程序博客网 时间:2024/06/06 14:27
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.
刚开始的方法:
class Solution {public: int firstMissingPositive(int A[], int n) { bool here[99999]; for (int i = 0; i < 99999; i++) { here[i] = false; } for (int i = 0; i < n; i++) { if (A[i] > 0) here[(A[i] % 99999)] = true; } for (int i = 1; i < 99999; i++) { if (here[i] == false) return i; } return 99999 + 1; }};
尽管是常数空间,但是这个常数会经常大于n
后来在discuss里面看到的方法:
首先将数组处理,使得A[i] = i;
这里看似两重循环,但是每一次的内部循环,都将使得A[target] = target,这样内部循环的控制条件之一target != A[target]将会变得越来越苛刻,对于一个n长度的数组,A[target] = target在最坏的情况下也只需n步即可完成;
最后再扫一遍,发现A[i] != i的即为所求;
但在这里,数组的起始点是0,导致少了一位可使用的位置,因而用变量last来指代A[n]。
class Solution {public: int firstMissingPositive(int A[], int n) { if (n == 0) return 1; int last; for (int i = 0; i < n; i++) { int target = A[i]; while (0 <= target && target < n && target != A[target]) { swap(target, A[target]); } if (target == n) last = target; } for (int i = 1; i <= n; i++) { if (i == n) { if (last != i) return i; else return n + 1; } if (A[i] != i) return i; } return n; }};
0 0
- LeetCode OJ:First Missing Positive
- LeetCode OJ - First Missing Positive
- LeetCode OJ First Missing Positive
- 【LeetCode OJ 41】First Missing Positive
- LeetCode OJ-41-First Missing Positive
- LeetCode: First Missing Positive
- LeetCode First Missing Positive
- LeetCode: First Missing Positive
- [Leetcode] First Missing Positive
- [LeetCode] First Missing Positive
- [Leetcode] First Missing Positive
- leetcode First Missing Positive
- LeetCode First Missing Positive
- [leetcode] first missing positive
- LeetCode: First Missing Positive
- [LeetCode]First Missing Positive
- LeetCode-First Missing Positive
- [leetcode] First Missing Positive
- 题目1021题 统计字符 九度Online Judge
- Number of 1 Bits(计算一个数中有多少位为1)
- LeetCode OJ Longest Valid Parentheses
- poj3067
- Android添加library报错
- LeetCode OJ First Missing Positive
- RadioGroup调用check(id)方法时,onCheckedChanged方法被执行多次调用解决办法
- 【数据结构】——排序算法——1.3、二叉树排序
- 关于sizeof的问题(2)——求结构体大小
- libCEF中C++与JavaScript的交互调用
- php do while
- LeetCode OJ Surrounded Regions
- Sql Developer关联Jdk版本的配置修改
- MySQL锁