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).spa

The replacement must be in-place, do not allocate extra memory.code

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,1blog

这道题题目一开始没看懂,其实它的意思就是输入一个整数数组,须要输出比这个数组“大”的最小数组,好比输入[1,2,3],把它当作123,而后比123的由这三个数字组成的最小数是132,因此应该输出[1,3,2]。排序

个人方法是用暴力方法。首先从后往前找到须要交换的数字位置(从后往前由于后面的数是least significant的位,这样有利于找到比输入大的最小数组 好比输入[1,2,3],从3往前,比较3与它以前的数字,找到第一个比它小的数记录下来,做为可能须要交换的数字(之因此是可能由于有特殊状况,好比[1,5,6,2],这个时候比2小的是1,可是注意到5也比6小,交换5,6获得1652,交换1,2获得2561,明显1652是符合要求的而非2561)把可能须要交换的数字(索引记作front和end,好比1的索引为front和2的索引为end)记录下来之后从起始索引减一再从新往前找,循环直到数组头部.在这过程当中,若是新front的索引更大,则替换front和end。最终获得的front和end就是须要交换的数字索引。交换完以后把front以后的数字从小到大排序,便可获得答案。索引

 

 1 class Solution {
 2 public:
 3 void swap(vector<int>& nums, int j){ //若是出现输入数组已是最大(降序排列),                  则reverse数组
 4     int temp;
 5     temp = nums[j];
 6     nums[j] = nums[nums.size()-1-j];
 7     nums[nums.size()-1-j] = temp;
 8 }
 9 
10 int findSwapPoint(vector<int>& nums,int startIndex){
11     int currentFrontPoint = -1,currenEndPoint = -1;//记录须要交换的两点索引
12     while(startIndex>0){  //从后往前,直到起始点索引等于1
13     for(int i = startIndex-1 ; i >= 0 ; i--){//从起始点索引-1的点开始和索引点比较
14         if(nums[i]<nums[startIndex]){   //该点小于索引点
15             if(i>currentFrontPoint){         //该店的索引大于当前记录的需交换索引点
16                 currentFrontPoint = i;      //更新需交换索引点
17                 currenEndPoint = startIndex;
18             }
19         }
20      }
21         startIndex --;   //索引减一,更新索引点
22     }
23     int temp = nums[currenEndPoint];
24     nums[currenEndPoint] = nums[currentFrontPoint];
25     nums[currentFrontPoint] = temp;
26     return currentFrontPoint;
27 }
28 void nextPermutation(vector<int>& nums) {
29     int len = nums.size();
30     bool isDecreaseOrder = true;
31     int swapPoint;
32     for(int i = len-1 ;i>0 ; i--){
33         if(nums[i]>nums[i-1]){
34             isDecreaseOrder = false;//判断是否降序排列
35             break;
36         }
37     }
38     if(isDecreaseOrder == true){//逆置数组
39         for(int j = 0 ; j <= (len-1)/2 ; j++){
40             swap(nums,j);
41         }
42     }
43     else{
44         swapPoint = findSwapPoint(nums,len-1);//找到须要交换的位置索引
45         for(int k = swapPoint+1; k<len-1 ;k++) {    //sort nums[i+1]~nums[len-1] in increase order                                    //升序排列索引以后的元素
46             int minNum = nums[k];
47             int index = k;
48             for(int l = k+1 ;l < len ;l++){
49                 if(nums[l]<minNum){
50                     minNum = nums[l];
51                     index = l;
52                 }
53             }
54             int temp = nums[k];
55             nums[k] = nums[index];
56             nums[index] = temp;
57         }
58     }
59 }
60 };
相关文章
相关标签/搜索