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. CodeForces546D:Soldier and Number Game(筛区间素数因子个数和)

    Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and giv ...

  2. java中为什么inputstreamreader和buffered reader要配合着用

    因为InputStreamReader是字节输出(汉字会被分为两个字节),而BufferedReader是它的“包装”(整行读取),效率更高,所以配合使用更好.可以通过BufferedReader 流 ...

  3. 任务30:RoutingMiddleware介绍以及MVC引入

    任务30:RoutingMiddleware介绍以及MVC引入 前面讲到app.Map的方式,它也可以实现路由 当我们的url是task的时候,就会执行里面的context的输出内容 app.Map( ...

  4. 一个NodeJS写的基于MVC的服务器

    目前实现了静态文件下载.根据地址导航到控制器相应的控制器方法,但视图模版功能目前还未实现. 服务器代码(httpserver.js): var http = require("http&qu ...

  5. 【Unity3d】3d角色换装实现原理及步骤

    http://www.cnblogs.com/dosomething/archive/2012/04/15/2450526.html 1.角色模型制作 unity3d支持Skin动画  但是不支持Ph ...

  6. P1226神经网络

    提交了7次,看了无数题解,要死啊~~~.(无限吐槽这道题...) 据说是Toposort,我其实也不是很清楚,反正BFS就可以过:写题之前先把题看懂: 根据公式,因为入度为零的点不会被传递,所以阈值是 ...

  7. Jquery | 基础 | 慕课网 | 层级选择器

    选择器中的层级选择器处理关系类型: 子元素 后代元素 兄弟元素 相邻元素 <!DOCTYPE html> <html> <head> <meta http-e ...

  8. c++ 语法解析

    大小 size()是取字符串长度的,跟length()用法相同 size_t其实是一种类型,类似于无符号整形(unsignted int).可以理解成unsignted int size,当unsig ...

  9. Jenkins持续集成多任务之MultiJob

    项目实践中,我们可能需要在多个任务发布成功后在执行某个任务,这里就需要用到MultiJob这个插件. 案例场景:有3个任务:A.B.C,其中C任务需要等A和B执行成功后才会执行,那么就要先执行A和B, ...

  10. 洛谷 P1445 [Violet]樱花

    #include<cstdio> #include<algorithm> #include<cstring> #include<vector> usin ...