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] ...
随机推荐
- python-随机数的产生random模块
random模块用来产生随机数: 查看random模块的方法: import random random.__dir__ Out[39]: <function __dir__> rando ...
- php检查是否是数字和字母
/* 检查是否是数字和字母* php内置函数ctype_alnum检查字符串是否是数字和字母,或者两者混合* $string*/ public function is_numandlitter($st ...
- Linux下useradd命令创建的用户不能登录的问题
Linux下useradd命令创建的用户不能登录的问题 问题: 用useradd命令新创建一个用户tester 密码pwdtest mkdir -p /home/tester(创建文件夹) user ...
- Linux命令中:rsync和cp之间的区别
rsync:只拷贝那些更新的文件: cp -u:也可以实现类似效果: 两者都基本可以满足备份的需求: 只是一般情况下,用rsync做这类备份之类的事情,更多见: 在备份的操作中,拷贝,过期文件的删除是 ...
- P3066 [USACO12DEC]逃跑的BarnRunning Away From
目录 题目 思路 错误&&注意 代码 题目 luoguP3066 思路 虽说这个题目有多种做法,但 左偏树算法: 我们发现这个合并的时候并不好合并,因为存的值不是固定的 那我们是不是可 ...
- 地宫取宝|2014年蓝桥杯B组题解析第九题-fishers
地宫取宝 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
- HDU 1811(并查集+拓扑排序)题解
Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球.为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他 ...
- 总结java中的super和this关键字
知识点: 在java类中使用super引用父类的成分,用this引用当前对象 this可以修饰属性.构造器.方法 super可以修饰属性.构造器.方法 关于子类实例化过程中的内存分配,在下一篇博客中说 ...
- 【TCP/IP详解 卷一:协议】第十八章 TCP连接 的建立与终止 (2)其余内容
18.5 TCP的半关闭 牢记 TCP 是 全双工 的. 半关闭:TCP提供了连接的一端 在结束了它的发送后 还能接收来自另外一端数据的能力.但是只有很少的应用程序利用它. 为了实现这个特性,编程接口 ...
- 【TCP/IP详解 卷一:协议】第二章:链路层
2.1 引言 链路层的三个目的: (1)为IP模块发送和接收IP数据报. (2)为ARP模块发送ARP请求和接收ARP应答.地址解析协议:ARP. (3)为RARP模块发送RARP请求和接收RARP应 ...