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.

LeetCode_Next Permutation的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. Permutation test: p, CI, CI of P 置换检验相关统计量的计算

    For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

随机推荐

  1. CCI_chapter 16 Low level

    16.5 Write a program to find whether a machine is big endian or little endian Big-Endian和Little-Endi ...

  2. 在docker以FPM-PHP运行php,慢日志导致的BUG分析

    问题描述: 最近将IOS书城容器化,切换流量后.正常的业务测试了一般,都没发现问题.线上的错误监控系统也没有报警,以为迁移工作又告一段落了,暗暗的松了一口气.紧接着,报警邮件来了,查看发现是一个苹果支 ...

  3. testNg官方文档

    官方文档:http://testng.org/doc/documentation-main.html

  4. docker_openwrt

    http://wiki.openwrt.org/doc/howto/docker_openwrt_image https://www.baidu.com/s?wd=lxc%20docker&r ...

  5. Linux企业级项目实践之网络爬虫(9)——通过URL抓取网页内容

    基本URL包含模式(或称协议).服务器名称(或IP地址).路径和文件名,如"协议://授权/路径?查询".完整的.带有授权部分的普通统一资源标志符语法看上去如下:协议://用户名: ...

  6. SQL 连接 JOIN 例解。(左连接,右连接,全连接,内连接,交叉连接,自连接)

    SQL 连接 JOIN 例解.(左连接,右连接,全连接,内连接,交叉连接,自连接) 最近公司在招人,同事问了几个自认为数据库可以的应聘者关于库连接的问题,回答不尽理想-现在在这写写关于它们的作用假设有 ...

  7. bzoj 1192

    http://www.lydsy.com/JudgeOnline/problem.php?id=1192 好像学过一个东西: [0..2^(N+1)-1]内的数都的都可以由2^0,2^1,...,2^ ...

  8. Xcode7中你一定要知道的炸裂调试神技(转)

    1.Address Sanitizer: 妈妈再也不用担心 EXC_BAD_ACCESS? EXC_BAD_ACCESS一直是很多开发者的噩梦,因为这个错误很不直观,出现后往往要花很长时间才能定位到错 ...

  9. <php>删除(有内容的)文件夹函数程序

    function deldir($dirname) { if(!file_exists($dirname)) {//判断文件夹是否存在 die("文件夹不存在!");//作用等于( ...

  10. ubuntu14.04折腾迅雷xware

    迅雷一直没有出linux版,wine不想去弄.linux下虽然也有各种bt软件,无奈我试用后却发现速度远比不上迅雷,甚至有些资源根本找不到.而有些迅雷的专用链接,更是没法下(原谅我2M的小水管,却喜欢 ...