[leetcode刷题系列]First Missing Positive
来源:互联网 发布:淘宝店铺无法订购花呗 编辑:程序博客网 时间:2024/06/15 21:50
好吧,智商是硬伤。 我承认题目要求的O(n)复杂度和常量空间,我没想到算法。于是就Google了下。
这种思路确实第一次见。长见识了。
class Solution {public: int firstMissingPositive(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function for(int i = 0; i < n; ++ i){ int now = i; while(now >= 0 && now < n){ if(A[now] - 1 == now) break; int target = A[now] - 1; if(target < 0 || target >= n || A[now] == A[target]) break; swap(A[target], A[now]); } } for(int i = 0; i < n; ++ i) if(A[i] != i + 1) return i + 1; return n + 1; }};
- [leetcode刷题系列]First Missing Positive
- LeetCode刷题笔录First Missing Positive
- leetcode系列(56)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
- [LeetCode] First Missing Positive
- LeetCode - First Missing Positive
- uva 565 Pizza Anyone?(二进制+搜索)
- hdu 2602 Bone Collector
- 什么是数据库分表技术
- strictmode
- HDU 4649 Professor Tian (2013多校联合5 1007)
- [leetcode刷题系列]First Missing Positive
- HDU 1070 Milk
- ARM高分笔记--(三)ARM寻址方式
- 数据结构--树和二叉树
- squid---StatCounters.h
- Python 列表和元组
- hdu 2190 : 悼念512汶川大地震遇难同胞——重建希望小学
- 浅析OpenGL光照
- php中对象转换数组与数组转换对象实例