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 ...
随机推荐
- [mysql-Ver5.6.23] windows版my.ini配置
基于utf8mb4比utf8多了种编码,能更好的支持emoji表情(http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.htm ...
- mysql Communication link failure, message from server: "Can't get hostname for your address"
在连接mysql jdbc时候,抛出了 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communicat ...
- semi-global matching 算法总结
semi-global matching(缩写SGM)是一种用于计算双目视觉中disparity的半全局匹配算法.在OpenCV中的实现为semi-global block matching(SGBM ...
- ASP.NET程序也能像WinForm程序一样运行[转载]
阅读目录 开始 操作方式 支持的ASP.NET程序类别 它也是个HTTP服务器 支持远程机器访问 不受限于Windows防火墙 尊重每个人的操作习惯 内置多标签浏览器支持 启动参数及配置文件 支持 . ...
- Android开发学习笔记-关于Android的消息推送以及前后台切换
下面是最简单的Android的消息推送的实现方法 package com.example.shownotic; import java.util.Random; import android.supp ...
- windows下WAMP php5.x redis扩展
其解压到php的扩展目录ext下,在php.ini文件中扩展部分增加一行:extension=php_redis.dll 新增下载地下: php5.3 http://download.csdn.net ...
- C# AES要解密的数据的长度无效
加密方式 AES-CBC-128 将解密方法改成如下 public string Decrypt(string toDecrypt, string key) { if (string.IsNullO ...
- Mac下配置svn服务器
Mac OS X 系统已经内置了svn支持,所以需要做的就只是配置,可以用svnadmin –vsersion(svnserve –version)查看.希望能对 您配置 SVN.进行开发版本控制有所 ...
- 使用npm国内镜像
嫌npm指令速度慢的童鞋可以把npm的源转换成国内的即可提高响应速度: 镜像使用方法(三种办法任意一种都能解决问题,建议使用第1或者第3种,将配置写死,下次用的时候配置还在):1.通过config命令 ...
- Java -- 异常的捕获及处理 -- Exception类与RuntimeException类
7.3 Exception类与RuntimeException类 Exception类与RuntimeException类的联系与区别??? 例:字符串变为整型 Class : RuntimeExce ...