Leetcode First Missing Positive
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.
给定无序整型数组,找出第一个不在数组里的正整数,要求时间复杂度为O(n),空间复杂度为O(1)
本题使用数组下标来存储相应的值,比如第k个元素(对应的下标是k-1)存储数字k,也就是A[k-1] = k。
对于大于数组长度的数字或者小于1的数字直接抛弃
一旦有了新数组,就从头开始扫描,遇到第一个A[k-1]不等于k时,输出k,如果没有遇到,那结果只能是数组长度的下一个数
class Solution {
public:
int firstMissingPositive(int A[], int n) {
for(int i = ; i < n; ++ i){
if(A[i] > && A[i]<=n){
if(A[i]- != i && A[A[i]-]!=A[i]) {
swap(A[i],A[A[i]-]);
i--;
}
}
}
for(int i = ; i < n; ++ i)
if(A[i]- != i) return i+;
return n+;
}
};
Leetcode First Missing Positive的更多相关文章
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode: First Missing Positive 解题报告
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- LeetCode – First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- LeetCode OJ-- First Missing Positive
https://oj.leetcode.com/problems/first-missing-positive/ 给一列数,找出缺失的第一个正数.要求时间复杂度 O(n) 第一步遍历一遍,找出最大的数 ...
- leetcode First Missing Positive hashset简单应用
public class Solution { public int firstMissingPositive(int[] A) { HashSet<Integer> hash=new H ...
- leetcode First Missing Positive python
class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[in ...
- leetcode:First Missing Positive分析和实现
题目大意: 传入整数数组nums,求nums中未出现的正整数中的最小值.要求算法在O(n)时间复杂度内实现,并且只能分配常量空间. 分析: 一般碰到这种问题,都先对数组进行排序,再遍历数组就可以找到最 ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- entity-model-first
- Top 5 iPad Pro Apps for Your Apple Pencil
1. Procreate - 5 to 10 dollars 2. Adobe Sketch - Free 3. Paper - Free 4. Pixelmator 5. Notes
- Mono-D在MacOS上的设置
1. 下载DMD 建议下载tar.xz压缩包,不建议下载dmg安装包,因为dmg中没有src,而后面需要用src中的内容设置代Code Completion. 地址:http://dlang.org/ ...
- css3 动画效果 定义和绑定执行
首先要定义一个动画效果 keyframes 关键字 这里动画效果执行完毕后 恢复本身的css样式 有的动画效果 移动到位置 要保持 就需要写好css 元素的位置 css里直接写 (这里是一般的 ...
- css清楚浮动的方法
- 直接拿来用,最火的.NET开源项目
综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...
- 将.dat文件导入数据库
*最近在搞文本分类,就是把一批文章分成[军事].[娱乐].[政治]等等. 但是这个先需要一些样本进行训练,感觉文本分类和"按图索骥"差不多,训练的文章样本就是"图&quo ...
- codevs1540 银河英雄传说
描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶集 ...
- SQL 查找表名 字段名
转载:http://www.accessoft.com/article-show.asp?id=6135 经常碰到一些忘记表名称的情况,此时只记得个大概,此时可通过查询系统表Sysobjects找到所 ...
- 如何查看、修改Linux的系统时间
如题: 如何修改Linux的系统时间? date -s 05/27/2011 date -s 10:24:00 clock -w 就这三条命令就OK了! 查看/修改Linux时区和时间 一.时区 1. ...