Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
61 lines (50 loc) · 1.9 KB

File metadata and controls

61 lines (50 loc) · 1.9 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

Difficulty: Medium

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be and use only constant extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,31,3,2
3,2,11,2,3
1,1,51,5,1

Solution

Language: Java

class Solution {
    public void nextPermutation(int[] nums) {
        this.isDone(nums, 0);
    }
​
    private boolean isDone(int[] nums, int begin) {
        int end = nums.length;
        if (end - begin < 1) {
            return true;
        }
        if (end - begin == 2) {
            boolean result = nums[end - 1] > nums[begin];
            int temp = nums[end - 1];
            nums[end - 1] = nums[begin];
            nums[begin] = temp;
            return result;
        }
        if (this.isDone(nums, begin + 1)) {
            return true;
        } else {
            int tmp = begin + 1;
            while (tmp < end) {
                if (nums[begin] < nums[tmp]) {
                    int temp = nums[begin];
                    nums[begin] = nums[tmp];
                    nums[tmp] = temp;
                    return true;
                }
                tmp++;
            }
            Arrays.sort(nums, begin, end);
            return false;  // [1,3,5,2,4]
        }
    }
}
            while (tmp < end) {

Morty Proxy This is a proxified and sanitized view of the page, visit original site.