CODE 102: Next Permutation

来源:互联网 发布:沉默的羔羊解析知乎 编辑:程序博客网 时间:2024/04/29 20:34

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 in-place, do not allocate 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,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

public void nextPermutation(int[] num) {// IMPORTANT: Please reset any member data you declared, as// the same Solution instance will be reused for each test case.List<Integer> rest = new ArrayList<Integer>();int i = 0;for (i = num.length - 1; i >= 1; i--) {if (num[i] <= num[i - 1]) {rest.add(num[i]);} else {break;}}if (i <= 0) {Arrays.sort(num);} else {int j = 0;rest.add(num[i]);for (; j < rest.size(); j++) {if (rest.get(j) > num[i - 1]) {break;}}int tmp = rest.get(j);rest.set(j, num[i - 1]);num[i - 1] = tmp;for (j = 0; i < num.length; i++, j++) {num[i] = rest.get(j);}}}