题目要求: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. python数据分析使用matplotlib绘图

    matplotlib绘图 关注公众号"轻松学编程"了解更多. Series和DataFrame都有一个用于生成各类图表的plot方法.默认情况下,它们所生成的是线形图 %matpl ...

  2. GXOI2018 滚粗记

    今天考了一次蜜汁省选,滚粗了.想了想,还是写点什么记录一下 8:10 折腾一番总算拿到题目和样例了,一打开dpf,立马感觉到了不对劲. 题目一股浓浓的劣质模拟题的画风,先不说题目质量,单是排版质量都被 ...

  3. QQ 邮箱日历提醒

    偶然发现 QQ 邮箱有日历的功能,而且可以设置农历并且每年邮件 + 短信 + 微信提醒.这下重要的日子(eg:生日...)就不会忘记啦! 1.找到日历 2.历史提醒 3.新建时间 4.设置时间 5.勾 ...

  4. GDB调试基础使用方法

    尽管目前使用的VS code可以使用插件一键构建和运行程序,但GDB作为调试利器,还是值得花时间去学习的. 概述 GDB(GNU Debugger) 是一个由GNU开源组织发布的.UNIX/LINUX ...

  5. JavaMail 发送邮件出现 Connection reset 问题

    问题描述 使用 java mail 发送邮件的时候,申请的 163 邮箱作为发件箱,然无论如何配置,均出现 Connection reset,无法正常发送邮件. Exception in thread ...

  6. 常用简单电脑bai快捷键大全

    Ctrl+C 复制.duCtrl+X 剪切.Ctrl+V粘贴.Ctrl+Z撤销.Ctrl+A全选所有文件.zhiDelete删除.daoShift+Delete避开回收站直接永久删除(不可找回).F3 ...

  7. Facebook 的神仙组长什么样?

    这里是<齐姐聊大厂>系列的第 14 篇 每周五早上 8 点,与你唠唠大厂的那些事 号外号外!前 12 篇已出 PDF:公粽号后台回复「大厂」即可获得! ❝ 小齐说: 这篇文章是来自阿米粥的 ...

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

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

  9. 剑指offer刷题(栈、堆、 队列、 图)

    Stack & Queue 005-用两个栈实现队列 题目描述 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数 push 和 pop ,分别完成在队列尾部插入整数和在队列头部删除整数 ...

  10. 关于android.view.InflateException【转载】

    在AndroidStudio中编译没有问题,但是运行时会crash,常发生于自定义View的引用.出现问题的原因大致分为以下几种 1.引用View的路径问题:如果自定义的view为CustomerVi ...