JAVA--------------Easy Version To Understand!!!!!


  • 0
    H
    	public static int firstMissingPositive(int[] nums) {
    	if (nums == null || nums.length == 0)
    		return 1;
    	int len = nums.length, tmp, i;
    	for (i = 0; i < len; i++) {
    		while (nums[i] > 0 && nums[i] <= len && nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]) {
    			tmp = nums[i];
    			nums[i] = nums[nums[i] - 1];
    			nums[tmp - 1] = tmp;
    		}
    	}
    
    	for (i = 1; i <= len; i++) {
    		if (i != nums[i - 1])
    			return i;
    	}
    	return i;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.