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.

思路:

又是一个不让排序,但是要得到跟排序有关信息的。想了半天,只能空间换时间,可是又只能用常量空间,这可难到我了。完全想不出来。放弃看答案。

发现,答案中其实是用了空间换时间的思想的,但是这里的空间直接就用了最开始的数组。

具体的思路是:从第一个数字开始,把正数换到按顺序排序的位置上。即1放到位置0,2放到位置1。直到当前位置的数字变成正确的数字。如果遇到小于0的数字或者大于元素个数的数字跳过,因为如果后面有对应位置的数字的话是会换过来的。

代码:

class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
for(int i = ; i < nums.size(); ++i)
{
while(nums[i] > && nums[i] <= nums.size() && nums[nums[i] - ] != nums[i]) //其实这里不要nums[i] > 0 也可以 就是会慢一点
swap(nums[i], nums[nums[i] - ]); //交换数字 直到当前位置数字正确 或者 数字无法按序放在当前矩阵中
} int i;
for(i = ; i < nums.size(); ++i)
{
if(nums[i] != i + )
break;
}
return i + ;
}
};

【leetcode】First Missing Positive(hard) ☆的更多相关文章

  1. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  2. 【leetcode】First Missing Positive

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  3. 【LeetCode】163. Missing Range

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...

  4. 【LeetCode】268. Missing Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...

  5. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  6. 【leetcode】1237. Find Positive Integer Solution for a Given Equation

    题目如下: Given a function  f(x, y) and a value z, return all positive integer pairs x and y where f(x,y ...

  7. 【LeetCode】163. Missing Ranges 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  8. 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  9. 【leetcode】1228.Missing Number In Arithmetic Progression

    题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...

随机推荐

  1. 解决pydev无法增加jython271 interpreter的问题

    ============================解决pydev无法增加jython271 interpreter的问题============================ 从jython. ...

  2. WCF binding的那些事!!!

    原文地址:http://www.cnblogs.com/Anima0My/archive/2008/04/16/1156146.html WCF中常用的binding方式: BasicHttpBind ...

  3. ionic导航之后返回功能的说明

    当我导航view之后,再使用$location.path("/path/origin")方法重新定位到初始页面,在深入进入其他的view之后使用这个方法就遇到了问题. 假设这个设置 ...

  4. Linux网络参数设置

    1.ifconfig  查询.设定网络卡与ip     设置桥接网络 # vi /etc/sysconfig/network-script/ifcfg-br0       DEVICE=br0     ...

  5. 电脑网线/水晶头的连接方法(A类,B类)

    一般的橙白,橙,绿白,蓝,蓝白,绿,棕白,棕. 若是只有四根线的,则任选四根,做线时对应水晶头的1\2\3\6四个入口压制即可. 如果只有一根网线,但想两台机子同时上网,不增加外设,做网线时45水晶头 ...

  6. 怎样更改wordpress登陆 URL防止恶意注册

    WP 默认的登陆 URL 是 wp-login.php或wp-admin.php,许多spamer会根据这些footprint来收集可注册的wordpress站点,然后你的站内就多出许多垃圾评论.如果 ...

  7. 格式化Double类型

    //格式化Double类型 //F:默认是2位小数点 //F6:输出小数点后6位,不够的话用0补齐 //G:默认输出原先的,保留小数点后面的位数 LalTotal.Text = "合计:原始 ...

  8. 扩展RBAC用户角色权限设计方案

    RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...

  9. 【转】WordPress转PHPCMS策略-数据库完美转换

    来源:http://www.sjyhome.com/php/wp-to-pc-sql.html WordPress的访问速度不可恭维?那就试试能够生成纯静态的PHPCMS,保证能够让你的网页访问速度有 ...

  10. Joda-time是java处理时间非常棒的jar

    http://www.joda.org/joda-time/ maven: <dependency> <groupId>joda-time</groupId> &l ...