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] ...
随机推荐
- Web jquery表格组件 JQGrid 的使用 - 11.问题研究
系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...
- NAT穿越
1.NAT类型 目前主要的NAT类型有如下几种: 1)Full-cone NAT, also known as one-to-one NAT 一旦一个内网地址 (iAddr:iPort) 被映射到一个 ...
- spark安装(实战)
sparksql+hive :http://lxw1234.com/archives/2015/06/294.htm 1,安装scala http://scala-lang.org/download/ ...
- 用jsp的application写一个记录用户登陆网站的数量
</head><body><%int i = 0;Object number = application.getAttribute("num");if ...
- 探讨兼容IE低版本的PC端响应式布局
http://www.jiangweishan.com/article/lowIeResposive.html 响应式布局,oh my god!!有点醉了,感觉是老生常谈的话题了.虽然已经谈过很多了, ...
- jquery手写实现单页滚动导航
效果说明:点击tab导航,页面滑动到下方相应板块.并且当页面通过鼠标滚动下去时,上方的tab也可以自动切换到当前位置的板块上. 代码说明:js中对两个动作分别写,一个是tab点击下滑到相应板块位置:一 ...
- sqlserver中分区函数 partition by的用法
partition by关键字是分析性函数的一部分,它和聚合函数(如group by)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录, partition by ...
- cf593d
题意:给出一个有n(n<=200000)的树形图,每条边有一个权值.有两种操作,1是将一个边的权值变小, 2是给定两点a,b和一个值y,用y(long long范围内)依次除以两点之间的路径上的 ...
- sqlserver中判断表或临时表是否存在
转自:http://www.cnblogs.com/yugen/archive/2010/07/25/1784749.html 1.判断数据表是否存在 方法一: use yourdb;go if ob ...
- PHP 函数(数组字符串)
函数四要素: 参数 变量 返回值 函数体 函数分类: 1.有参数的函数: function Show() { echo "hello"; } Show(); 2. 有返回 ...