Java for LeetCode 041 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.
解题思路一:
刚看到题目的时候感觉无从下手,后来仔细理解题意,需要找到first missing positive integer,这个数肯定是1---nums.length+1的一个值,因此我们可以开一个布尔数组,存储i是否存在,JAVA实现如下:
public int firstMissingPositive(int[] nums) {
boolean[] num=new boolean[nums.length];
for (int i = 0; i < nums.length; i++)
if (nums[i] > 0&&nums[i] <= nums.length)
num[nums[i]-1] = true;
for (int i = 0; i < num.length; i++)
if (!num[i])
return i+1;
return nums.length+1;
}
解题思路二:使用bool数组会有O(N)的空间复杂度,直接采用交换的方法可以避免,JAVA实现如下:
static public int firstMissingPositive(int[] nums) {
if(nums.length==0)
return 1;
for (int i = 0; i < nums.length; ) {
if (nums[i] >= 0 &&nums[i] < nums.length &&nums[i] != i && nums[i] != nums[nums[i]]){
int temp=nums[i];
nums[i]=nums[nums[i]];
nums[temp]=temp;
}
else i++;
}
for (int i = 1; i < nums.length; ++i)
if (nums[i] != i)
return i;
return nums[0] == nums.length ? nums.length + 1 : nums.length;
}
Java for LeetCode 041 First Missing Positive的更多相关文章
- LeetCode 041 First Missing Positive
题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
随机推荐
- Day1 login
使用流程: 1.程序启动后,显示欢迎信息,提示用户输入用户名: 2.判断用户是否存在,不存在则提示重新输入,或者关闭程序:客户存在则提示客户输入密码: 3.判断密码是否正确,如果不正确则提示用户重新输 ...
- bzoj3037 创世纪
两种解法: 一.树状DP /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring&g ...
- mysql 存储过程中注意的问题
OPEN cur; REPEAT FETCH cur INTO int_type, int_element_id, int_num, int_user_id; IF NOT _DONE THEN IF ...
- configure: error: Please reinstall the libcurl distribution
configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/ 基本上 ...
- 织梦DedeCms调用全站相关文章方法
织梦DedeCms 有个标签可以调用相关文章,通过下面的修改可以调用全站的相关文章,文章页内显示相关文章内容,可以提高关键词密度,还是挺不错的. 模板调用代码 <div> < ...
- WebBrowser 禁用右键
禁用错误脚本提示 将 WebBrowser控件的 ScriptErrorsSuppressed 设为 true 禁用右键菜单 将 WebBrowser 的 IsWebBrowserContextMen ...
- Jetty和tomcat的比较
转自: http://blog.csdn.net/classicbear/article/details/6968930 相同点: 1. Tomcat和Jetty都是一种Servlet引擎, ...
- Milking Cows
Milking Cows Three farmers rise at 5 am each morning and head for the barn to milk three cows. The f ...
- poj1012.Joseph(数学推论)
Joseph Time Limit: 1 Sec Memory Limit: 64 MB Submit: 493 Solved: 311 Description The Joseph's prob ...
- cocos基础教程(2)Window环境下搭建
第一步:开始安装VS2012 第二步:下载Cocos2d-x 3.4源码 配置环境变量 COCOS_CONTROL = E:\cocos2d-x-3.4\tools\cocos2d-console ...