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:

     def nextPermutation(self, a):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
def swap(a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp def reverces(a, i):
for k in range(int((len(a) - i) / 2)):
swap(a, i + k, len(a) - k - 1) # 后找
i = len(a) - 2
while(i>=0 and a[i + 1] <= a[i]):
i -= 1 # 小大
if(i >= 0):
j = len(a) - 1
while j >= 0 and a[j] <= a[i]:
j -= 1
# 交换
swap(a, i, j)
# 反转
reverces(a, i+1)

31. Next Permutation (下一个全排列)的更多相关文章

  1. [LeetCode] 31. Next Permutation 下一个排列

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

  2. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

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

  3. [leetcode]31. Next Permutation下一个排列

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

  4. LeetCode 31 Next Permutation(下一个全排列)

    题目链接: https://leetcode.com/problems/next-permutation/?tab=Description   Problem :寻找给定int数组的下一个全排列(要求 ...

  5. NextPermutation,寻找下一个全排列

    问题描述:给定一个数组是一个全排列,寻找下一个全排列.例如123->132, 321->123, 115->151. 算法分析:从后往前寻找顺序,找到后从往前寻找第一个大于当前元素, ...

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

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

  7. Next Permutation 下一个排列

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

  8. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  9. 【LeetCode每天一题】Next Permutation(下一个排列)

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

随机推荐

  1. 用 HTML5+ payment方法支付宝支付遇到的坑

    用 HTML5+ payment方法碰到的第一个坑就是如果是支付宝的话签约那种支付方式. 因为 Dcloud的文档没有更新的原因你可以看到他们说的都是‘移动支付’,但是你去支付宝平台的时候看到的根本就 ...

  2. CStringArray序列化处理

    开发中需要对CStringArray进行保存操作,涉及到序列化,特总结一下: //写 CStringArray saTmp1; CStringArray saTmp2 saTmp1.AddString ...

  3. Linux命令之乐--md5sum

    md5sum命令用于生成和校验文件的md5值.它会逐位对文件的内容进行校验,它常用于检查文件的完整性. 读取文件的MD5值 [root@new ~]# md5sum /usr/local/sbin/* ...

  4. soundpool播放声音

    一般大家使用的是MediaPlayer来播放音频,它的创建和销毁都是非常消耗资源的,如果我们的需求是播放一些短促而且频繁播放的音频的话MediaPlayer就有些不合适了,我们来讲讲SoundPool ...

  5. JSP小例子——以Model1的思想实现用户登录小例子(不涉及DB操作)

    Model1简介现在比较流行的就是Model1和Model2,这里介绍Model1.在Model1模型出现前,整个Web应用的情况是:几乎全部由JSP页面组成,JSP页面接受处理客户端请求,对请求处理 ...

  6. springMVC问题

    网站中springmvc.xml配置: <bean id="viewResolver" class="org.springframework.web.servlet ...

  7. 06.Curator Barrier

        分布式Barrier是这样一个类: 它会阻塞所有节点上的等待进程,知道某一个被满足, 然后所有的节点继续进行.     比如赛马比赛中, 等赛马陆续来到起跑线前. 一声令下,所有的赛马都飞奔而 ...

  8. TCL电视直播软件

    升级你的电视系统我的型号46寸 V7300 3D,具体的升级程序在"技术宅"里有下载 找个格式化过的U盘把你的程序拷贝进去,插在电视上,电视会自动升级 当你成功安装V8-0MT32 ...

  9. Zabbix分布式监控

    上一篇:Zabbix的API的使用 zabbix分布式监控 新建一台主机 安装zabbix proxy和数据库 yum -y install mariadb-server zabbix-proxy-m ...

  10. CH5402 选课【树形DP】【背包】

    5402 选课 0x50「动态规划」例题 描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了 N(N≤300) 门的选修课程,每个学生可选课程的数量 M 是 ...