First Missing Positive

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.

通过swap操作,把各个元素swap到相应的位置上,然后再扫描一遍数组,确定丢失的正数的位置
 
 class Solution {
public:
int firstMissingPositive(int A[], int n) { if(n==) return ; for(int i=;i<n;i++)
{
if(A[i]!=i+&&A[i]<=n&&A[i]>=&&A[i]!=A[A[i]-])
{
swap(A[i],A[A[i]-]);
i--;
}
} for(int i=;i<n;i++)
{
if(A[i]!=i+) return i+; }
return n+; } void swap(int &a,int &b)
{
int tmp=a;
a=b;
b=tmp;
}
};

【leetcode】First Missing Positive的更多相关文章

  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(hard) ☆

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  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. fis总结

    1.fis捕获组 $1.$2.$3……是正则表达式替换中对捕获组的引用 $0或$&是对整个匹配字符串的引用 2.fis命令 fis3 release -d ../output prod -wL ...

  2. Mongodb3.0.6副本集+分片学习笔记

    一.使用问题记录 1. mongodb3.0.6使用mongostat参数 >./mongostat -h 127.0.0.1:27017 -u root -p 123456 /authenti ...

  3. 正确运用synchronized和二次判断 实现多线程安全

    正确运用synchronized和二次判断 实现多线程安全,做出高效二符合预期的程序,特别是多个线程跑一个对象的时候,如下图所示:  测试代码如下: 特别注意if(shutdownRequested) ...

  4. 一款符合当前主流审美的Swing外观(Look and Feel)_测试版发布

    [前言] 本文将展示的是一款J2SE平台Swing外观(Look and Feel)实现,目前给出的演示jar包仅供测试之用,主体工作已经完成,余下是兼容性测试和调整,附件中的演示jar包推荐运行于j ...

  5. C#~异步编程续~.net4.5主推的await&async应用(转)

    之前写过两篇关于异步编程的文章,详细可以进入C#~异步编程和C#~异步编程在项目中的使用 .net的各个版本都有自己主推的技术,像.NET1.1中的委托,.NET2.0中的泛型,.NET3.0中的Li ...

  6. django book

    一.安装配置 1.下载地址: https://www.djangoproject.com/download/ 2.安装: tar zxvf Django-1.6.1.tar.gz && ...

  7. CSS-animations和transitions性能:浏览器到底做了什么?

    CSS animations 和 transitions 的性能:浏览器到底做了什么?(译) 原文地址:http://blogs.adobe.com/webplatform/2014/03/18/cs ...

  8. jQuery 请指出'$'和'$.fn'的区别?或者说出'$.fn'的用途。

    ---------------------------------------------------------------------------------- 我们先把jQuery看成了一个类, ...

  9. sql批量更换dedecms文章来源和作者

    前面写了一篇修改dedecms默认文章来源 "未知"改为关键词,那个是修改以后发布的文章“来源”才会变成自己设定的关键词,如果修改之前已经有很多文章了,那些文章“来源”还是显示“未 ...

  10. nginx反向代理proxy模块相关参数

    http_proxy_module Proxy_pass proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器:官方说明:http://ngi ...