41. First Missing Positive (sort) O(n) time
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
solution: compare the element with index + 1, if not equal, swap them.
class Solution {
public int firstMissingPositive(int[] nums) {
//preprocess the string
//deal with duplicate elements--two pointers
//sorted to remove
for(int i = ; i<nums.length; i++){
//check nums[i] != i+1
while(nums[i] > && nums[i] <= nums.length && nums[i] != nums[nums[i] -]){
//swap them
/*int temp = nums[i];
nums[i] = nums[nums[i] - 1];
nums[temp - 1] = temp;*/
int temp = nums[nums[i] - ];
if(temp == nums[i]) break; //check the duplicate elemnt
nums[nums[i] - ] = nums[i];
nums[i] = temp;
}
}
for(int i = ; i<nums.length; i++){
if(nums[i] <= || nums[i] != i+)
return i+;
}
return nums.length + ;
}
}
Easily, to solve this problem, we can use an extra space to solve this, like a hash set or array.
good reference: https://blog.csdn.net/SunnyYoona/article/details/42683405
41. First Missing Positive (sort) O(n) time的更多相关文章
- 刷题41. First Missing Positive
一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对 ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- 41. First Missing Positive(困难, 用到 counting sort 方法)
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- 41. First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 【一天一道LeetCode】#41. First Missing Positive
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- 【LeetCode】41. First Missing Positive (3 solutions)
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
随机推荐
- java.net.SocketException四大异常解决方案---转
java.net.SocketException如何才能更好的使用呢?这个就需要我们先要了解有关这个语言的相关问题.希望大家有所帮助.那么我们就来看看有关java.net.SocketExceptio ...
- jmeter(5)——参数化
之前接触过QTP或者Loadrunner的小伙伴,应该对参数化不陌生,在<badboy详解篇>中也介绍了badboy的参数化,今天说一下jmeter的参数化,同样,我们举例说明,以msn. ...
- Redis学习笔记--常用命令
以下为本人学习Redis的备忘录,记录了大部分常用命令 1.客户端连接redis服务端: ===启动Redis服务端 redis-server /yourpath/redis.conf ===启动Re ...
- 记一次走心One 2 One沟通
聊的比较零零碎碎,内容比较散,有些solution不错,记一些要点吧(1)要学会汇报,就是坐你身边的人,也未必知道你在干啥 三个人都在砌墙.当人们分别问他们在做什么,他们的答案却不一样:第一个人头也没 ...
- 深入理解JavaScript系列(45):代码复用模式(避免篇)
介绍 任何编程都提出代码复用,否则话每次开发一个新程序或者写一个新功能都要全新编写的话,那就歇菜了,但是代码复用也是有好要坏,接下来的两篇文章我们将针对代码复用来进行讨论,第一篇文避免篇,指的是要尽量 ...
- JavaScript使用Object.defineProperty方法实现双数据绑定
Object.defineProperty这个方法非常值得学习,很多mvc框架中的双向数据绑定就是通过它来实现的. 本着互联网分享精神,今天我就将我自己的见解分享给大家,希望能有所帮助. 开始使用 O ...
- (转)The remote certificate is invalid according to the validation procedure
If you get “The remote certificate is invalid according to the validation procedure” exception while ...
- deblurGAN
-- main.py -- util.py -- data_loader.py -- mode.py -- DeblurGAN.py -- vgg19.py -- layer.py -- vgg19. ...
- Java - 避免使用finalizer
Finalizers are unpredictable ,often dangerous ,and generally unnecessary. 在Java中,GC会自动回收不可达对象相关的空间,而 ...
- c#单例(Singleton)模式实现
sealed class Singleton { private Singleton(); public static readonly Singleton Instance=new Singleto ...