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 给出一个未排序的数组,求出第一个丢失的正数. ...
随机推荐
- Linux - gcc和g++的区别
一般linux系统都自带了gcc编译器的,你可以用你的安装光盘去安装,如果你是觉得自带的gcc版本太低了,可以去gcc的官方网站可以下载到,编译需要很长的时间,如果你只编译C或者C++可以只下载gcc ...
- java面试题之ssh
1.写出你熟悉的开源框架以及各自的作用(项目中为什么使用SSH) 答:框架:hibernate,spring,struts1/struts2. Hibernate主要用于数据持久化:封装了JDBC操作 ...
- BinaryWriter
c#里的文件操作 fileInfo dir的一大堆属性不用说 地球人都知道(什么fileName,create() delete()) ,文件系统的概念很好理解的 文件读写也好理解(硬盘到内存 然后再 ...
- NABCD分析
NABCD——今日事 N(Need):开创的成就系统可以在一定程度上督促用户坚持下来. A(Approach):做一个APP软件,是在android平台构建. B(Benefit):可以逐步改变用户的 ...
- beanUtil
mvc中,页面传值进来,struts2框架是用modeldriven spingmvc是model 不用框架的话,要手动一个一个的设置,然后在用dao方法与数据库联系 servlet框架有BeanUt ...
- 对于C#中的一些点滴你真的理解了吗?
废话不多说看题目,看看我们自己真的理解了吗? 1.如下代码输出的结果是什么? public class A{ public virtual void Func(int number=10) { Co ...
- php中的include()的使用技巧
php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...
- 打造高质量Android应用:Android开发必知的50个诀窍
打造高质量Android应用:Android开发必知的50个诀窍
- HYSBZ 1858 线段树 区间合并
//Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...
- Note_Master-Detail Application(iOS template)_06_ YJYDetailViewController.h
// YJYDetailViewController.h #import <UIKit/UIKit.h> @interface YJYDetailViewController : UIV ...