(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中的继承只能发生在类上,不能发生在枚举和结构体上.一个类可以继承另一个类的方法.属性.下标等特征,当一个类继承其他类时,继承类叫子类,被继承类叫 ...
随机推荐
- BZOJ.2339.[HNOI2011]卡农(思路 DP 组合 容斥)
题目链接 \(Description\) 有\(n\)个数,用其中的某些数构成集合,求构造出\(m\)个互不相同且非空的集合(\(m\)个集合无序),并满足每个数总共出现的次数为偶数的方案数. \(S ...
- Apache之.htaccess备忘录(二)
博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助的可以关注我. 转载请注明"深蓝的镰刀" 书接上回,<Apache之.htaccess备忘录(一)& ...
- VC 调用 Python
//file:py.h BOOL InitPython(); BOOL ClosePython(); ======================== //file:py.cpp #include & ...
- python开发_imghdr_图像格式支持
在python中,imghdr模块对图像格式提供了支持 该模块主要是处理识别图像的格式 imghdr模块提供的函数如下: imghdr.what(filename, h=None) Tests the ...
- jmeter用BeanShell调用jar包对HTTP请求中的参数进行MD5加密
前提: eclipse.JDK.Jmeter 说明: 本文分为两部分进行配置说明 第一部分:编写JavaMD5加密脚本 第二部分:使用Jmeter的BeanShell进行验证 ************ ...
- 重温PHP之快速排序
基本原理:选出当前数组中任一元素(通常为第一个)作为标准,新建两个空数组分别置于当前数组前后,然后遍历当前数组,如果数组中元素值小于等于第一个元素值就放到前边空数组,否则放到后边空数组. //快速排序 ...
- 安装oracle 11gr2 rac on solaris
http://candon123.blog.51cto.com/704299/389470
- Iptables 指南 1.1.19
Iptables 指南 1.1.19 Oskar Andreasson oan@frozentux.net Copyright © 2001-2003 by Oskar Andreasson 本文在符 ...
- 写给在Java和.net中徘徊的新手
在很多网站上,网友都会问一个相同的问题,到底是学Java还是.net,个有个的见解. 自从.Net问世以来,程序员都很关心的一个问题是「该学Java或.NET」.我也在挣扎,该「该继续Java的研究, ...
- 怎样用Java代码来把SSL的证书自己主动导入到Java的秘钥存储文件(keystore)
我们在开发或者使用SSL的过程中,非常多的软件须要我们提供java的keystore.特别是一些基于Java的中间件产品. 我们常规的做法是JDK自带的工具命令(keytool)去做.比方,以下的样例 ...