LeetCode 041 First Missing Positive
题目要求: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的更多相关文章
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- java查询elasticsearch聚合
java查es多分组聚合: SearchRequestBuilder requestBuilderOfLastMonth = transportClient.prepareSearch(TYPE_NA ...
- Django之富文本(获取内容,设置内容)
富文本 1.Rich Text Format(RTF) 微软开发的跨平台文档格式,大多数的文字处理软件都能读取和保存RTF文档,其实就是可以添加样式的文档,和HTML有很多相似的地方 图示 2.tin ...
- JS中使用for-each遍历数组
1 let array = [1, 3, 6, 8, 9, 0, 5]; 2 /* 3 index是数组索引 4 value代表数组的值 5 arr是指整个数组 6 */ 7 array.forEac ...
- springboot + post 中文乱码
去检查你的 filter 配置,是否配置了一个最高优先级的 filter, 这个最高优先级的 filter 会影响 springboot 自动配置的 CharacterEncodingFilter.原 ...
- [论文阅读] RNN 在阿里DIEN中的应用
[论文阅读] RNN 在阿里DIEN中的应用 0x00 摘要 本文基于阿里推荐DIEN代码,梳理了下RNN一些概念,以及TensorFlow中的部分源码.本博客旨在帮助小伙伴们详细了解每一步骤以及为什 ...
- c#练习习题:while循环
2006年培养学员80000人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人? int count = 80000; int year = 2006; while (count ...
- Cassandra数据模型和模式(Schema)的配置检查
免责声明 本文档提供了有关DataStax Enterprise(DSE)和Apache Cassandra的常规数据建模和架构配置建议.本文档需要DSE / Cassandra基本知识.它不能代替官 ...
- 1、线性DP 213. 打家劫舍 II
https://leetcode-cn.com/problems/house-robber-ii/ //rob 0, not rob n-1 || not rob 0,not rob n-1 ==&g ...
- 头秃了,二十三张图带你从源码了解Spring Boot 的启动流程~
持续原创输出,点击上方蓝字关注我 目录 前言 源码版本 从哪入手? 源码如何切分? 如何创建SpringApplication? 设置应用类型 设置初始化器(Initializer) 设置监听器(Li ...
- 通过ceph-deploy安装不同版本ceph
之前有在论坛写了怎么用 yum 安装 ceph,但是看到ceph社区的群里还是有人经常用 ceph-deploy 进行安装,然后会出现各种不可控的情况,虽然不建议用ceph-deploy安装,但是既然 ...