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

这题我看了半天都没搞懂啥是"下一个排列(next permutation)".找了些资料(http://www.geeksforgeeks.org/find-next-greater-number-set-digits/),才了解啥叫 next permutation.

这么弄:

假设 A = [2 1 8 7 6 5], 它的 next permutation 咋求? 这样求:

  1. 从右往左,5 6 7 8 都是增序,突然 1 降下来了, 那就所定 1;
  2. 1 右边有 8 7 6 5, 找比1大的但却是这四个数中较小的那个数, 就是 5 喽;
  3. 交换 1 和 5, 结果是 A = [2 5 8 7 6 1];
  4. 然后 对 [8 7 6 1] 增序排序. 最后返回 A = [2 5 1 6 7 8].

打完,收功!

这么做就能得到某序列的所谓的 next permutation 了?

是的!

为啥?

没为啥,遍个程序实现它就完事了!

题目要求中有个特殊情况,就是当 A = [4 3 2 1] 时,返回 A = [1 2 3 4].

以上就是本题的全部解释了.

更加详细的考虑及设置,在我的代码注释中写的很清楚.

人家想法,自个代码:

有个 cpp 副产品: reverse(A.begin() + start, A.end());

这个 start 是 A = [2 1 8 7 6 5] 中元素 8 的 index, start = 2.

\(O(n)\) time, \(O(1)\) extra space.

严格来说,里面用到了排序,就不是\(O(n)\) time 了.

// e.g.
// A = [2 1 8 7 6 5]
// step 1: from right to left, seek element '1';
// step 2: from right to left, seek '5' and swap(A[1], A[5])
// --- After step 2, A = [2 1 8 7 6 5] --> A = [2 5 8 7 6 1]
// step 3: reverse(A.begin() + 2, A.end())
// --- return A = [2 5 1 6 7 8] // special case: A = [4 3 2 1] --> will return A = [1 2 3 4] void nextPermutation(vector<int>& A) {
const int n = A.size();
if (n == 0 || n == 1)
return; //special case int start; // 4 3 2 1, can not found A[i]<A[i+1]
bool found = false; // step 1
for (int i = n - 2; i >= 0; i--) {
if (A[i] < A[i + 1]) {
start = i + 1;
found = true; // found the case A[i]<A[i+1]
break;
}
} // special case
// 4 3 2 1 --> return 1 2 3 4 directly
if (found == false) {
reverse(A.begin(), A.end());
return;
} // step 2
for (int j = n - 1; j >= start; j--) {
if (A[j] > A[start - 1]) {
swap(A[j], A[start - 1]);
break;
}
} // step 3
reverse(A.begin() + start, A.end()); return;
}

31. Next Permutation(中等,搞清楚啥是 next permutation)的更多相关文章

  1. 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence

    ▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...

  2. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  3. CF785CAnton and Permutation(分块 动态逆序对)

    Anton likes permutations, especially he likes to permute their elements. Note that a permutation of  ...

  4. Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    C. Permutation Cycle   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces 500B. New Year Permutation[连通性]

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. CF 500 B. New Year Permutation 并查集

    User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...

  7. (转)排列算法 Permutation Generation

    转自:http://www.cnblogs.com/dragonpig/archive/2010/01/21/1653680.html http://www.notesandreviews.com/p ...

  8. Codeforces 612E - Square Root of Permutation

    E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...

  9. cf500B New Year Permutation

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. 新概念英语(1-39)Don't drop it!

    新概念英语(1-39)Don't drop it! Where does Sam put the vase in the end ? A:What are you going to do with t ...

  2. UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 1987: illegal multibyte sequence

    在爬取 url = "http://stats.meizhou.gov.cn/show/index/1543/1689" 时出现了问题: UnicodeEncodeError: ' ...

  3. Mac里安装Jmeter

    前提是需要安装jdk,参见http://www.cnblogs.com/fun0623/p/4703456.html 1.解压包 (双击apache-jmeter-2.13) 2.进去到解压后的bin ...

  4. LayUI之table数据表格获取行、行高亮等相关操作

    前言 目前LayUI数据表格既美观有不乏一些实用功能.基本上表格应有的操作已经具备,LayUI作者[贤心]肯定是煞费苦心去优化,此处致敬.但是实话实话,如果单纯那数据表格功能来说,EasUI的数据表格 ...

  5. IT技术有感

    今天看技术文章,spring相关的,某一个点以前每次看一直不理解, 可是不知道为什么隔了1年左右,中间什么都没做,现在却都懂了. 在看懂的那一刻,笼罩在我心上的躁动突然平静了许多,我的心这一年来前所未 ...

  6. c# IPC实现本机进程之间的通信

    IPC可以实现本地进程之间通信.这种用法不是太常见,常见的替代方案是使用wcf,remoting,web service,socket(tcp/pipe/...)等其他分布式部署方案来替代进程之间的通 ...

  7. Spring之IOC(一)

    Spring之IOC(一) 对于Spring的基本概念在这里就不在赘述了. Spring的最核心的两部分则是AOP(面向切面编程)和IOC(控制反转).本篇文章主要讲我对IOC的理解. IOC:即In ...

  8. vue-router动态路由 刷新页面 静态资源没有加载的原因

    在做项目的时候,发现刷新页面的时候,静态路由没有加载,度娘了一圈,终于解决了. vue-router使用history模式+使用嵌套路由: 访问路由和嵌套路由页面,显示正常,但是刷新页面的时候,嵌套路 ...

  9. 解决-Django使用filter过滤时间,无法获取月份的问题

    django中的filter日期查询属性有:year.month.day.week_day.hour.minute.second 但是但我在使用过滤查询是却总是无法过滤出月份,各种查资料,最后才发现是 ...

  10. java中lamda表达式的应用

    lamda表达式主要是为了解决匿名内部类的繁琐过程 范例:简单的lamda表达式 此处使用匿名内部类 package com.java.demo; interface IMessage{ public ...