leetcode31 Next Permutation

Posted 王宜鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode31 Next Permutation相关的知识,希望对你有一定的参考价值。

思路:

找规律。

实现:

 1 class Solution
 2 {
 3 public:
 4     void nextPermutation(vector<int>& nums)
 5     {
 6         int n = nums.size(); bool flg = false;
 7         for (int i = n - 2; i >= 0; i--)
 8         {
 9             if (nums[i] < nums[i + 1])
10             {
11                 sort(nums.begin() + i + 1, nums.end());
12                 int p = upper_bound(nums.begin() + i + 1, nums.end(), nums[i]) - nums.begin();
13                 swap(nums[i], nums[p]);
14                 flg = true; break;
15             }
16         }
17         if (!flg) sort(nums.begin(), nums.end());
18     }
19 }

以上是关于leetcode31 Next Permutation的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode OJ 31. Next Permutation

[array] leetcode - 31. Next Permutation - Medium

[LeetCode] 31. Next Permutation ☆☆☆

Leetcode 31. Next Permutation

LeetCode-31-Next Permutation

[Leetcode]31. Next Permutation