package leetcode;

/**
 * https://leetcode.com/problems/first-missing-positive/
 */
public class Problem41 {
    public int firstMissingPositive(int[] nums) {
        int i = 0;
        while (i < nums.length) {
            if (nums[i] == i + 1 || nums[i] <= 0 || nums[i] >= nums.length) {
                i++;
            } else if (nums[nums[i] - 1] != nums[i]) {
                swap(nums, i, nums[i] - 1);
            } else {
                i++;
            }
        }
        i = 0;
        for (; i < nums.length; i++) {
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }
        return i + 1;
    }

    private static void swap(int[] nums, int i, int j) {
        if (i < 0 || i >= nums.length || j < 0 || j >= nums.length) {
            return;
        }
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

@@ -232,28 +232,8 @@
 s%5Bi%5D
- == i + 1 %7C%7C nums%5Bi%5D
  %3C= 
@@ -255,32 +255,32 @@
  nums.length) %7B%0A
+
                 
@@ -365,19 +365,16 @@
 ap(nums,
- i,
  nums%5Bi%5D
@@ -377,16 +377,19 @@
 s%5Bi%5D - 1
+, i
 );%0A     
@@ -476,15 +476,15 @@
     
-for (; 
+while (
 i %3C 
@@ -498,13 +498,8 @@
 ngth
-; i++
 ) %7B%0A
@@ -560,24 +560,24 @@
 turn i + 1;%0A
-
             
@@ -570,32 +570,49 @@
 ;%0A            %7D%0A
+            i++;%0A
         %7D%0A      

Update problem 41