public void nextPermutation(int[] nums) {
        if (nums == null) return;
        int i = nums.length - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) {
            --i;
        }
        if (i < 0) {
            reverse(nums, 0, nums.length - 1);
            return;
        }
        int j = nums.length - 1;
        while (j >= i + 1 && nums[j] <= nums[i]) {
            --j;
        }
        swap(nums, i, j);
        reverse(nums, i + 1, nums.length - 1);
    }