(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中的继承只能发生在类上,不能发生在枚举和结构体上.一个类可以继承另一个类的方法.属性.下标等特征,当一个类继承其他类时,继承类叫子类,被继承类叫 ...
随机推荐
- angularJS简介及其特点—— 五大特性,加快 Web 应用开发
AngularJS 是谷歌的一个 JavaScript 框架,旨在简化前端应用程序的开发. 一. 关于和jquery的比较 首先angular是一个mvc框架,它与jquery不同之处在于,前者致力于 ...
- Zookeeper的基本操作
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- 参考内容: <私塾 ...
- Educational Codeforces Round 13 A. Johny Likes Numbers 水题
A. Johny Likes Numbers 题目连接: http://www.codeforces.com/contest/678/problem/A Description Johny likes ...
- 解决IE11下载文件 文件名乱码问题
1.Win + R输入gpedit.msc打开组策略编辑器:(不会请看下图) 2.定位到计算机配置→管理模板→windows组件→Internet Explorer→自定义用户代理字符串(有些系统用的 ...
- 3.3V与5V电平双向转换
http://www.amobbs.com/thread-5541344-1-2.html 3.3V转5V:S3输入为0V时,NMOS管导通,S5=S3=0V:S3输入为3.3V时,NMOS管截止,S ...
- MYSQL 源代码学习
http://ourmysql.com/ http://blog.chinaunix.net/uid-26896862-id-4009777.html
- [Asp.net MVC]Bundle合并,压缩js、css文件
摘要 在web优化中有一种手段,压缩js,css文件,减少文件大小,合并js,css文件减少请求次数.asp.net mvc中为我们提供一种使用c#代码压缩合并js和css这类静态文件的方法. 一个例 ...
- 关于OPC Client 编写2
最近在搞到一个OPC动态库OPCAutomation.dll,该动态库在http://www.kepware.com/可下载,下面介绍如何用C#进行OPC Client开发. 1.新建C#应用程序,命 ...
- C#(静态String类)
[转]http://blog.csdn.net/angelazy/article/details/8501776 C#中提供了比较全面的字符串处理方法,很多函数都进行了封装为我们的编程工作提供了很大的 ...
- 为免费app嵌入Admob广告
为免费app嵌入Admob广告,进而获得广告收入. 1.http://www.admob.com/注册一个帐号, 添加Add Mobile Site/app,输入相关信息后,提交完成, 下载Andro ...