leetcode 41 First Missing Positive ---java
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.
这道题求的是在一个乱序数组中缺少的第一个正数,要求时间复杂度o(n)空间复杂度为1。
先上代码
package leetcode;
public class firstMissingPositive {
public int firstMissingPositive(int[] nums) {
int len = nums.length;
int i = 0;
while( i<len){
if( nums[i] == i+1 || nums[i]<0 || nums[i] > len+1)
i++;
else if( nums[i] != nums[nums[i]-1])
swap(nums,i,nums[i]-1);
else
i++;
}
i = 0;
while(i<len && nums[i] == i+1)
i++;
return i+1;
}
public void swap(int[] nums, int a,int b){
int num = nums[a];
nums[a] = nums[b];
nums[b] = num;
}
/*
* 1.求第一个缺少的正数,想通思路很简单。
*/
}
这道题虽然是hard,难点就是在于时间和空间复杂度,但是算法并不困难。
就是将每一个在1-len之间的正数放在num[i]上,然后遍历一次,遇到的第一个不一样的数就是结果。
leetcode 41 First Missing Positive ---java的更多相关文章
- [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 Problem's Link ---------------------------------------------------------- ...
- 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]41. First Missing Positive第一个未出现的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo ...
- 41. First Missing Positive (JAVA)
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)
题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description 给出一个未排序的数组,求出第一个丢失的正数. ...
随机推荐
- 算法导论-钢条切割 C# 递归实现
下班前看到有位兄弟写 钢条切割问题,尝试实现C#版, 还没有实现最优版,分享一下. int[] parr; private void button1_Click(object sender, Even ...
- C# 小规模查找集合性能测试
项目中包含浮点运算,大概每秒 20 - 100 万左右. 其计算结果每秒只包含1000个左右. 因此大量运算是重复性的.程序运行时,cpu 在 3% - 10% 浮动.打算将结果缓存.根据键值索值. ...
- poj1274 二分匹配
今天复习二分匹配,A 了一道模板题. 二分匹配需要理解增广路的寻找.用dfs来更新最大匹配.注意一些点:赋初值:愚蠢地把==写成了= ; 然后match的记值;每个点都要重新走一遍. #include ...
- 蓝桥杯 ALGO-108 最大体积 (动态规划)
问题描述 每个物品有一定的体积(废话),不同的物品组 合,装入背包会战用一定的总体积.假如每个物品有无限件可用,那么有些体积是永远也装不出来的.为了尽量装满背包,附中的OIER想要研究一下物品不能装 ...
- ognl表达式root中取值顺序
不加#,先从栈顶取,如果没有(是没有这个属性而不是这个属性没有值),再往下取. 如果栈顶和非栈顶的对象拥有同一个属性名称,想直接取非栈顶的属性可以在ognl中用#root[i].属性名,可以取到属性的 ...
- php中ckeditor(Fckeditor)的配置方法
ckeditor 编辑器php正确配置方法 1. 下载安装 CKEditor: http://ckeditor.com/ 解压下载到的CKEditor放到网站的路径中即可 2. 下载安装 CKFind ...
- SVG 2D入门6 - 坐标与变换
坐标系统 SVG存在两套坐标系统:视窗坐标系与用户坐标系.默认情况下,用户坐标系与视窗坐标系的点是一一对应的,都为原点在视窗的左上角,x轴水平向右,y轴竖直向下:如下图所示: SVG的视窗位置一般是由 ...
- 有意义的命名 Meaningful names
名副其实 use intention-revealing names 变量.函数或类的名称应该已经答复了所有的大问题.它该告诉你,他为什么会存在,他做什么事,应该怎么用.我们应该选择都是致命了计量对象 ...
- iOS提交AppStore被拒原因
1. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound by ...
- PHP 上传图片和安全处理
上传图片 public function images() { $data = $_FILES['file']; switch($data['type']) { case 'image/jpeg': ...