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 ...
随机推荐
- 在Eclipse添加Android兼容包( v4、v7 appcompat )[转]
昨天添加Android兼容包,碰到了很多问题,在这里记录一下,让后面的路好走. 如何选择兼容包, 请参考Android Support Library Features(二) 一.下载Support ...
- 【Lua】关于遍历指定路径下所有目录及文件
关于Lua中如何遍历指定文件路径下的所有文件,需要用到Lua的lfs库. 首先创建一个temp.lua文件,用编辑器打开: 要使用lfs库,首先需要把lfs库加载进来 require("lf ...
- CentOS7下Django安装
Django安装介绍 安装环境: CentOS7 安装Django比较简单,但需要安装其依赖的东西,还是需要一定时间的.我使用的环境是CentOS Linux release 7.3.1611. 内核 ...
- Vim常用插件——前端开发工具系列
作为一名开发者,应该对编辑器之神Vim与神之编辑器Emacs有所耳闻吧.编辑器之战的具体细节有兴趣的童鞋可以google之. Vim最大的特点是打开速度快,功能强大,一旦掌握了其中的命令,编程过程双手 ...
- 在ASPNETCORE中获得所有Action
在ASPNETCORE中获得所有Action 本文旨在记录自己在aspnetcore工作中需要获取所有Action,在查询了资料后进行了几种方法的记录.后期有发现其它方式再进行追加. 一.通过 反射 ...
- 资料汇总--Web前端
01.前端技能汇总 02.gitHub优秀前端资料分享 03.大前端 HTML Doctype作用?严格模式与混杂模式如何区分?它们有何意义? 1. <!DOCTYPE> 声明位于文档中的 ...
- WEB下渗透测试经验技巧(全)[转载]
Nuclear’Atk 整理的: 上传漏洞拿shell: 1.直接上传asp.asa.jsp.cer.php.aspx.htr.cdx….之类的马,拿到shell.2.就是在上传时在后缀后面加空格或者 ...
- Firebird 用查询结果集更新数据,merge
Merge语法: MERGE INTO target [[AS] target-alias ] USING source [[AS] source-alias ] ON join-condition ...
- python监控linux内存并写入mongodb
(需要安装psutil 用来获取服务器资源,以及pymongo驱动)#pip install psutil #pip install pymongo #vim memory_monitory.py 文 ...
- Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
环境 Android Studio 3.0 升级&导入项目 错误 Error:java.util.concurrent.ExecutionException: com.android.tool ...