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. javaScript的几个问题简答

    1.javascript的typeof返回哪些数据类型    Object.number. function. boolean. underfind 2.例举3种强制类型转换和2种隐式类型转换?    ...

  2. Synchronized之三:Synchronized与线程中断、线程wait

    线程中断 见<Thread之八:interrupt中断> 正如中断二字所表达的意义,在线程运行(run方法)中间打断它,在Java中,提供了以下3个有关线程中断的方法 //中断线程(实例方 ...

  3. py-day15_css+js_初

    css+js_初 一.鼠标移动变色 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  4. 【旧文章搬运】Windbg+Vmware驱动调试入门(三)---Windbg基本调试入门

    原文发表于百度空间,2009-01-09========================================================================== 这一节的内 ...

  5. std::unique

    类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...

  6. Python学习之旅—生成器对象的send方法详解

    前言 在上一篇博客中,笔者带大家一起探讨了生成器与迭代器的本质原理和使用,本次博客将重点聚焦于生成器对象的send方法. 一.send方法详解  我们知道生成器对象本质上是一个迭代器.但是它比迭代器对 ...

  7. 看鸟哥的Linux私房菜的一些命令自我总结(一)

    -显示目前所支持的语言  echo &LANG -修改语言成为英文系统  LANG=en_US -显示日历的命令 cal [[month] year] -惯用关机命令 shutdown 参数: ...

  8. 【WEB基础】HTML & CSS 基础入门(4)列表及其样式

    前面 网页中漂亮的导航.整齐规范的文章标题列表和图片列表等等.这些都是离不开HTML里一个重要的元素----列表,在HTML中有无序列表.有序列表和定义列表三种类型.其中,无序列表应用最为广泛,下面, ...

  9. 洛谷 - P1355 - 神秘大三角 - 简单计算几何

    https://www.luogu.org/problemnew/show/P1355 判断一个点和三角形的位置关系,最简单的思路就是用向量. 首先排除掉和三角形顶点重合的情况. 把三角形设计成一个首 ...

  10. 算法学习--Day2

    今天要多学一些内容了,昨天就写了一点sort和struct的用法,今天写了两道关于日期的题目,记录在这里. 题目描述 有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天 ...