/**
*
* Source : https://oj.leetcode.com/problems/first-missing-positive/
*
* Created by lverpeng on 2017/7/15.
*
* 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.
*
*/
public class FindFirstMissingPositive { /**
* 找到第一个确实的正整数
* 题目特点:
* 数组是一个整数数组
* 数组中的正整数中除了缺失的一个正整数,其他都是连续的,也就是说一个长度为n的数组,数组中的数是1-n(最多,因为可能有0或者负数)
*
* 可以把每一个数房放在其下标减1的位置
* 然后遍历数组,发现num[i] != num[i-1]或说明就找到了缺失的数
*
* @param num
* @return
*/
public int fistMissingPositive (int[] num) {
for (int i = 0; i < num.length;) {
if (num[i] > 0 && num[i] != num[num[i] - 1]) {
int temp = num[num[i]-1];
num[num[i]-1] = num[i];
num[i] = temp;
} else {
i ++;
}
} for (int i = 1; i < num.length; i++) {
if (num[i] != (num[i-1] + 1)) {
return num[i - 1] + 1;
} }
return -1;
} public static void main(String[] args) {
FindFirstMissingPositive findFirstMissingPositive = new FindFirstMissingPositive();
int[] arr = new int[]{1,2,0};
int[] arr1 = new int[]{3,4,-1,1};
int[] arr2 = new int[]{3,4,1,1};
int[] arr3 = new int[]{3,4,1,1};
System.out.println(findFirstMissingPositive.fistMissingPositive(arr));
System.out.println(findFirstMissingPositive.fistMissingPositive(arr1));
System.out.println(findFirstMissingPositive.fistMissingPositive(arr2));
System.out.println(findFirstMissingPositive.fistMissingPositive(arr3));
} }

leetcode — first-missing-positive的更多相关文章

  1. [LeetCode] First Missing Positive 首个缺失的正数

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

  2. Leetcode First Missing Positive

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

  3. LeetCode: First Missing Positive 解题报告

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

  4. LeetCode – First Missing Positive

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  5. LeetCode OJ-- First Missing Positive

    https://oj.leetcode.com/problems/first-missing-positive/ 给一列数,找出缺失的第一个正数.要求时间复杂度 O(n) 第一步遍历一遍,找出最大的数 ...

  6. leetcode First Missing Positive hashset简单应用

    public class Solution { public int firstMissingPositive(int[] A) { HashSet<Integer> hash=new H ...

  7. leetcode First Missing Positive python

    class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[in ...

  8. leetcode:First Missing Positive分析和实现

    题目大意: 传入整数数组nums,求nums中未出现的正整数中的最小值.要求算法在O(n)时间复杂度内实现,并且只能分配常量空间. 分析: 一般碰到这种问题,都先对数组进行排序,再遍历数组就可以找到最 ...

  9. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

  10. leetcode 41 First Missing Positive ---java

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

随机推荐

  1. Linux学习---指针运算、修饰符(const、volatile、typedef)及、运算符(++、--、+、-)

    const:常量.只读[不能变] char *p; const char *p; [T] 字符串内容可以为“hello world”或“aaa”,但只读(不可修改) char const *p; ch ...

  2. abaqus修改inp直接建立工程

    前面已经知道,通过修改以下inp的节点和单元编号,就可以新建模型,可是对于大的工程来说,逐个选取单元进行添加材料以及确定哪步进行填土仍是比较麻烦的(如果工程网格划分好并告知哪些单元好属于哪些材料,哪些 ...

  3. golang 快速排序及二分查找

    二分查找 func main() { arr := []int{0, 1, 2, 3, 4, 5, 6} fmt.Println(BinarySearch(arr, 5)) } func Binary ...

  4. Unity3D编辑器扩展(三)——使用GUI绘制窗口

    前两篇分别讲解了创建菜单https://www.cnblogs.com/xiaoyulong/p/10115053.html和创建窗口https://www.cnblogs.com/xiaoyulon ...

  5. java上传文件获取跟目录的办法

    在java中获得文件的路径在我们做上传文件操作时是不可避免的.web 上运行1:this.getClass().getClassLoader().getResource("/"). ...

  6. wx 参数传值

    1: data-id 我们可以给HTML元素添加自定义的data-*属性 example:   假设页面里有下面的元素存在: <div id="myDiv" data-nam ...

  7. Elasticsearch System Call Filters Failed to Install

    Elasticsearch starts to run, error occurs: : system call filters failed to install; check the logs a ...

  8. POI对Excel的操作

    1. 先导包 commons-io-2.6.jar包,用于对文件的操作. 下载地址:http://commons.apache.org/proper/commons-io/download_io.cg ...

  9. IO模型的介绍

    Stevens 在文章中的一种IO Model: ****blocking IO    #阻塞 IO   (系统调用不返回结果并让当前线程一直阻塞,只有当该系统调用获得结果或者超时出错才返回) *** ...

  10. Centos7 网络报错Job for iptables.service failed because the control process exited with error code.

    今天在进行项目联系的时候,启动在待机的虚拟机,发现虚拟机的网络设置又出现了问题. 我以为像往常一样重启网卡服务就能成功,但是它却报了Job for iptables.service failed be ...