Leetcode_first-missing-positive (updated c++ and python version)
来源:互联网 发布:centos samba图形界面 编辑:程序博客网 时间:2024/05/17 15:35
地址:http://oj.leetcode.com/problems/first-missing-positive/
思路:模拟题。用一个足够长的布尔数组来存储。时间复杂度O(N), 空间复杂度都O(k).
在做题之前应该明白第一个缺失的正数应该是1~n+1之间的,如果1~n都被填满,那么应该返回n+1
如果k存在,则放在k-1的下标处,即A[k-1],具体如下操作:
1. 如果A[k] <=0 || A[k] > n, 无效, k递增,下一次循环
2. 如果A[k] <= k+1, 直接将A[k]赋值为k+1,k递增,下一次循环
3. 否则,若A[ A[K]-1 ] 值 <=0 || >n, 说明A[ A[K]-1 ] 值无效,直接将A[ A[K]-1 ] 赋值为A[k]
4. 否则,[ A[K]-1 ] 值有效,如果A[k] 与 A[ A[k]-1 ] 不相等, 置换,(相等的话k递增继续处理,因为A[k]的值被其后的A[A[k]-1]保留了)
5. 否则,k递增下一次循环
参考代码:c++
//SECOND TRIAL, O(N) running time with constant spaceclass Solution {public:int firstMissingPositive(int A[], int n) {int k = 0;while(k<n){if(A[k]>0 && A[k]<=n){if(A[k]<=k+1)A[A[k]-1] = A[k++];else if(A[A[k]-1]<=0 || A[A[k]-1]>n)A[A[k]-1] = A[k++];else if(A[A[k]-1]!=A[k])swap(A[k], A[A[k]-1]);else++k;}else++k;}for(int i = 0; i<n; ++i)if(A[i]!=i+1)return i+1;return n+1;}};
python:
class Solution:# @param A, a list of integers# @return an integerdef firstMissingPositive(self, A):cnt = 0while cnt < len(A):if A[cnt] > 0 and A[cnt] <= len(A):if A[cnt] <= cnt + 1:A[ A[cnt]-1 ] = A[cnt]cnt += 1elif A[ A[cnt]-1 ] <=0 or A[ A[cnt]-1 ]>len(A):A[ A[cnt]-1 ] = A[cnt]cnt += 1elif A[cnt] != A[ A[cnt]-1 ]:#better to use tmp to swap#tmp = A[cnt]#A[cnt] = A[A[cnt]-1]#A[tmp-1] = tmp#Attention!! this one is OK, but A[cnt], A[A[cnt]-1] = A[A[cnt]-1], A[cnt] is wrongA[A[cnt]-1], A[cnt] = A[cnt], A[A[cnt]-1]else:cnt += 1else:cnt += 1for i in range(0, len(A)):if A[i] != i+1:return i+1return len(A)+1
0 0
- Leetcode_first-missing-positive (updated c++ and python version)
- LeetCode_First Missing Positive
- leetcode_First Missing Positive
- leetCode_First Missing Positive
- Leetcode_pascals-triangle (updated c++ and python version)
- Leetcode_unique-paths (updated c++ and python version)
- Leetcode_path-sum (updated c++ and python version)
- Leetcode_jump-game (updated c++ and python version)
- Leetcode_valid-number(updated c++ and python version)
- Leetcode_multiply-strings (updated c++ and python version)
- Leetcode_maximum-depth-of-binary-tree (updated c++ and python version)
- Leetcode_binary-tree-level-order-traversal (updated c++ and python version)
- Leetcode_remove-duplicates-from-sorted-list (updated c++ and python version)
- Leetcode_merge-sorted-array(updated c++ and python version)
- Leetcode_pascals-triangle-ii (updated c++ and python version)
- Leetcode_length-of-last-word (updated c++ and python version)
- Leetcode_trapping-rain-water(updated c++ and python version)
- *Leetcode_binary-tree-maximum-path-sum (updated c++ and python version)
- 怎么实现的SDL播放画面嵌入MFC控件中去的。
- 9.7 Region(部分)
- list<>
- SP自定义标签中,doStartTag()方法的返回值,EVAL_BODY_INCLUDE与SKIP_BODY的区别
- mysql数据量很大时两种不同存储过程
- Leetcode_first-missing-positive (updated c++ and python version)
- Mantis 查看问题 Unknown column 'user_id'……mantis_bug_file_table 问题
- c++第五周项目一三角形类2
- iOS第三方支付(支付宝SDK)
- 如何显示RepositoryItemHyperLinkEdit控制在一个DevExpress GridControl
- android listview实现快速查询A—Z (模拟一些天气搜狐,网易等天气预报)
- 求职面试中你需要知道的一些事
- UVA 11766 - Racing Car Computer(DP)
- LZMA压缩/解压缩工具命令行用法参考