【leetcode】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) { 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的更多相关文章
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode】163. Missing Range
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 【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 ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【leetcode】1228.Missing Number In Arithmetic Progression
题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...
随机推荐
- 常用JS效果 不断进步贴 不停更新~ 纪念用~
常用效果 JS 都是Jquery 没有特殊说明 1.选项卡 用的JQuery 以后学好点再来对比 看下 /* * @parent 最外层父级元素 * @EventElement 触发事件元素 ...
- salt stack 工具之一——远程命令
salt stack 远程命令 salt stack是一种自动化的运维工具,可以同时对N台服务器进行配置管理.远程命令执行等操作. salt stack分为两个部分: salt-master,部署在控 ...
- select2搜索框查询加遍历
<div class="form-group"> <label class="control-label col-sm-1 no-padding-rig ...
- linq/EF/lambda 比较字符串日期时间大小
在使用EF时,想要比较字符串类型的日期时,参考以下: SQL语句: 1 2 3 4 1)select * from TableName where StartTime > '2015-04-08 ...
- Linux 运行 apt-get install 就出现jdk installer 错误的解决方法
解决办法如下: sudo rm /var/lib/dpkg/info/oracle-java7-installer* sudo apt-get purge oracle-java7-installer ...
- c# 字符串前加@
@在c#中为强制不转义的符号,在里面的转义字符无效. 例如:Console.WriteLine("你好\t吗?"); Console.WriteLine(@"你好\t吗& ...
- 清北暑假模拟day1 艳阳天
/* 注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数 */ #include <cstdio> #include <algorith ...
- YC大牛的判题任务-想法
YC大牛的判题任务 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class ...
- All Kind Of Conference(随时更新...)
收集一些前端开发的各种会议,里面有视频或者PPT,随时查看都还是很有收获的.还有要向这些演讲的前辈看齐- AC 2015:http://ac.alloyteam.com/2015/ AC 2016:h ...
- [POJ1151]Atlantis
[POJ1151]Atlantis 试题描述 There are several ancient Greek texts that contain descriptions of the fabled ...