题目要求: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. NB-IOT基站的优势和特点

    NB-IOT基站是什么        NB-IOT基站的主要目的是完成移动通信网和UE之间的通信和管理功能,在移动通信中是组成蜂窝小区最基本的单元.只有在基站信号的覆盖范围之内通过运营商网络连接的NB ...

  2. bat批处理常用脚本

    windows下有很多场景需要编写批处理来解决问题,跟定时任务相结合使用更佳. 1.创建文件,md,mkdir都可以进行文件创建 set AwrPath=D:\OracleTabChk if not ...

  3. windows 查看内存

    MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatusEx (&statex); _tprin ...

  4. http twisted

    Sunday, September 30th, 2007 Twisted的WEB开发 作者: gashero <harry.python@gmail.com> 目录 1   简介 2    ...

  5. dubbo2.7.X版本带来的服务注册和服务调用方式改变

    参考地址:https://www.cnblogs.com/alisystemsoftware/p/13064620.html 注册中心数据结构格式改变(service:接口服务,application ...

  6. find命令的简单使用

    Find命令 格式:find [option] [Path] [筛选条件] [处理动作] Path:默认当前目录 筛选条件:对文件/目录设置筛选条件 处理动作:默认显示所有文件 筛选条件: -name ...

  7. Mybatis 批量更新遇到的小问题

    小问题 记一个开发过程中因为小细节的遗漏而引发的 "莫名其妙",公司中有个2B(to B)供应链项目,持久层用的是 JPA,这里我就不吐槽 JPA 了,这种 SQL 嵌入在代码里的 ...

  8. [MIT6.006] 13. Breadth-First Search (BFS) 广度优先搜索

    一.图 在正式进入广度优先搜索的学习前,先了解下图: 图分为有向图和无向图,由点vertices和边edges构成.图有很多应用,例如:网页爬取,社交网络,网络传播,垃圾回收,模型检查,数学推断检查和 ...

  9. 【JVM第四篇--运行时数据区】堆

    写在前面的话:本文是在观看尚硅谷JVM教程后,整理的学习笔记.其观看地址如下:尚硅谷2020最新版宋红康JVM教程 一.堆的概述 JVM的运行时数据区如下: 一个Java程序运行起来对应着一个进程(操 ...

  10. 重点思维导图------redis深度历险