[array] leetcode - 31. Next Permutation - Medium
2017-11-15 10:00
375 查看
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 order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1
解析
方法 1
暴力解决,O(n!) 的时间复杂度求出数组的全排列,然后返回比当前排列大的序列中最小的。方法 2
时间复杂度:O(n);空间复杂度:O(1)这里有很好的解释
不失一般性,假设数组如图所示。我们从后往前遍历数组,比较相邻的两个数,直到出现前一个数小于后一个数时,循环停止。如图中 a[i-1] < a[i],循环的结果有以下两点说明:
在 a[i,...n-1] 是非递增有序(递减有序)的数组,因为 for 循环提前结束的条件是 a[i-1] < a[i],如果没有提前结束则说明,a[i-1] > a[i]。在这样的情况下,我们需要找到 a[i,...,n-1] 中比 a[i-1] 大的数中最小的那个,假设为 a[j]。将 a[j] 和 a[i-1] 交换,由数组的性质可得 a[i,...,n-1] 依然保持非递增(递减)有序,这时我们只需要将 a[i,...,n-1] 反转,即可得到下一个最小的排列。
如果循环没有提前结束,则说明每一次比较都是 a[i-1] > a[i],说明整个数组非递增(递减)有序。在这样的情况下直接将数组反转即可得到最小的排列。
具体实现的逻辑参看代码。
code
#include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution{ public: void nextPermutation(vector<int>& nums) { if(nums.empty()) return; int isplit = -1; for(int i=nums.size()-1; i>0; i--){ if(nums[i-1] < nums[i]){ isplit = i-1; break; } } // if isplit == -1, which indicate nums is in descending // otherwhise, [isplit+1, n-1] is descending if(isplit == -1){ reverse(nums, 0, nums.size()-1); }else{ // construct next permutation // find the smallest one from the numbers which larger than nums[isplit] int ismallest = nums.size()-1; while(ismallest>isplit && nums[ismallest] <= nums[isplit]) ismallest--; // nums[ismallest] > nums[isplit] >= nums[ismallest+1, ... , n-1] // and nums[isplit+1, ismallest-1] >= nums[ismallest] >= nums[ismallest+1,..,n-1] swap(nums[isplit], nums[ismallest]); // nums[isplit+1, ... , n-1] still in descending // so just reverse reverse(nums, isplit+1, nums.size()-1); } } void reverse(vector<int>& nums, int ileft, int iright){ while(ileft < iright){ swap(nums[ileft], nums[iright]); ileft++; iright--; } } }; int main() { return 0; }
相关文章推荐
- 【Leetcode】【Medium】Find Minimum in Rotated Sorted Array
- LeetCode 215. Kth Largest Element in an Array(Medium)
- 【LeetCode】215. Kth Largest Element in an Array (Medium)
- Leetcode 108. Convert Sorted Array to Binary Search Tree (Medium) (cpp)
- 【Leetcode-Medium-238】Product of Array Except Self
- 【leetcode】Array—— Next Permutation(31)
- [leetcode 80, Medium] Remove Duplicates from Sorted Array II
- Leetcode 167. Two Sum II - Input array is sorted (Medium) (cpp)
- Leetcode 81. Search in Rotated Sorted Array II (Medium) (cpp)
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
- LeetCode-31-Next Permutation(找规律/证明)-Medium
- 【LeetCode】 Find Minimum In Rotated Sorted Array_II(BinarySearch) - Medium
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
- [array] leetcode - 40. Combination Sum II - Medium
- LeetCode 31.Next Permutation & 33.Search in Rotated Sorted Array
- LeetCode刷题(C++)——Remove Duplicates from Sorted Array II(Medium)
- (medium)LeetCode 238.Product of Array Except Self
- [array] leetcode-55. Jump Game - Medium
- 算法分析与设计丨第二周丨LeetCode(3)——Kth Largest Element in an Array(Medium)
- Array Leetcode :Medium