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,31,3,2
3,2,11,2,3
1,1,51,5,1

asked 25 Feb '12, 16:40

1337c0d3r's gravatar image

1337c0d3r ♦♦
306217108
accept rate: 0%


void nextPermutation(vector<int> &num) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    vector<int>::iterator it = num.end() - 1;

    while (it!=num.begin() && (*(it-1) >= *it)) it--; //find *(it-1) < *it
    if (it == num.begin()) { //last element, rewind to the first one, i.e, ascending order
        reverse(it, num.end());
        return;
    }
    it--; //left elem to be exchanged

    vector<int>::iterator rit = it+1;
    while (rit!=num.end() && *rit > *it) rit++;
    rit--; //right elem to be exchanged

    //swap (left,right), then reverse right part to ascending order
    iter_swap(it, rit);
    reverse(it+1, num.end());
}
link

answered 03 Jan, 07:43

bridger's gravatar image

bridger
463
accept rate: 0%

class Solution {
public:
    void nextPermutation(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        for (int i = num.size() - 2; i >= 0; --i) {
            if (num[i] < num[i + 1]) {
                for (int j = i + 1; j < num.size(); ++j) {
                    if (j == num.size() - 1 || num[j + 1] <= num[i]) {
                        swap(num[i], num[j]);
                        reverse(num.begin() + i + 1, num.end());
                        return;
                    }
                }
            }
        }
        reverse(num.begin(), num.end());
    }
};
link

answered 23 Jan, 21:18

Linfuzki's gravatar image

Linfuzki
714
accept rate: 0%

the same as bridger's solution; but this one uses index instead of iterator

(23 Jan, 21:20) Linfuzki

public void nextPermutation(int[] num) {

    for(int i = num.length - 1; i >= 0; i--) {
        for(int j = i - 1; j >= 0; j--) {
            if(num[j] < num[i]) {
                // find the first num[j] < num[i], swap them, and sort elements after j
                int temp = num[j];
                num[j] = num[i];
                num[i] = temp;

                Arrays.sort(num, j + 1, num.length);
                return;
            }
        }
    }

    Arrays.sort(num);
}
link

answered 2 days ago

entourage's gravatar image

entourage
113
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×125

Asked: 25 Feb '12, 16:40

Seen: 102 times

Last updated: 2 days ago