41. First Missing Positive (JAVA)
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
因为不能额外使用空间,所以不能用HashTable,利用原本的数组记录状态。将值为正整数的数字放到对应值-1的下标位置。
注意无限循环:比如[1,1],所以nums[nums[i]-1]==nums[i]的情况要排除。
class Solution {
public int firstMissingPositive(int[] nums) {
int tmp;
int i = 0;
while(i < nums.length){
if(nums[i] < nums.length && nums[i] > 0 && nums[i] != i+1 && nums[nums[i]-1]!=nums[i]){
//put nums[i] at position of nums[i]-1
tmp = nums[i];
nums[i] = nums[tmp-1];
nums[tmp-1] = tmp;
}
else{
i++;
}
}
for(i = 0; i < nums.length; i++){
if(nums[i] != i+1) break;
}
return i+1;
}
}
41. First Missing Positive (JAVA)的更多相关文章
- leetcode 41 First Missing Positive ---java
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 ---------------------------------------------------------- ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- [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 Given an unsorted integer array, find the first missing positive integer. ...
- 刷题41. First Missing Positive
一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对 ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
- 41. First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- 微信小程序POST请求参数传递不到后台, 前台获取不到后端返回的数据, 以及 post 请求返回 404 但后台能收到数据
1 微信小程序POST请求参数传递不到后台 需要在微信请求 wx.request 改变默认 header 配置为如下 wx.request({ url: 'test.php', //仅为示例,并非真实 ...
- 在windows下使用Mingw搭建模拟Linux
1.到官网下载最新版Mingw 2.点击安装,最好选择默认路径,如果不是的话,路径中一定不能有空格. 3.选择安装,mingw-developer-toolkit.mingw32-base.mingw ...
- 设计模式(2): 响应store中数据的变化
概述 最近最近做项目的时候总会思考一些大的应用设计模式相关的问题,我把自己的思考记录下来,供以后开发时参考,相信对其他人也有用. store里面响应数据变化 通常情况下,我们会把数据存在store里面 ...
- 一个ETF基金经理的心路历程
简介: 鹏华沪深300ETF拟任基金经理崔俊杰先生,金融工程专业管理学硕士,5年证券基金从业经验.2008年7月加盟鹏华基金管理有限公司,历任产品规划部产品设计师.量化投资部量化研究员,先后从事产品设 ...
- python使用相对路径创建文件夹
两个py文件,一个是al文件夹下的test1.py,一个是和al文件夹同层的test2.py test1.py代码如下: import os def test(): path = './source_ ...
- Alert弹出框处理
selenium的API提供了Alert类对alert弹出框的处理的方法,涉及到的方法有text,dismiss(),accept()和send_keys(),在javascript中主要有alert ...
- lsb-realse
[root@localhost ~]# lsb_release -a -bash: lsb_release: command not found 解决方法:yum install redhat-lsb ...
- FTP搭建YUM源服务器
一.FTP搭建YUM源服务器 1.服务器 挂载centos镜像[root@localhost ~]#yum install vsftpd[root@localhost ~]#systemctl sta ...
- 递归算法之Fibonacci 斐波那契数列第n个数的求解
Fibonacci 斐波那契数列第n个数的求解,也可以用递归和非递归的形式实现,具体如下,dart语言实现. int fibonacci(int n) { if (n <= 0) throw S ...
- Java本周总结1
这两周我上认真的课应该就是李老师的课了/ 第一周主要跟我们讲述了java的发展史何java开发环境的搭建,带领我们走进了java,李老师的精彩讲述让我们对Java有了深刻的认识/. jdk下载安装包我 ...