LeetCode41 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.(Hard)
分析:
如果题目要求是O(n^2)时间复杂度,O(1)空间复杂度,或O(n)时间复杂度和O(n)空间复杂度,问题都是好解的。
前者从1开始,对每个数遍历一遍数组即可,发现没有的返回;
后者开辟一个数组flag,flag[i] == 0 或 1表示是否出现过,遍历一遍把flag填充上,再遍历一遍找到第一个没有出现过的数即可。
题目要求O(n)时间复杂度和O(1)空间复杂度,则要考虑在nums数组本身上面做文章。
考虑通过交换使得nums[i]存 i + 1,这样从头遍历找到第一个不满足的位置即可。
交换方案就是用nums[i]与 nums[nums[i] - 1]交换,直到nums[i] == i + 1或 i 超过数组范围(0...size())。
注意:加nums[i] != nums[nums[i] - 1]判断语句(见代码第6行),否则可能出现死循环。
时间复杂度,因为每次交换都会有一个元素到达正确位置,所以复杂度O(n)
代码:
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int i = ;
while (i < nums.size()) {
if (nums[i] > && nums[i] <= nums.size() && nums[i] != i + && nums[i] != nums[nums[i] - ]) { //nums[i] != nums[nums[i] - 1] 否则类似[1,1]
swap(nums[i], nums[nums[i] - ])
}
else {
i++;
}
}
for (int j = ; j < nums.size(); ++j) {
if (nums[j] != j + ) {
return j + ;
}
}
return nums.size() + ;
}
};
LeetCode41 First Missing Positive的更多相关文章
- [Swift]LeetCode41. 缺失的第一个正数 | First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- [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
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(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 ---------------------------------------------------------- ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
随机推荐
- geeksforgeeks@ Largest Number formed from an Array
http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array G ...
- python 字符串,数组,元祖操作基础巩固。
由于上个星期有点忙,没时间来抽空记一些有用的东西.丢了比较久的python很多忘记的小操作我也会重新捡起来 以前最容易搞混的 str.split() #操作会生成一个数组对象.example:'lap ...
- InternetOpenA
[ilink32 Error] Error: Unresolved external 'InternetOpenA' referenced from ..\WIN32\DEBUG\NATIVEXML ...
- IdTCPServer
IdTCPServer1 Server本身就支持多线程,一个服务端连接多个客户端. IdTCPServer1.Bindings.Add.IP := '127.0.0.1';IdTCPServer1.B ...
- HD1049Climbing Worm
Problem Description An inch worm is at the bottom of a well n inches deep. It has enough energy to c ...
- 使用timer8秒读取一次方法进行操作
public void TestofTimer() { System.Timers.Timer tt = new System.Timers.Timer(); //获取或设置引发 Elapsed 事件 ...
- HDU1963Investment(DP)
简单DP,题解见代码
- getConnection 区别
1. 这是一个接口 package javax.sql; DataSource.class /** * <p>Attempts to establish a connection with ...
- [SQL]sql语句bug
sql语句格式必须严格检查,一个空格的错误都会导致执行错误. 常见有 1:字符串的值要用 ‘ ’ 括起来 2:用 , 分隔语句段 3:切记判断条件前有一空格:eg:这里最容易出错 4:属性名是否 ...
- JavaScript 不重复的随机数
在 JavaScript 中,一般产生的随机数会重复,但是有时我们需要不重复的随机数,如何实现?本文给于解决方法,需要的朋友可以参考下 在 JavaScript 中,一般产生的随机数会重复,但 ...