41. First Missing Positive *HARD*
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.
int firstMissingPositive(vector<int>& nums) {
int n = nums.size(), t, i;
for(i = ; i < n; i++)
{
t = nums[i];
while(t > && t < n && nums[t-] != t)
{
swap(nums[i], nums[t-]);
t = nums[i];
}
}
for(i = ; i < n; i++)
{
if(nums[i] != i+)
return i+;
}
return n+;
}
Idea:
*
* We can move the num to the place whcih the index is the num.
*
* for example, (considering the array is zero-based.
* 1 => A[0], 2 => A[1], 3=>A[2]
*
* Then, we can go through the array check the i+1 == A[i], if not ,just return i+1;
41. First Missing Positive *HARD*的更多相关文章
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- 刷题41. First Missing Positive
一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对 ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 41. First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
- leetcode problem 41 -- First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- Elasticsearch 疑难解惑
Elasticsearch是如何实现Master选举的? Elasticsearch的选主是ZenDiscovery模块负责的,主要包含Ping(节点之间通过这个RPC来发现彼此)和Unicast(单 ...
- Python3基础 else 循环完整结束才执行
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 自己写操作系统---bootsector篇
其实博主本来想在寒假自己写一个OSkernal的,高高兴兴的影印了本<一个操作系统的实现>. 然后又去图书馆借来<30天自制操作系统>和<X86/X64体系探索编程> ...
- C#学习笔记(十八):数据结构和泛型
数据结构 只有这四种 a.集合:数据之间没有特定的关系 b.线性结构:数据之间有一对一的前后联系 c.树形结构:数据之间有一对多的关系,一个父节点有多个子节点,一个子节点只能有一个父节点 d.图状结构 ...
- Mybatis配置映射文件中parameterType的用法小结
原创: 在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配 ...
- Ubuntu 14.04 下 安装Protocol Buffers
参考: Protocol Buffers - Google's data interchange format Ubuntu 14.04 下 安装Protocol Buffers 环境 Ubuntu ...
- 【TCP/IP详解 卷一:协议】第十八章 TCP连接 的建立与终止 (2)其余内容
18.5 TCP的半关闭 牢记 TCP 是 全双工 的. 半关闭:TCP提供了连接的一端 在结束了它的发送后 还能接收来自另外一端数据的能力.但是只有很少的应用程序利用它. 为了实现这个特性,编程接口 ...
- MUI --- 多个页面之间的传值 A页面B 页面 C页面
问题: 夸页面传值的,A.B.C三个页面,点A弹出B,C是B子页面;A有两个值要传到C页面中,要怎么样传递呢? A页面传值就不累述了 B页面才是关键 mui.plusReady(function() ...
- OpenGL遮挡查询
转自:http://www.cnblogs.com/mazhenyu/p/5083026.html 在一个场景中,如果有有些物体被其他物体遮住了不可见.那么我们就不需要绘制它.在复杂的场景中,这可以减 ...
- golang基础类型
1.变量 Go使用关键字var定义变量,类型信息放在变量名后面,初始化为零,如下: 在函数内部还有一种更简洁的:=定义方式 变量赋值,两个变量可直接交换 注意:函数内部定义的变量没使用的话编译器会报错 ...