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 给出一个未排序的数组,求出第一个丢失的正数. ...
随机推荐
- Java 类的一些高级特征
1. 面向对象的特征二:继承性 * 1.为什么要设计继承性? 继承的出现提高了代码的复用性. 继承的出现让类与类之间产生了关系,提供了多态的前提. * 2.通过"class A extend ...
- CSU1022
题目: blue和AutoGerk是好朋友.他们的相同点是都喜欢研究算法,不同点是AutoGerk已是大牛而blue还是菜鸟.blue经常拿一些自以为很难的问题去问AutoGerk,想难倒他,但是每次 ...
- nginx初识
- Problem K 栈
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- setLayoutParams getLayoutParams
继承关系:java.lang.Object ↳ android.view.ViewGroup.LayoutParams ↳ android.view.ViewGroup.MarginLayoutPar ...
- 数组prototype添加函数呢,采用回调判定函数内容
1.解决方案 Array.prototype.all = function (p) { return this.filter(p).length == this.length; }; Array.pr ...
- Emacs常用命令
1.离开Emacs 挂起Emacs C-z 退出Emacs C-x C-c 2.文件 打开文件 C-x C-f 保存文件 C-x C-s 保存所有的文件 C-x s 将一个文件的内容插入到当前buff ...
- River Crossing 简单的动态规划 .
第一行 t 表示有几组测试数据 . 每组测试数据的 第一行是 n, m . 然后 下面有n行数据 . 题意:有1个人和N只羊要过河.一个人单独过河花费的时间是M,每次带一只羊过河花费时 ...
- C++面向过程解决三阶行列式问题
#include<iostream> #include <cstdlib> using namespace std; int print() { cout<<&qu ...
- ACDream-C - Transformers' Mission(Dijastra最短路径)
dijstra求最短路径:经典应用题目: 题意:给你一个带权值无向图,权值是A点到B点的时间,然后告诉你起点,一个人可以去炸掉一个结点或多个节点,也可以派多个人,最终这些人在终点集合,问最后一个到达终 ...