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 给出一个未排序的数组,求出第一个丢失的正数. ...
随机推荐
- jsoup Cookbook(中文版)--爬虫(java)
转载:http://www.open-open.com/jsoup/ 目录: 入门 解析和遍历一个html文档 输入 解析一个html字符串 解析一个body片断 根据一个url加载Document对 ...
- Spring学习笔记之方法注入
public abstract class ReplacedBean {protected static final Log log = LogFactory.getLog(ReplacedBean ...
- php变量的判空和类型判断
(1)var_dump(); 判断一个变量是否已经声明并且赋值,并且打印类型和值 <?php $a; var_dump($a);//输出null <?php var_dump($a);// ...
- 【kate总结】matlab调用opencv总结
正常情况下,编写好matlab调用opencv的代码. 1.输入 MEX XX.CPP(所有的mex都要编译) 2.将生成的.mexw64 放到要调用的文件夹下即可 出错总结: 本人写的matla ...
- 关于强制类型转换(c语言)
因为今天看的代码中用到了结构体的强制类型转换,就很想了解一下结构体的强制类型转换是怎样的. 一个结构体如下: 在下面这段代码中rbuf->reqCmdBuf是一个空指针,首先将这个空指针赋值给一 ...
- iOS 端的 UI 聊天组件ChatKit及代码实现
ChatKit 是一个免费且开源的 UI 聊天组件,自带云服务器,自带推送,支持消息漫游,消息永久存储.底层聊天服务基于LeanCloud(原名 AVOS ) 的 IM 实时通信服务「LeanMess ...
- 检测INT3 软断点
“INT3”断点指令的机器码是 “0xcch” 检测思路,取函数地址,判断第一个字节是不是 “CCh” BYTE bFirst = ; ProcAddres = GetProcAddress(Load ...
- SVG 2D入门1 - SVG综述
位图与矢量图 以前,浏览器中显示的图形,例如jpeg.gif等,都是位图,这些图像格式是基于光栅的.在光栅图像中,图像文件定义了图像中每个像素的颜色值.浏览器需要读取这些值并做出相应行动.这种图像的再 ...
- Fake_AP模式下的Easy-Creds浅析
Easy-Creds是一款欺骗嗅探为主的攻击脚本工具,他具备arp毒化,dns毒化等一些嗅探攻击模式.它最亮的地方就是它的fakeAP功能.它比一般自行搭建的fake AP要稳定的多.而且里面还包含了 ...
- HDOJ-三部曲一(搜索、数学)-1006- Catch That Cow
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...