LeetCode First Missing Positive
来源:互联网 发布:淘宝主视频制作软件 编辑:程序博客网 时间:2024/06/16 15:53
题目要求:
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) { if(A == NULL || n == 0) return 1; for(int i = 0; i < n; ++i) { if(A[i] < n && A[i] > 0) { if(A[i] != A[A[i] - 1]) { swap(A[i], A[A[i] - 1]); --i; } } } for(size_t i = 0; i < n; ++i) { if((A[i] - 1) != i) return (i + 1); } return (n + 1); }};
0 0
- 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
- LeetCode | First Missing Positive
- Leetcode: First Missing Positive
- Leetcode:First Missing Positive
- 对linux 0.11版本内核中的进程退出和回收的理解
- 网页编码就是那点事
- 页面超时后用户退出回主页面源代码
- Hadoop源码解析之: TextInputFormat如何处理跨split的行
- 《X86汇编语言 从事模式到保护模式》问题
- LeetCode First Missing Positive
- LayoutInflater的使用
- 随机数是骗人的
- Programming Methodology in Java 笔记
- 解决ADT大量出现"Unexpected value from nativeGetEnabledTags: 0"的问题
- 几种常见排序算法的实现(冒泡法,选择法,插入法,快速排序、堆排序)
- GlusterFS源码解析 —— GlusterFS 配置
- 微软100题算法解答
- spring mvc 控制层梳理