leetcode 41. First Missing Positive

    xiaoxiao2021-03-25  236

    leetcode 41. First Missing Positive 

    今天上午做了两个0-n  排序的问题,排序关键所在于swap

    public class Solution { public int firstMissingPositive(int[] nums) { int len = nums.length; int i=0; while(i<len){ if(nums[i]>len||nums[i]<=0||nums[i]==i+1) i++; else if(nums[i]!=nums[nums[i]-1]) swap(nums,nums[i]-1,i); else{ i++; } } i = 0; while(i<len&&nums[i]==i+1) i++; return i+1; } public void swap(int[] nums,int i,int j){ int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } }

    转载请注明原文地址: https://ju.6miu.com/read-563.html

    最新回复(0)