Next Permutation - LeetCode

注意点

  • 如果是字典序最大的串则要返回字典序最小的串

解法

解法一:参见:http://www.cnblogs.com/grandyang/p/4428207.html 时间复杂度O(n)。

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int n = nums.size(),i = n-2,j = n-1;
        while(i >= 0 && nums[i] >= nums[i+1]) i--;
        cout << i;
        if(i >= 0)
        {
            while(nums[j] <= nums[i]) j--;
            swap(nums[i],nums[j]);
        }
        reverse(nums.begin()+i+1,nums.end());
    }
};

Next Permutation - LeetCode

小结

  • 排列题,有许多变种

相关文章:

  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-16
  • 2021-10-23
  • 2021-07-07
  • 2021-09-24
  • 2021-12-17
  • 2021-09-15
  • 2021-09-29
相关资源
相似解决方案