(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中的继承只能发生在类上,不能发生在枚举和结构体上.一个类可以继承另一个类的方法.属性.下标等特征,当一个类继承其他类时,继承类叫子类,被继承类叫 ...
随机推荐
- [BZOJ1815&BZOJ1488]有色图/图的同构(Polya定理)
由于有很多本质相同的重复置换,我们先枚举各种长度的点循环分别有多少个,这个暴搜的复杂度不大,n=53时也只有3e5左右.对于每种搜索方案可以轻易求出它所代表的置换具体有多少个. 但我们搜索的是点置换组 ...
- php 获取所有常量
有的时候想得到某个完整路径,看看都定义了哪些常量,可以这样做,即把所有的常量都打印出来,然后看看有没有自己想要的,感觉挺方便 官方给的原型: array get_defined_constants ( ...
- ant design Modal遮罩层颜色加深 解决方案
1.原因 页面中存在多个Modal同时渲染及弹出(在table里使用Modal就会出现这种问题) 2.解决方案 不让多个Modal同时渲染就行了,设置Modal的visible属性为this.stat ...
- Android 通话记录分析
http://stackoverflow.com/questions/6786666/how-do-i-access-call-log-for-android http://android2011de ...
- 完整的POM文档内容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- MYSQL 源码- 淘宝数据库研发组
http://mysql.taobao.org/index.php?title=%E8%B5%84%E6%96%99%E5%85%B1%E4%BA%AB
- Material Design(原质化设计)视觉设计语言规范 踏得网镜像
Android 5.0 Lollipop(棒棒糖,也就是之前的代称Android L)全面实践了谷歌最新研发的 Material Design 设计语言规范,只是该设计规范并不是仅针对移动平台. 我们 ...
- StringBuilder和StringBuffer解析(百度面试题优化须要用到的)
StringBuilder是java5及以后提供的API,它不是线程安全的,而StringBuffer是java1.4曾经的API,它是线程安全的,所以说StringBuilder的效率更高一些,今天 ...
- 如何使用C#关键字const,readonly,static
如果有一个值不太会变化,我们经常使用const和readonly,这2者有何不同呢?有时候,我们也会在readonly之前加上关键字static,这又意味着什么呢? const ● const默认是静 ...
- 演示Microsoft Advertising SDK for Windows Phone 8.1
演示Microsoft Advertising SDK for Windows Phone 8.1,Only for Windows Phone 8.1 1.在References上点右键,添加引用, ...