leetcode 31 Next Permutation

2018-06-04 12:31:33 浏览数 (1)

代码语言:javascript复制
class Solution {
public:
    void nextPermutation(vector<int> &num) {
        int i, j, n = num.size();
        for (i = n - 2; i >= 0; --i) {
            if (num[i   1] > num[i]) {
                for (j = n - 1; j >= i; --j) {
                    if (num[j] > num[i]) break;
                }
                swap(num[i], num[j]);
                reverse(num.begin()   i   1, num.end());
                return;
            }
        }
        reverse(num.begin(), num.end());
    }
};

0 人点赞