(LeetCode 41)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.
题目:
给一无序数组,找到数组中从1开始第一个不出现的正整数。
要求O(n)的时间复杂度和常数空间复杂度。
思路:
1、方法一:
先排序,然后遍历数组,找出第一个不出现的正整数。但时间复杂度为O(nlogn),不符合要求。
实现如下:
#include <iostream>
#include <algorithm> using namespace std; int firstMissPositive(int A[],int n){
sort(A,A+n,less<int>());
int i=;
while(A[i]<=) i++;
int j=;
while(i<n){
if(i<n- && A[i]==A[i+]) i++;
if(A[i]!=j) break;
i++;
j++;
}
return j;
} int main()
{
int A[]={,,,-,-,,};
int n=sizeof(A)/sizeof(A[]);
cout<<firstMissPositive(A,n);
return ;
}
2、方法二:
对于正整数A[i],如果将它放在数组中满足A[i]=i+1的位置,那么如果当某个位置不满足A[i]==i+1时,则i为第一个不出现的正整数。
- 遍历数组,
- 当遇到小于n(n为数组大小)的正整数,如果它满足A[i]=i+1,则跳过,i++,如果不满足则将它交换它属于它的位置,即swap(A[i],A[A[i]-1]);
- 当遇到小于0或者大于n的数,或者需交换的位置已经有了满足条件的值即A[i]==A[A[i]-1](数组中有重复数字的时候会有这种情况),则跳过,i++,因为没有合适的位置可以跟它们交换。
- 再次遍历数组,如果A[i]!=i+1,则i为第一个不出现的正整数。
代码如下:
class Solution {
public:
void swap(int &a,int &b){
int tmp;
tmp=a;
a=b;
b=tmp;
}
int firstMissingPositive(vector<int>& nums) {
int n=nums.size();
// if(n==0) return 1;
int i=;
while(i<n){
if(nums[i]==i+ || nums[i]==nums[nums[i]-] || nums[i]<= || nums[i]>n)
i++;
else
swap(nums[i],nums[nums[i]-]);
}
for(i=;i<n;i++){
if(nums[i]!=i+)
break;
}
return i+;
}
};
(LeetCode 41)First Missing Positive的更多相关文章
- LeetCode(41)First Missing Positive
题目 Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...
- (Problem 41)Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
- 【LeetCode练习题】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- [LeetCode 题解]: First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode OJ:First Missing Positive (第一个丢失的正数)
在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...
- (LeetCode 78)SubSets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 《从零开始学Swift》学习笔记(Day 41)——类的继承
原创文章,欢迎转载.转载请注明:关东升的博客 Swift中的继承只能发生在类上,不能发生在枚举和结构体上.一个类可以继承另一个类的方法.属性.下标等特征,当一个类继承其他类时,继承类叫子类,被继承类叫 ...
随机推荐
- 【平面图最小割】BZOJ2007-[NOI2010]海拔
[题目大意] 城市被东西向和南北向的主干道划分为n×n个区域,包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向道路.现得到了每天每条道路两个方向的人流量.每一个交叉路口都有海拔,每向上爬h ...
- Elasticsearch中document的基础知识
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题
B. Bear and Finding Criminals 题目连接: http://www.codeforces.com/contest/680/problem/B Description Ther ...
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题
B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- Spotlight on linux 监控 linux服务器资源
步骤一:在window主机上安装spotlight 下载地址:http://worlddownloads.quest.com.edgesuite.net/Repository/www.quest.co ...
- 双频无线网安装设置(5g ) for linux
为了在局域网实现远程wifi调试,例如调试需要图像数据传输,则需要搭建局域网5g无线网络. 1.硬件要求 a. TP-Link(型号:TL-WDR6500,AC1300双频无线路由器,支持5g,2.4 ...
- oracle存储过程获取异常信息码和异常信息
oracle存储过程,可以通过sqlcode 获取异常编码.通过sqlerrm获取异常信息. 例子: create or replace procedure write2blob(p_id in nu ...
- mac 下 outlook 邮箱 服务器端口设置
- 算法:图(Graph)的遍历、最小生成树和拓扑排序
背景 不同的数据结构有不同的用途,像:数组.链表.队列.栈多数是用来做为基本的工具使用,二叉树多用来作为已排序元素列表的存储,B 树用在存储中,本文介绍的 Graph 多数是为了解决现实问题(说到底, ...