LeetCode Top Interview Questions 41. First Missing Positive (Java版; Hard)

welcome to my blog

LeetCode Top Interview Questions 41. First Missing Positive (Java版; Hard)

题目描述
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.
class Solution {
    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        if(n==0){
            return 1;
        }
        //鸽巢; index 0 上放 1
        int i=0;
        while(i<n){
            //当前巢不对&&索引不越界&&索引不越界&&正确的巢没被占
            if(nums[i] != i+1 && nums[i]-1<n && nums[i]-1>=0 && nums[nums[i]-1] != nums[i]){
                swap(nums, nums[i]-1, i);
            }else{
                i++;
            }
        }
        i=0;
        for(; i<n; i++){
            if(nums[i] != i+1){
                break;
            }
        }
        return i+1;

    }

    private void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
第一次做; 两边遍历; 双指针的做法稍微容易乱; 可以不用双指针, 进行两次遍历即可
class Solution {
    public int firstMissingPositive(int[] nums) {
        //鸽巢原理
        int i=0;
        while(i<nums.length){
            //当前数能被安放到正确的位置上, 执行交换操作, 交换完别++, 得判断交换过来的数
            if(i+1!=nums[i] && nums[i]-1>=0 && nums[i]-1<nums.length && nums[nums[i]-1] != nums[i]){
                swap(nums, i, nums[i]-1);
            }
            //当前数不能被安放到正确的位置上, 执行++操作, 处理下一个数
            else{
                i++;
            }
            
        }
        //找出第一个缺失的整数
        i=0;
        for(; i<nums.length; i++){
            if(i+1!=nums[i])
                break;
        }
        return i+1;
    }
    
    public void swap(int[] arr, int i, int j){ 
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
第一次做; 双指针, l表示[0,l-1]上依次是[1,l], r表示[0,r-1]上最好情况是[1,r], 也表示[r,end]是无效区域
/*
核心:理解l和r的含义
l:[0,l-1]位置上依次是[1,l]
r:[0,r-1]位置上最好是[1,r];   [r,end]是无效区域
过一遍这个示例:3,4,-1,1   output 2
*/
class Solution {
    public int firstMissingPositive(int[] nums) {
        if(nums==null||nums.length==0)
            return 1;
        //initialize
        //[0,l-1]范围, 保证nums[i]==i+1
        int l = 0;
        //[0,r]范围, 最好的情况是从1到r
        int r = nums.length;
        //l==r时退出循环
        while(l<r){
            if(nums[l]==l+1){
                l++;
            }
            /*
            核心:
            没用的数:
            1) nums[l]小于l+1,按理说应该把nums[l]放到该去的位置上, 但是由于l从0递增, 所以nums[l]-1这个位置已经处理完了, 所以nums[l]没用了
            2) nums[l] > r, nums[l]应该放在nums[l]-1位置上, 所以当nums[l]>r时, all[l]-1就越界了
            3) nums[l] == nums[nums[l]-1]时, 说明nums[l]该去的位置上已经有合适的了, 所以当前的nums[l]就没用了
            */ 
            else if(nums[l] < l+1 || nums[l] >r || nums[l] == nums[nums[l]-1]){
                r--;
                //把没考虑过的元素中最靠后的元素拿过来
                swap(nums, l, r);
            }
            //nums[l] > l+1 && nums[l] <= r && nums[l] != nums[nums[l]-1]
            //nums[l]放到正确的位置上:nums[l]-1
            else{
                swap(nums, l, nums[l]-1);
            }
        }
        return l+1;
    }
    public void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值