【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 ...
随机推荐
- Css常用收集
/*-------------------------------------- 圆角*/ -webkit-border-radius: 4px; -moz-border-radius: 4px; ...
- [CentOS]添加删除用户
摘要 在安装CentOS的时候,我们只设置了root,类似windows的超级管理员.当然我们在工作的时候,为了安全考虑,不可能对外开发root,一方面是从安全的角度,另一方面也是方便管理. 添加删除 ...
- JDBC:从数据库中取数据的一个bug
先看错误信息: java.sql.SQLException: Before start of result set at com.mysql.jdbc.SQLError.createSQLExcept ...
- web 开发前端学习
调试插件:http://www.getpostman.com/ http://bootstrap.evget.com/javascript.html bootstrap: http://www.bo ...
- XDU 1160 - 科协的数字游戏I
Problem 1160 - 科协的数字游戏I Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 184 ...
- python 中文编码问题
1.文件首#coding=utf-8 作用:为了把文件内容编码python所识别的utf-8,若不指定编码格式,则会出现以下错误: 2. 文件储存指定编码为utf-8 作用:将文件编码成utf-8.如 ...
- redis-string1
package com.ztest.redis.string; import com.sun.istack.internal.logging.Logger;import com.ztest.redis ...
- 中国天气预报数据API收集
{"weatherinfo":{"city":"北京","cityid":"101010100" ...
- mongodb certification
又偷懒 也有学到不少东西 这个东西算是小结啦 给2013年的碌碌无为挽回点面子 哈哈~
- maven引入jar包时,一个jar的引入错误,会导致后来的jar包的引入。
maven引入本jar包时,引入失败. 问题是另一个jar没有引入正确.