LC31 Next Permutation

有关排列的题目,如果用DFS去做,就十分低效。这里介绍一种做法:求下一个序列,先从尾部开始找最长的递增数组,如果从尾到头都是递增,则这已经是最大序列,下一个序列就是将最大序列翻转一下。如果不存在递增数组,则将最后两位数交换一下。其他情况,则记录下递增数组的前一位数,并找出递增数组中比这个数大的最小的那个数,交换两个数位置,然后将这个递增数组翻转一下,结果就是下一个序列。网上可以找到相关证明。

 1 class Solution {
 2 public:
 3     void nextPermutation(vector<int>& nums) {
 4         if(nums.size()<=1)
 5             return;
 6         int i=nums.size()-1;
 7         while(i>=1&&nums[i-1]>=nums[i])
 8         {
 9             i--;
10         }
11         if(i==0)
12         {
13             reverse(nums.begin(),nums.end());
14         }
15         else if(i==nums.size()-1)
16         {
17             swap(nums[i],nums[i-1]);
18         }
19         else
20         {
21             i--;
22             int j;
23             for(j=nums.size()-1;j>=i;j--)
24             {
25                 if(nums[j]>nums[i])
26                     break;
27             }
28             swap(nums[i],nums[j]);
29             reverse(nums.begin()+i+1,nums.end());
30         }
31     }
32 };

如果要求上一个序列,则是找最长的递减数组。其他分析都差不多。

 1 class Solution {
 2 public:
 3     void prevPermutation(vector<int>& nums) {
 4         if(nums.size()<=1)
 5             return;
 6         int i=nums.size()-1;
 7         while(i>=1&&nums[i-1]<=nums[i])
 8         {
 9             i--;
10         }
11         if(i==0)
12         {
13             reverse(nums.begin(),nums.end());
14         }
15         else if(i==nums.size()-1)
16         {
17             swap(nums[i],nums[i-1]);
18         }
19         else
20         {
21             i--;
22             int j;
23             for(j=nums.size()-1;j>=i;j--)
24             {
25                 if(nums[j]<nums[i])
26                     break;
27             }
28             swap(nums[i],nums[j]);
29             reverse(nums.begin()+i+1,nums.end());
30         }
31     }
32 };

时间: 2024-12-05 19:28:31

LC31 Next Permutation的相关文章

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

Permutation Sequence

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

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 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

60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

【数组】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 repla

[LeetCode]题解(python):031-Next Permutation

题目来源 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 possible

HDU3664 Permutation Counting

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 754 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l