https://leetcode.com/problems/first-missing-positive/

给定一个长度为len的无序数组nums,找到其第一个丢失的正整数。

解法:

使用cyclic swapping algorithm。将满足条件  0 < num <= nums.size()的num放到下标为num的位置上,最终第一个nums[i]!=i的i即是最小的丢失的正整数。

代码将num放在下标为num-1的位置,思想是一样的。

注意交换的条件,避免死循环。

class Solution
{
public:
int firstMissingPositive(vector<int>& nums)
{
int res=;
for(int i=; i<nums.size(); i++)
while(nums[i]> && nums[i]<=nums.size() && nums[i]!=i+ && nums[nums[i]-]!=nums[i])
swap(nums[i], nums[nums[i]-]);
for(int i=;i<nums.size();i++)
if(nums[i]!=i+)
return i+;
return nums.size()+;
}
};

leetcode_41. First Missing Positive_cyclic swapping的更多相关文章

  1. LeetCode: First Missing Positive 解题报告

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

  2. [Algorithm] Find first missing positive integer

    Given an array of integers, find the first missing positive integer in linear time and constant spac ...

  3. 【spring boot】整合LCN,启动spring boot2.0.3 启动报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

    spring boot 2.0.3启动报错: Error starting ApplicationContext. To display the conditions report re-run yo ...

  4. cyclic swapping algorithm

    原文见:https://leetcode.com/problems/couples-holding-hands/discuss/113362/JavaC%2B%2B-O(N)-solution-usi ...

  5. error C4430:missing type specifier 解决错误

    错误    3    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...

  6. Missing Push Notification Entitlement 问题

    最近打包上传是遇到一个问题: 描述: Missing Push Notification Entitlement - Your app includes an API for Apple's Push ...

  7. PHPmailer关于Extension missing: openssl报错的解决

    最近在写一个网页的时候,需要用到PHPmailer来发送邮件,按照官网上给出的demo写出一个例子,却报错Extension missing: openssl 最后发现需要修改php.ini中的配置: ...

  8. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. [LeetCode] Missing Ranges 缺失区间

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

随机推荐

  1. 使用masonry手写约束

    在iOS开发过程中,手写contraints是非常痛苦的一件事情,往往那么一丢丢功能要写大量的代码,非常容易发生错误,并且非常不方便调试.所以只有在不得以的情况下才采用手工方式写contraints, ...

  2. DP专辑之线性DP

    POJ1390 题目链接:http://poj.org/problem?id=1390 分类:记忆化搜索 dp[i][j][k] 表示,从i到j块且j后面有k块与第j块的颜色一样.dp[l][r][k ...

  3. JAVA编程思想中总结的与C++的区别

    Java和C++都是面向对象语言.也就是说,它们都能够实现面向对象思想(封装,继乘,多态).而由于c++为了照顾大量的C语言使用者,而兼容了C,使得自身仅仅成为了带类的C语言,多多少少影响了其面向对象 ...

  4. 003--linux用户权限常用命令

    一.useradd命令选项 –u:指定用户的UID         useradd –u 1024 mu    #指定mu的UID为1024 –g:指定用户所属的群组   useradd –g jac ...

  5. 简析hotjar录屏功能实现原理

    简析hotjar录屏功能实现原理 众所周知,hotjar中录屏功能是其重要的一个卖点,看着很牛X酷炫的样子,今天就简单的分析一下其可能实现(这里只根据其请求加上个人理解分析,并不代表hotjar中真实 ...

  6. HDU 6092:Rikka with Subset(dp)

    分析 很多个较小的数字可以随机组合成较大的数字,所以B数组从小到大开始遍历,除了空集,最小的那个存在的个数对应的数字必然是a数组中的数字. 每求出这一部分之后,更新后续的B序列. 分析完后,主要的难点 ...

  7. sublime text3 注册码

    —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...

  8. Integer Cache(带你脱坑)

    Integer Cache 废话不多说----->直接上代码: public class IntegerDemo { public static void main(String[] args) ...

  9. Linux下自动还原MySQL数据库的Shell脚本

    创建shell脚本topjui_source.exp,内容如下: #!/usr/bin/expect spawn echo "###### running... ######" s ...

  10. iOS MD5 (Swift3)

    import Foundation extension Int { func hexedString() -> String { return NSString(format:"%02 ...