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 ...
随机推荐
- 【随笔】关于绝对定位absolute相对于父元素定位的问题
绝对定位absolute的官方定义: 设置为绝对定位的元素框从文档流完全删除,并相对于其包含块定位,包含块可能是文档中的另一个元素或者是初始包含块.元素原先在正常文档流中所占的空间会关闭,就好像该元素 ...
- MySql的数据查询
SELECT语句是最常用的查询语句,它的使用方式有些复杂,但功能却相当强大.SELECT语句的基本语法如下: select selection_list//要查询的内容,选择哪些列 from数据表名/ ...
- 修改MyEclipse8.5的workspaces
到MyEclipse8.5的安装目录下 我安装在D盘,路径为:D:\Genuitec\MyEclipse 8.5\configuration\config.ini 打开config.ini文件: ...
- React项目的打包
1.create-react-app 来自Facebook官方的零配置命令行工具.create-react-app是来自于Facebook出品的零配置命令行工具,能够帮你自动创建基于Webpack+E ...
- vmstat命令——监控给定时间间隔的服务器的状态值
vmstat n m 时间间隔为n秒,采集m组数据vmstat n 时间间隔为n秒 # vmstat 2 3 procs -----------memory---------- ---swap ...
- 关于vue的常识问题及解决方法
一.VSCode开发必备插件 1.Beautify:语法高亮: 2.Bracket Pair Colorizer :对括号对进行着色: 3.ESLint:ESLint插件,高亮提示: 4.HTML C ...
- Splunk和ELK深度对比
转自:http://blog.51cto.com/splunkchina/1948105 日志处理两大生态Splunk和ELK深度对比 heijunmasd 0人评论 5312人阅读 2017-07- ...
- 另一个C#模拟post请求的例子
private string returninstallTmnl(AddTmnlInstallParameter model) { string url = ConfigurationSettings ...
- Mysql插入Emoji表情出错
Caused by: java.sql.SQLException: Incorrect at com.mysql.jdbc.SQLError.createSQLException(SQLError.j ...
- Node.js学习笔记(二) --- CommonJs和Nodejs 中自定义模块
一. 什么是 CommonJs? JavaScript 是一个强大面向对象语言,它有很多快速高效的解释器. 然而, JavaScript标准定义的 API 是为了构建基于浏览器的应用程序.并没有制定一 ...