码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 31 Next Permutation

时间:2015-11-12 06:30:43      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

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,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 };

 

LeetCode 31 Next Permutation

标签:

原文地址:http://www.cnblogs.com/yang-xiong/p/4957855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!