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

  

class Solution {
public:
void reverse(int begin, int end , vector<int> &num)
{
assert(begin >= && begin <= end && end < num.size()); int i = begin , j = end;
while(i<j)
{
int temp = num[i];
num[i] = num[j];
num[j] = temp;
i++;
j--;
}
}
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = num.size();
if(len < ) return ;
int i, j;
for( i = len - ; i>= ; i-- )
if(num[i] >= num[i+])
continue;
else
break; if(i>=)
{
j = len -;
while(j >= && num[j] <= num[i])j--; int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
reverse(i+, len-, num);
}
};

reference :

http://stackoverflow.com/questions/11483060/stdnext-permutation-implementation-explanation

http://yucoding.blogspot.com/2013/04/leetcode-question-61-next-permutation.html

http://fisherlei.blogspot.com/2012/12/leetcode-next-permutation.html

一个很好的解释,来字stackoverflow

Let's look at some permutations:

...
How do we go from one permutation to the next? Firstly, let's look at things a little differently. We can view the elements as digits and the permutations as numbers. Viewing the problem in this way we want to order the permutations/numbers in "ascending" order. When we order numbers we want to "increase them by the smallest amount". For example when counting we don't count 1, 2, 3, 10, ... because there are still 4, 5, ... in between and although 10 is larger that 3, there are missing numbers which can be gotten by increasing 3 by a smaller amount. In the example above we see that 1 stays as the first number for a long time as there are many reorderings of the last 3 "digits" which "increase" the permutation by a smaller amount. So when do we finally "use" the ? When there are only no more permutations of the last digits.
And when are there no more permutations of the last digits? When the last digits are in descending order. Aha! This is key to understanding the algorithm. We only change the position of a "digit" when everything to the right is in descending order because if it isn't in descending order then there are still more permutations to go (ie we can "increase" the permutation by a smaller amount). Let's now go back to the code: while (true)
{
It j = i;
--i; if (*i < *j)
{ // ...
} if (i == begin)
{ // ...
}
}
From the first lines in the loop j is an element and i is the element before it.
Then if the elements are in ascending order (if (*i < *j)) do something.
Otherwise if the whole thing is in descending order (if (i == begin)) then this is the last permutation.
Otherwise we continue and we see that j and i are essentially decremented. We now understand the if (i == begin) part so all we need to understand is the if (*i < *j) part. Also note: "Then if the elements are in ascending order ..." which supports out previous observation that we only need to do something to a digit "when everything to the right is in descending order". The ascending order if statement is essentially finding the leftmost place where "everything to the right is in descending order". Let's look again at some examples: ... ... ...
We see that when everything to the right of a digit is in descending order, we find the next largest digit and put it in front and then put the remaining digits in ascending order. Let's look at the code: It k = end; while (!(*i < *--k))
/* pass */; iter_swap(i, k);
reverse(j, end);
return true;
Well since the things to the right are in descending order, to find the "next largest digit" we just have to iterate from the end which we see in the first lines of code. Next we swap the "next largest digit" to the front with the iter_swap() statement and then since we know that that digit was the next largest, we know that the digits to the right are still in descending order so to put it in ascending order we just have to reverse() it.

最新文章

  1. javascript 中 !~ 什么意思
  2. 七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)
  3. mysql命令行创建存储过程命令行定时执行sql语句
  4. 业务gis 怎么让别的开发人员不需要懂gis就可以搞开发? (五)
  5. oracle11g 重新配置em
  6. DIY Ruby CPU 分析 Part II
  7. bzoj 3531 [Sdoi2014]旅行(树链剖分,线段树)
  8. 分页SQL技术1-COUNT STOPKEY.
  9. c++ 07
  10. mysql的“Got error 28 from storage engine”错误
  11. CentOS下yum使用代理的设置
  12. 第四章——SQLServer2008-2012资源及性能监控(1)
  13. JS基础与循环
  14. office 2013幻灯片中插入SmartArt图形时出现错误下列一个或多个文件由于包含错误而无法运行
  15. Jmeter接口测试使用beanshell断言json返回
  16. Iframe高度自适应(兼容IEFirefox、同域跨域)
  17. jsp基础语言-jsp表达式
  18. Oracle 10046
  19. 进军的socket
  20. 微信小程序 wx.getUserInfo 解密 C# 代码 - 转

热门文章

  1. XJOI网上同步训练DAY5 T1
  2. Delphi 函数指针(函数可以当参数)
  3. 树莓派入门教程——使用Qt开发界面程序
  4. 关于IoAttachDeviceToDeviceStack
  5. zabbix 发送邮件配置
  6. easyui获取日期datebox中的值
  7. IOS uitableviewcell 向左滑动删除编辑等
  8. 使用IDEA动态调试smali代码
  9. UI基础视图----UIScrollView总结
  10. JS判断,今天所在季度,第几周, 季度的第几周,年度第几周