LeetCode – Refresh – Next Permutation

 1 class Solution {
 2 public:
 3     void swap(int &a, int &b){int t = a; a = b; b = t;};
 4     void reverse(vector<int> &num, int start, int end) {
 5         int len = (end - start + 1)/2;
 6         for (int i = 0; i < len; i++) swap(num[start+i], num[end-i]);
 7     }
 8     void nextPermutation(vector<int> &num) {
 9         int len = num.size();
10         if (len < 2) return;
11         for (int i = len-2; i >= 0; i--) {
12             if (num[i] < num[i+1]) {
13                 for (int j = len-1; j > i; j--) {
14                     if (num[i] < num[j]) {
15                         swap(num[i], num[j]);
16                         break;
17                     }
18                 }
19                 reverse(num, i+1, len-1);
20                 return;
21             }
22         }
23         reverse(num, 0, len-1);
24     }
25 };
时间: 2024-10-11 05:39:40

LeetCode – Refresh – Next Permutation的相关文章

LeetCode - Refresh - Pascal&#39;s Triangle II

Exact same as I. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if (rowIndex < 0) return vector<int> (); 5 vector<int> result(1, 1); 6 for (int i = 1; i <= rowIndex; i++) { 7 for (int j = result.size()-1; j >

LeetCode - Refresh - Pascal&#39;s Triangle

This is simple. Just everything use current[j] += current[j-1]. But leave the first one to be "1". Then add another "1" to end. 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 vector<vector&

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

[array] leetcode - 31. Next Permutation - Medium

leetcode - 31. Next Permutation - Medium descrition 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

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

LeetCode 31. Next Permutation

Problem: https://leetcode.com/problems/next-permutation/ 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 poss

leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

Next Permutation 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 orde

LeetCode 31. Next Permutation (下一个排列)

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 replaceme

[LeetCode][JavaScript]Next Permutation

Next Permutation 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 orde