leetcode第40题--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.
给定一个没有排好序的数组。找出其中第一个消逝的正整数。例如 [1,2,5,4]那就是缺3.
思路:利用counting sort的想法。遍历一次,如果当前的数超出了 1 到 n-1的范围,跳过。如果是1到n-1但是,A[i]不等于i+1,并且在 A[i] - 1 的位置的数不满足 A[A[i]-1]+1的要求,那就将两个位置的数置换。同时,还要在当前位置判断换过来的数是否满足上述要求。这个哥们的图很好理解。
class Solution {
public:
void swap40(int A[], int i, int j) // 其实std有自带的函数swap(A[i], A[j])
{
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
return;
}
int firstMissingPositive(int A[], int n)
{
if (n <= 0 )
return 1;
for (int i = 0; i < n; ++i)
{
if (A[i] > 0 && (A[i] - 1 < n) && A[i] != i + 1 && A[i] != A[A[i] - 1])
{swap(A, i, A[i] - 1);i--;} // i--是为了和之后的++i持平,因为交换的条件满足的话,交换过来的数还要再判断
}
for (int i = 0; i < n; ++i)
{
if (A[i] != i + 1)
return i + 1;
}
return n + 1;
}
};
leetcode第40题--First Missing Positive的更多相关文章
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
- LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- LeetCode(41)First Missing Positive
题目 Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...
- LeetCode刷题 fIRST MISSING POSITIVE
Given an unsorted integer array,find missing postive integer. For example , Given [1,2,0]return 3, a ...
- leetcode第40题:组合总和II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- LeetCode OJ:First Missing Positive (第一个丢失的正数)
在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
随机推荐
- lsblk请参阅块设备
lsblk可以查看分区和挂载的磁盘使用情况 lsblk全部的參数 -a, --all 显示全部设备 -b, --bytes 以bytes方式显示设备大小 - ...
- 致网友Wonderfei的一封信(怎样选择自己主动化框架的几点拙见)
注:本来这封信要发给Wonerfei网友的,可是由于每次仅仅能发200字,所以干脆贴到博客上,叫Wonderfei同学到这上面来看,也算是我自己的一个暂时总结吧.同一时候也希望大家给予Wonderfe ...
- 解决 Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat
在配置 mybatis mapper.xml文件时, 一不小心就会报如下类似的异常: Caused by: org.springframework.beans.factory.BeanCreation ...
- Java虚拟机参数设置(转)
今天在加载一幅图片时,eclipse报出如下错误: “Exception in thread "main" java.lang.OutOfMemoryError: Java hea ...
- ZooKeeper完全分布式安装和配置
ZooKeeper简介见官方网站. 1.环境说明 在两台装有centos6.4(32位)的server上安装ZooKeeper,官网建议至少3个节点.资源有限,本次实验就2台了. 须要提前安装jdk. ...
- 64位WIN7+oracle11g+plsql
64位WIN7+oracle11g+plsql安装 Posted on 2015-07-28 22:41 算命大师不算卦 阅读(27) 评论(0) 编辑 收藏 上部转自Oracle 11g R2 fo ...
- 馋-c语言的规则
在记者采访过程,有着c的认识的情况,有时会被问到有关字符搭配以及运算先后顺序的问题,比方a+++++b的值.++i+++i+++i+i的值等类似的,这都属于c的符号方面的问题.那么如何才干轻而易举的去 ...
- bnu 34986 Football on Table(数学+暴力)
pid=34986" target="_blank" style="">题目连接:bnu 34986 Football on Table 题目大 ...
- 文《左右c++与java中国的垃圾问题的分析与解决》一bug分析
文<左右c++与java中国的垃圾问题的分析与解决>一bug分析 DionysosLai(906391500@qq.com) 2014/10/21 在前几篇一博客<关于c++与jav ...
- linux_shell_根据网站来源分桶
应用场景: 3kw行url+\t+html记录 [网站混合] 需要:按照网站来源分桶输出 执行shell cat */*pack.html|awk -F '\t' '{ split($1,arr,&q ...