题目要求: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.

代码如下:

class Solution {
public:
int firstMissingPositive(int A[], int n) { for (int i = 0, temp = -1; i < n; i++) {
while (A[i] - 1 >= 0 && A[i] - 1 < n && A[i] - 1 != i) {
swap(A, i, A[i] - 1);
if (A[i] == temp) {
break;
}
else {
temp = A[i];
}
}
}
for (int i = 0; i < n; i++) {
if (A[i] - 1 != i) {
return i + 1;
}
}
return n + 1;
} private:
void swap(int A[], int idx1, int idx2) {
A[idx1] ^= A[idx2];
A[idx2] ^= A[idx1];
A[idx1] ^= A[idx2];
}
};

LeetCode 041 First Missing Positive的更多相关文章

  1. Java for LeetCode 041 First Missing Positive

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

  2. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  3. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  4. leetcode 41 First Missing Positive ---java

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

  5. [LeetCode] 41. First Missing Positive 首个缺失的正数

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

  6. 【leetcode】First Missing Positive

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

  7. 【leetcode】First Missing Positive(hard) ☆

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

  8. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  9. LeetCode题解-----First Missing Positive

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

随机推荐

  1. AMA指标原作者Perry Kaufman 100+套交易策略源码分享

    更多精彩内容,欢迎关注公众号:数量技术宅.想要获取本期分享的完整策略代码,请加技术宅微信:sljsz01 AMA技术指标与原作者 Kaufman 说起 Perry Kaufman 这个名字,不少读者会 ...

  2. Unity正交相机智能包围物体(组)方案

    Unity正交相机智能包围物体(组)方案 目录 Unity正交相机智能包围物体(组)方案 一.技术背景 二.相关概念 2.1 正交摄像机 2.2 正交相机的Size 2.3 相机的Aspect 2.4 ...

  3. HTTPDNS开源 Android SDK,赋能更多开发者参与共建

    为赋能更多开发者参与共建,阿里云HTTPDNS开源 Android SDK,iOS SDK也在做开源准备,不久也将开放给开发者.HTTPDNS是阿里云移动研发平台面向多端应用(移动端APP,PC客户端 ...

  4. 下载eclipse及其插件

    1.安装JDK 2.配置JAVA_HOME 3.具体下载地址 (1)JDK的下载和安装 jdk官网 http://www.oracle.com/technetwork/java/javase/down ...

  5. Windows平台Python Pyramid实战从入门到进阶:第一个服务

    Pyramid是比较流行的Python Web 框架,比较灵活,功能也很强大.最近项目上用到,便打算学习一下.网上教程比较少,而且很多都是针对linux平台的,我是windows土著所以对那些linu ...

  6. Node.js 搞Javascript开发的无论如何要尝试一下

    我想找个因子给大家介绍Node.js 这样吧,我想Jquery的占有率那么高,就拿Jquery来说吧. https://github.com/jquery/jquery 首先打开Jquery的gith ...

  7. Cacti如何实现电话告警

    Cacti是一套基于PHP,MySQL,SNMP及RRD Tool开发的网络流量监测图形分析工具.Cacti提供了一个快速轮询器,高级图表模板,多种数据采集方法和用户管理功能.所有这一切都被包装在一个 ...

  8. 极客mysql13

    1:为啥删除了表的一半数8据,表文文件大小没变化?因为delete 命令其实只是把记录的位置,或者数据页标记为了"可复用",但磁盘文件的大小是不会变的.也可以认为是一种逻辑删除,所 ...

  9. 自适应哈希索引(Adaptive Hash Index, AHI) 转

    Adaptive Hash Index, AHI 场景 比如我们每次从辅助索引查询到对应记录的主键,然后还要用主键作为search key去搜索主键B+tree才能找到记录. 当这种搜索变多了,inn ...

  10. rpm命令介绍

    rpm安装不能指定安装位置. 查看系统安装了哪些软件:rpm -qa  rpm -qa |grep keyword      (q:query  a 是all)   查看软件是否安装: rpm -q  ...