博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:41. First Missing Positive
阅读量:4042 次
发布时间:2019-05-24

本文共 2781 字,大约阅读时间需要 9 分钟。

LeetCode刷题:41. First Missing Positive

原题链接:

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.


算法设计

package com.bean.algorithmbasic;

/*

 * 41. First Missing Positive
 * 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 FirstMissingPositive {

    
    public static int findFirstMissingPositive1(int[] nums) {
        int n = nums.length;
        
        // 1. mark numbers (num < 0) and (num > n) with a special marker number (n+1) 
        // (we can ignore those because if all number are > n then we'll simply return 1)
        for (int i = 0; i < n; i++) {
            if (nums[i] <= 0 || nums[i] > n) {
                nums[i] = n + 1;
            }
        }
        // note: all number in the array are now positive, and on the range 1..n+1
        
        // 2. mark each cell appearing in the array, by converting the index for that number to negative
        for (int i = 0; i < n; i++) {
            int num = Math.abs(nums[i]);
            if (num > n) {
                continue;
            }
            num--; // -1 for zero index based array (so the number 1 will be at pos 0)
            if (nums[num] > 0) { // prevents double negative operations
                nums[num] = -1 * nums[num];
            }
        }
        
        // 3. find the first cell which isn't negative (doesn't appear in the array)
        for (int i = 0; i < n; i++) {
            if (nums[i] >= 0) {
                return i + 1;
            }
        }
        
        // 4. no positive numbers were found, which means the array contains all numbers 1..n
        return n + 1;
    }

    public static int findFirstMissingPositive2(int[] target) {

        if (target.length == 0 || target == null)
            return 1;
        // 把元素放入正确的位置,例如1放在target[0],2放在target[1]...
        for (int i = 0; i < target.length; i++) {
            while (target[i] != i + 1) {
                if (target[i] >= target.length || target[i] <= 0 || target[i] == target[target[i] - 1])
                    break;
                int temp = target[i];
                target[i] = target[temp - 1];
                target[temp - 1] = temp;
            }
        }

        for (int i = 0; i < target.length; i++) {

            if (target[i] != i + 1)
                return i + 1;
        }
        return target.length + 1;
    }
    
    public static int findFirstMissingPositive3(int[] nums) {
        if(nums.length==0){
            return 1;
        }
        int ans=1;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==i+1){
                continue;
            }
            while(nums[i]<=nums.length&&nums[i]>0&&nums[i]!=nums[nums[i]-1]){
                int temp=nums[nums[i]-1];
                nums[nums[i]-1]=nums[i];
                nums[i]=temp;
            }
        }
        int k=0;
        for(k=0;k<nums.length;k++){
            if(nums[k]!=k+1){
                break;
            }
        }
        return k+1;
    }

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        int[] demo = new int[] { 3, 4, -1, 1 };

        // int[] demo=new int[] {1,2,0};

        int result = findFirstMissingPositive3(demo);

        System.out.println("result= " + result);

    }

}

程序运行结果:

result= 2

 

转载地址:http://kitdi.baihongyu.com/

你可能感兴趣的文章
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Word Break(python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>