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 ---------------------------------------------------------- ...
随机推荐
- BZOJ-2257 瓶子和燃料 分解因数+数论方面乱搞(裴蜀定理)
一开始真没想出解法...后来发现那么水.... 2257: [Jsoi2009]瓶子和燃料 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 970 So ...
- CentOS下yum安装VNCserver
VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...
- boost构造,解析json
void asynDBCenter::isGetActorInfoEx(void* on_process, const char* arg) { std::stringstream ros(arg); ...
- 解决微信OAuth2.0网页授权回调域名只能设置一个的问题
https://github.com/HADB/GetWeixinCode GetWeixinCode 解决微信OAuth2.0网页授权回调域名只能设置一个的问题 使用方法 部署get-weixin- ...
- json中loads的用法
python中json中的loads()和dumps()它们的作用经常弄换了,这里记录下,loads方法是把json对象转化为python对象,dumps方法是把pyhon对象转化为json对象,我是 ...
- Yii2事件
namespace app\components; use yii\base\Component; use yii\base\Event; class MessageEvent extends Eve ...
- [整理]Ajax Post请求下的Form Data和Request Payload
Ajax Post请求下的Form Data和Request Payload 通常情况下,我们通过Post提交表单,以键值对的形式存储在请求体中.此时的reqeuest headers会有Conten ...
- Request请求总结
Request.ServerVariables["Url"] 返回服务器地址 Request.ServerVariables["Path_Info"] 客户端提 ...
- 遇到个小问题,Java泛型真的是鸡肋吗?
今天遇到一个小问题,让我感觉Java的泛型(因为背负了历史的包袱导致的)有点鸡肋啊. 我们经常会遇到要一些自定义的key-value字符串,比如: "key1:1k;key2:2;key3: ...
- HDOJ 1907 John
对于任意一个 Anti-SG 游戏,如果我们规定当局面中所有的单一游戏的 SG 值为 0 时,游戏结束,则先手必胜当且仅当: (1)游戏的 SG 函数不为 0 且游戏中某个单一游戏的 SG 函数大于 ...