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] ...
随机推荐
- torch7入门(安装与使用)
http://blog.csdn.net/real_myth/article/details/52291636 1.安装与使用 测试系统是ubuntu14.04LTS,默认安装在-/torch,当然我 ...
- 查询oracle数据库里面所有的表名
如果是当前用户,"select * from tab"即可
- vc编辑器常用设置
代码格式化 1.选中代码: 2.ctrl+K: 3.ctrl+F; 显示行号
- 关于STM32 MDK中USE_STDPERIPH_DRIVER问题的解释
初学STM32,在RealView MDK 环境中使用STM32固件库建立工程时,初学者可能会遇到编译不通过的问题.出现如下警告或错误提示: warning: #223-D: function &qu ...
- Ansible 入门指南 - 常用模块
介绍 module 文档: 官宣-模块分类的索引 官宣-全部模块的索引 在playbook脚本中,tasks 中的每一个 action都是对 module的一次调用.在每个 action中: 冒号前面 ...
- redhat7 防火墙设置
查看防火墙的状态# firewall-cmd --staterunning # systemctl stop firewalld //关闭防火墙服务# systemctl start firewa ...
- 【maven】在IDEA上 使用maven进行打包时报错:Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.10.3:jar
报错内容如下: [INFO] ------------------------------------------------------------------------ [INFO] BUILD ...
- The destination you provided is not a full refname (i.e., starting with "refs/")
$ git push v5 v5/hotfix/5.1:hotfix/5.1-quartzerror: The destination you provided is not a full refna ...
- 第十一章 非对称加密算法--DH
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 11.1.非对称加密算法 特点: 发送方和接收方均有一个密钥对(公钥+私钥),其中公 ...
- C# 查出数据表DataTable 清除一列中的重复项保留其他项
http://bbs.csdn.net/topics/391085792 DataTable 老表= 新表.AsEnumerable().GroupBy(p => p["姓名& ...