LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)
1 0 3 -1 2 5
1 0 3 -1 2 5
1 0 3 -1 2 5
1 0 3 -1 2 5
1 2 3 -1 0 5
1 2 3 -1 0 5
1 2 3 -1 5 0
1 2 3 -1 5 0
4
public class Solution41 {
public int firstMissingPositive(int[] A) {
// added line9 condition to avoid infinite loop
// added line13, the i--, to check the swapped item. Or we failed to check all the numbers.
// ref http://stackoverflow.com/questions/1586858/find-the-smallest-integer-not-in-a-list
if (A.length == 0) return 1;//长度等于0 直接返回1
for (int i = 0; i < A.length; i++) {//遍历
//是整数,小于数组长度,不等于当前下标
if (A[i] <= A.length && A[i] > 0 && A[i] != i+1) {
if (A[A[i]-1] != A[i]) { //line 9 进行交换操作
int tmp = A[A[i]-1];
A[A[i]-1] = A[i];
A[i] = tmp;
i--; //line 13
}
}
}
for (int i = 0; i < A.length; i++) {
if (A[i] != i+1) return i+1;
}
return A.length+1;
}
}
参考代码2:
package leetcode_50; /***
*
* @author pengfei_zheng
* 找到第一个丢失的正数
*/
public class Solution41 {
public static int firstMissingPositive(int[] nums) {
int i = 0;
while(i < nums.length){
if(nums[i] == i+1 || nums[i] <= 0 || nums[i] > nums.length) i++;
else if(nums[nums[i]-1] != nums[i]) swap(nums, i, nums[i]-1);
else i++;
}
i = 0;
while(i < nums.length && nums[i] == i+1) i++;
return i+1;
} private static void swap(int[] A, int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public static void main(String[]args){
int []nums = {1,1};
System.out.println(firstMissingPositive(nums));
}
}
LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)的更多相关文章
- [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 ---------------------------------------------------------- ...
- [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 ---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 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo ...
- [leetcode]41. First Missing Positive第一个未出现的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
随机推荐
- workerman定时器使用
From: http://doc3.workerman.net/worker-development/add.html add int \Workerman\Lib\Timer::add(float ...
- The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
From: http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-remo ...
- Linux 下 Nginx + JDK + Tomcat + MySQL 安装指南
转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52026342 Nginx 是一款高性能的 http 服务器/反向代理服务器/电子邮 ...
- sp_replication_agent_checkup
Transact-SQL 参考sp_replication_agent_checkup 检查每个分发数据库的复制代理程序,这些复制代理程序正在运行但在指定的心跳信号间隔内没有历史记录.此存储过程在分发 ...
- slab着色,可以减少cache conflict miss概率么?
以内部slab为例,管理区 + object总大小+left_over size = 1page,我们做个极端假设,cache为 direct-mapped caches. 1.没有采用slab着色: ...
- java中*和**的作用
“*”就表示了所有的文件,但是“*”并不包括子目录下的文件: “**”匹配包含任意级子目录中所有的文件
- snmp简单使用
preface snmp 不多说 环境介绍 1.使用CentOs7的系统,内核版本为3.10.0-123.el7.x86_64 2.ip地址为192.168.56.12 安装snmp 1.yum安装: ...
- VC获取物理网卡的MAC地址
获取网卡的MAC地址的方法很多,如:Netbios,SNMP,GetAdaptersInfo等.经过测试发现 Netbios 方法在网线拔出的情况下获取不到MAC,而 SNMP 方法有时会获取多个重复 ...
- Python+Django(Python Web项目初体验)
参考:https://blog.csdn.net/qq_34081993/article/details/79229784 Django是一个开放源代码的Web应用框架,由Python写成. 安装Dj ...
- C#操作共享文件夹
public class NetFileShare { public NetFileShare() { } public static bool connectState(string path) { ...