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

Subscribe to see which companies asked this question

 
该题在STL库里已经被实现,但是写下这种题目,有助于对模拟,还有边界条件的认识
以下是在LeetCode里提交后的排名

 只要能理清楚1234这个简单的排列的下一个排列1243和下下个排列1324是如何得到的,就可以列出以下三个关键点:
1.找出最后一个升序的位置,如果是最后两个数字,就直接交换最后两个数字
2.如果存在升序,但是不是最后两个数字,就交换升序位置的较小数字和后面数列里恰好比该数字大的数字(及比该数字大的最小数字);再将后面的数列按照从小到大的顺序排列
3.如果不存在升序的位置,就说明是一个轮回里的最后一个排列,下一个排列就是将数列反过来。
思考后发现第1点和第2点的程序是可以放在一起写
最后可以得到如下程序:

 
 
 本问题还有一种问法是60题 Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

Subscribe to see which companies asked this question

这就不能用上面的模拟操作来进行求解了,因为这样会超时(我试过,在n=9时超时)
所以要用数学规律来计算已知 n, k 条件下该输出什么
这里我们考虑一个数组1,2,3,4
如果固定第一个元素为1, 则后面的排列有3!个;如果固定第一个元素为2,则后面的排列有3!;同理可以依次考虑每一位固定后,后面的排列可能有多少种,这样就可以得到最终的排列,程序如下:

 
 
 

LeetCode31 Next Permutation and LeetCode60 Permutation Sequence的更多相关文章

  1. LeetCode60:Permutation Sequence

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

  2. Leetcode60. Permutation Sequence第k个排列

    给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  3. [Swift]LeetCode31. 下一个排列 | Next Permutation

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

  4. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  5. Next Permutation&&Permutation Sequence

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

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

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

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

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

  8. 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 ...

  9. cf500B New Year Permutation

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

随机推荐

  1. 使用 JS 来动态操作 css ,你知道几种方法?

    JavaScript 可以说是交互之王,它作为脚本语言加上许多 Web Api 进一步扩展了它的特性集,更加丰富界面交互的可操作性.这类 API 的例子包括WebGL API.Canvas API.D ...

  2. 从SAP社区上的一篇博客开始,聊聊SAP产品命名背后的那份情怀

    最近Jerry在SAP社区上看到一篇博客:It's Steampunk now. 博客原文:https://blogs.sap.com/2019/08/20/its-steampunk-now/ 什么 ...

  3. jmeter第一次使用

    创建线程组 依次创建后,在http请求页面填入接口地址,参数,头信息,点击运行,然后查看结果树

  4. python 中 ModuleNotFoundError: No module named 'Crypto' 错误处理

    今天在微信小程序服务端集成了微信的登录解密模块 WXBizDataCrypt,集成后运行程序时出现了下面的错误 (.venv) [1lin24@1lin24]# python manager_dev. ...

  5. 滑雪 ( bfs+记忆化

    https://www.luogu.org/problemnew/show/P1434  题目 #include<iostream> #include<cstdio> #inc ...

  6. 使用Scrapy框架爬取腾讯新闻

    昨晚没事写的爬取腾讯新闻代码,在此贴出,可以参考完善. # -*- coding: utf-8 -*- import json from scrapy import Spider from scrap ...

  7. seastar笔记

    1.seastar::A.then([]{B});C:的核心思想是说我们开始了A之后就开始B,开始B之后就开始C,而不需要等A做完了再做B,B做完再做C 2.通常的return是一个需要执行的函数,例 ...

  8. C语言Ⅰ博客作业06

    这个作业属于哪个课程 C语言程序设计Ⅰ 这个作业要求在哪里 熟练掌握多分支结构,字符型数据类型和逻辑运算符 我在这个课程的目标是 https://www.cnblogs.com/tongyingjun ...

  9. [cf920G][容斥原理+二分]

    https://codeforc.es/contest/920/problem/G G. List Of Integers time limit per test 5 seconds memory l ...

  10. IDEA2018创建SpringBoot无法连接https://start.spring.io

    这是由于spring-boot需要访问https://start.spring.io外网,但是由于国内的局域网限制导致的. 解决办法: 进入到IDEA的setting 搜索 HTTP Proxy 选择 ...