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 ---------------------------------------------------------- ...
随机推荐
- array,vertor,arraylist,hashable,hashmap等几个易混淆概念的区别
Array可以存放Object和基本数据类型,但创建时必须指定数组的大小,并不能再改变, Vertor是放的Object. Vertor一维,Hashmap/Hashtabe二维: Vertor/Ar ...
- C#给文件夹添加权限
//==== //添加权限 private void SetAttributes(string folder) { if (folder == "" || !Directory.E ...
- No message found under code ' for locale 'en'.
1.如果你使用eclipse创建的工程是class和src分开的,那么资源属性文件一定要放在src目录以内.2.属性文件名的写法:messages_zh_CN.properties (中文)messa ...
- 【poj1007】 DNA Sorting
http://poj.org/problem?id=1007 (题目链接) 题意 给出m个字符串,将其按照逆序对个数递增输出. Solution 树状数组经典应用. 代码 // poj1007 #in ...
- view的绘制原理
转:http://blog.csdn.net/berber78/article/details/42069301 自定义UI控件,需继承 View类或View的子类,并重载View类中的一些方法,不必 ...
- Jquery EasyUI的添加,修改,删除,查询等基本操作介绍
http://www.jb51.net/article/42016.htm 初识Jquery EasyUI看了一些博主用其开发出来的项目,页面很炫,感觉功能挺强大,效果也挺不错,最近一直想系统学习一套 ...
- Linux的学习路线图
一.学习Linux的基本要求1. 掌握至少50个以上的常用命令. 2. 熟悉Gnome/KDE等X-windows桌面环境操作 . 3. 掌握.tgz..rpm等软件包的常用安装方法 4. 学习添加外 ...
- RSA算法小记
学习来源:http://www.cnblogs.com/vamei/p/3480994.html 小记: 一.数学基础: 欧拉Phi函数:Φ(n)=总数(从1到n-1中与n互质的整数) (1)欧拉定理 ...
- Java及Android开发环境搭建
前言 自从接触java以来,配置环境变量折腾了好几次,也几次被搞得晕头转向,后来常常是上网查阅相关资料才解决.但是过一段时间后一些细节就会记不清了,当要在其他机子上配置时又得上网查或者查阅相关书籍,如 ...
- Practical Machine Learning For The Uninitiated
Practical Machine Learning For The Uninitiated Last fall when I took on ShippingEasy's machine learn ...