public int firstMissingPositive(int[] nums) {
        for (int i = 0; i < nums.length; ++i) {
            int num = nums[i];
            while (num != i + 1 && num <= nums.length && num > 0 && nums[num - 1] != num) {
                swap(nums, i, num - 1);
                num = nums[i];
            }
        }
        for (int i = 0; i < nums.length; ++i) {
            if (i + 1 != nums[i]) {
                return i + 1;
            }
        }
        return nums.length + 1;
    }