LeetCode First Missing Positive
来源:互联网 发布:与制工作室长板淘宝 编辑:程序博客网 时间:2024/06/06 09:50
题目:
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) { removeNonPositive(A, n); //使用负号记下是否出现for(int i = 0; i < n; i++) {if(abs(A[i]) <= n) {if (A[abs(A[i])-1] > 0)A[abs(A[i])-1] *= -1;}} for(int i = 0; i < n; i++)if(A[i] > 0)return i+1;return n+1; }private://去除数组中小于等于0的数 void removeNonPositive(int A[], int &n) {int k = 0;for(int i = 0; i < n; i++) {if(A[i] > 0)A[k++] = A[i];}n = k;}};
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
- SSH框架+Oracle整合之问题2
- 数据结构与算法分析学习笔记--第四章(二叉树:创建、递归遍历、非递归遍历、根据数据删除结点等)
- 三、cocos是如何将c++类导出到lua的?
- 母函数入门+模板
- 设计模式—原型、模板方法模式
- LeetCode First Missing Positive
- 日志打印的5点建议
- Oracle中dual表
- 内容显示页调用方法
- 快速排序
- emc测试中,esd里直接放电和间接放电是什么意思
- Codeforces Round #244 (Div. 2)——Checkposts
- 用PHP与XML进行网站编程
- 高效5步走,快速搭建Hadoop2伪分布环境