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)空间。
一开始想的是记录正数的个数和他们的和,利用1~n的公式求和,减去sum,看缺了哪个。
指导提交错误,发现居然可以输入 [1,1]。原来还可以有重复的。
想到应该是利用了原来的数组。
用原来的数组做哈希。
对于每一个数,把它放到它应该在的位置上。比如一个4,把它放到a[3]的位置上。
原来的数怎么办呢?调换。a[3]原来的数就放在a[i]的位置。反复调换,反复调换。当然其中有坑的,我提交了三次才AC了。
下面代码的执行效果就是:
比如输入 -3 1 5 3 2
index 0 1 2 3 4
————————————————
i = 0: -3 1 5 3 2 负值跳过了
i = 1: 1 -3 5 3 2 swap(-3,1)
i = 1: 1 -3 5 3 2 负值跳过
i = 2: 1 -3 3 swap(5,2)
i = 2: 1 2 -3 3 5 swap(2,-3)
i = 2: 1 2 -3 3 5 负数跳过
i = 3: 1 2 3 -3 5 swap(-3,3)
i = 4: 1 2 3 -3 5 负数跳过
最后变成了 1 2 3 -3 5
很明显,缺的是4啦。
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int i,j,n,tmp;
vector<int> &a = nums;
n = a.size();
if(n == ) return ;
for(i = ,j = ; i < n; i++)
{
if(a[i] > && a[i] != i + ) //一个正数不在它自己的位置上,
{
tmp = a[i] - ; //tmp是它应该呆的位置
if(tmp < n && a[tmp]!= a[i]) //防止下标越界和 死循环
{
swap(a[tmp],a[i]);
i--;
} //这个调换最多会有多少次呢? 最多也就是n次。因为n次以后,n个数都会在正确的位置了。
}
}
for(i = ; i<n; i++) //数一数,看看中间却了哪个呢?如果都不缺,那就是1..n的连续数组,就返回 n + 1
{
if(a[i] != i + )
break;
}
return i + ;
}
};
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] ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
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 ---------------------------------------------------------- ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
随机推荐
- mina2的processor
processor顾名思义,就是进行IO处理,处理当前session的数据读写,并进行业务处理. 在mina server初始化的时候,会初始化一个processor池,通过NioSocketAcce ...
- IDC:UPS(不间断电源)
ylbtech-IDC:UPS(不间断电源) UPS(Uninterruptible Power System/Uninterruptible Power Supply),即不间断电源,是将蓄电池(多 ...
- 用ADB打开MUMU模拟器的WLAN用于设置代理IP
adb connect 127.0.0.1:7555 adb shell am start -a android.intent.action.MAIN -n com.android.settings/ ...
- 修改ECSHOP的小数点保留位数
客户站点http://carfa.hk79.2ifree.com 原来的程序直接取整了,现在做下面修改. 首先打开文件 /carfa/web/includes/lib_common.php 第一步:在 ...
- java为什么匿名内部类的参数引用时final(转)
https://blog.csdn.net/z69183787/article/details/68490440 https://www.zhihu.com/question/21395848 htt ...
- UTC时间和普通时间的区别
UTC时间 [root@openstack01 ~]# timedatectl Local time: Sat 2018-08-18 23:04:24 CST Universal ti ...
- Ubuntu 搭建 Zerotier One MOON 根目录服务器
原文转摘:http://www.congan.wang/archives/947 博主倒腾了一天,总算搞定了,主要是受到各种搭建教程的错误引导,导致关键过程错误.官网的MOON搭建教程:https:/ ...
- JSP基础解析
EL表达式 https://www.cnblogs.com/zhouguanglin/p/8117406.html EL(Expression Language) 是为了使JSP写起来更加简单 ...
- android开发 一个更优的listView的写法
布局xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...
- Maintenance Planner calculate SPs by manual
note Are you unable to view your system or updated system information? Apply the latest version of t ...