当前位置: 代码迷 >> 综合 >> LeetCode刷题:41. First Missing Positive
  详细解决方案

LeetCode刷题:41. First Missing Positive

热度:0   发布时间:2024-01-15 19:25:31.0

LeetCode刷题:41. First Missing Positive

原题链接:https://leetcode.com/problems/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

 

  相关解决方案