Given an unsorted integer array, find the first missing positive integer.

For example,
Given[1,2,0]return3,
and[3,4,-1,1]return2.

Your algorithm should run in O(n) time and uses constant space.

题意:给定连续的数列,找出一个缺失的正数

思路:开始理解错了题意,没有想到题中隐藏正数从1开始这么大一个信息量(不知道,我现在理解错了没)。首先想到的是用unordered_set,这样从1开始(若1不存在,就直接找到了),不断寻找值加1是否存在,从而找到缺失的正数。必须用到常数空间,恩,换个思路。遍历数组,我们将值为A[i]放在对应的位置(A[A[i]-1]),这样就按照从小到大的顺序将数组排好序了,然后再次遍历数组,找到 i+1不等于A[i]的点。值得注意的是通过交换将A[i] 放在A[A[i]-1]以后,遍历再次启动点应该是 i ,因为,新的A[i] 值是否满足A[i] =i+1还未可知。若是A[i] =i+1或是A[i]<=0只直接跳过。代码如下:

 class Solution {
public:
int firstMissingPositive(int A[], int n)
{
int i=;
while(i<n)
{
if(A[i] !=i+ &&A[i]>&&A[i]<=n&&A[A[i]-] !=A[i])
swap(A[i],A[A[i]-]);
else
++i;
}
for(i=;i<n;++i)
{
if(A[i] !=i+)
break;
}
return i+;
}
};

[Leetcode] first missing positve 缺失的第一个正数的更多相关文章

  1. LeetCode:缺失的第一个正数【41】

    LeetCode:缺失的第一个正数[41] 题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3示例 2: 输入: [3,4,-1,1] ...

  2. LeetCode缺失的第一个正数

    LeetCode 缺失的第一个正数 题目描述 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数. 进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗? ...

  3. Leetcode 41.缺失的第一个正数

    缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: ...

  4. Java实现 LeetCode 41 缺失的第一个正数

    41. 缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: ...

  5. LeetCode(41):缺失的第一个正数

    Hard! 题目描述: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输 ...

  6. LeetCode 41. 缺失的第一个正数(First Missing Positive)

    题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8 ...

  7. leetcode之缺失的第一个正数

    给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0]输出: 3示例 2: 输入: [3,4,-1,1]输出: 2示例 3: 输入: [7,8,9,11,12] ...

  8. [LeetCode] 268. Missing Number 缺失的数字

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

  9. [Swift]LeetCode41. 缺失的第一个正数 | First Missing Positive

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

随机推荐

  1. python计算MD5

    python有自带的MD5模块hashlib,用起来简单很多.Python Hashlib模块的使用说明 http://docs.python.org/2/library/hashlib.htmlfd ...

  2. 廖老师的Python教程——Python简介

    一直想了解下Python,今儿在外面办事排队的时候,打开了廖老师的官网,找到了Python教程.虽然只是一篇关于Python的简介,但是通过将Python的特性与C进行对比,很生动地归纳了Python ...

  3. 解决Pycharm无法使用已经安装Selenium的问题

    重要:参考资料 当前版本 python版本:2.7 pycharm: 2017 原来本机是已经安装了2.7和selenium,新安装了一个pycharm的ide,于是selenium总是安装报错.At ...

  4. Python Web开发中,WSGI协议的作用和实现原理详解

    首先理解下面三个概念: WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server ...

  5. ubuntu 14.04离线安装docker和docker compose

    准备安装包 1.下载docker docker支持以下版本的ubuntu系统Artful 17.10 (Docker CE 17.11 Edge)Zesty 17.04Xenial 16.04 (LT ...

  6. 用filter()筛选出素数

    'use strict'; function get_primes(arr) { return arr.filter(function isPrime(number) { if (typeof num ...

  7. HDU暑假多校第四场J-Let Sudoku Rotate

    一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...

  8. 44- EF + Identity实现

    1-配置EF, 需要创建如下几个类 默认User主键为guid类型,现在改成int类型 namespace MvcCookieAuthSample.Models { public class Appl ...

  9. torndb在python3中运用

    #连接数据库:db = torndb.Connect() #查询一条的数据get() #查询多行的数据query() #创建数据表,数据库execute() #插入一条数据:sql = "i ...

  10. c#根据ip获取城市地址

    用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...