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)的更多相关文章

  1. leetcode 41 First Missing Positive ---java

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  2. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  3. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

  4. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  5. LeetCode题解41.First Missing Positive

    41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...

  6. 刷题41. First Missing Positive

    一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对 ...

  7. Java [Leetcode 41]First Missing Positive

    题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...

  8. 41. First Missing Positive

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  9. LeetCode OJ 41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

随机推荐

  1. 在最新的 create-react-app 中添加 less 支持

    前置知识: 把 webpack 的相关配置暴露出来 运行 git add -A 运行 git commit -m 'project init' 运行 yarn eject 然后选 y 项目中多出来两个 ...

  2. 利用word宏功能一键导出数据库表结构

    前言: 需求是: 为了完成<数据库设计文档>中的表结构展示,需要导出所有的表结构,包括字段名.长度.注释等必要标题. 数据库:MySQL 我选择的方法是——用word的宏功能导出.很多博客 ...

  3. Min_25筛初级应用:求$[1,n]$内质数个数

    代码 #include <bits/stdc++.h> #define rin(i,a,b) for(int i=(a);i<=(b);++i) #define irin(i,a,b ...

  4. mysql中对比 JSON_VALUE 与 JSON_QUERY

    1. JSON概述 MySQL里的json分为json array和json object. $表示整个json对象,在索引数据时用下标(对于json array,从0开始)或键值(对于json ob ...

  5. 测试常用linux命令之sed篇

    一.sed命令 可以放在单独的脚本文件中执行:sed -f script1 data1 多条命令也可以用{}括起来 sed可以进行文本的替换,删除,行添加,打印等.采用 sed [address]{c ...

  6. Visual Studio 2019 企业版 注册码 百度云下载

    微软官网下载地址:https://visualstudio.microsoft.com/zh-hans/downloads/ Key: Visual Studio 2019 Enterprise 企业 ...

  7. js2flowchart

    https://github.com/Bogdan-Lyashenko/js-code-to-svg-flowchart js2flowchart - a visualization library ...

  8. Base64工具类并发问题!

    为减少对象创建次数,一般会做如下编码: public class EncodeUtils { private static BASE64Encoder encoder; private static ...

  9. ros 下常用的依赖库

    <buildtool_depend>catkin</buildtool_depend> <build_depend>nav_msgs</build_depen ...

  10. CentOS 6、CentOS7 防火墙开放指定端口

    当我们在CentOS服务器中装了一些开发环境(如 tomcat.mysql.nginx 等...)时,希望能从外界访问,就需要配置防火墙对指定端口开放. CentOS 6.51.开放指定端口/sbin ...