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. 设置Android程序图标和程序标题

    修改程序名称 在/res/values/strings.xml中修改程序名称,例如: <string name="app_name">你的程序名称</string ...

  2. angularJs多文件上传

    <input type="file" id="file{{$index}}" class="file{{$index}}" ngf-s ...

  3. SqlAlchemy使用详解

    python之sqlalchemy创建表的实例详解 通过sqlalchemy创建表需要三要素:引擎,基类,元素 from sqlalchemy import create_engine from sq ...

  4. yii 常用orm

    yii2 orwhere andwhere的复杂写法:https://www.codercto.com/a/6513.html $files = XXXX::find() ->andWhere( ...

  5. mybatis总结(三)之多表查询

    上一节,已经把实体类和配置文件都写过了,这节课直接添加几个方法吧 在DeptMapper.xml文件中添加 <!-- 多表查询(1对多) ,通过部门编号,查询出部门所在的员工姓名,部门名,部门编 ...

  6. jquery获取设置input值

    $("#input").val("123"),注意val()这个函数$("#input").val("123"),//给 ...

  7. Hibernate_day01--Hibernate配置文件详解_核心api

    Hibernate映射配置文件(重点) 1 映射配置文件名称和位置没有固定要求 2 映射配置文件中,标签name属性值写实体类相关内容 (1)class标签name属性值实体类全路径 (2)id标签和 ...

  8. Activity 5秒 Broadcast 10秒 Service 20秒

    第一:什么会引发ANR? 在Android里,应用程序的响应性是由Activity Manager和WindowManager系统服务监视的 .当它监测到以下情况中的一个时,Android就会针对特定 ...

  9. dbForge mysql数据库比对

    Comparison选项卡,新建一个表结构比较, (将source库的表结构变化应用到target库) 下面示例中,source用positec_uat,     target用positec_pro ...

  10. 进击的RecyclerView入门一(简单上手)

    虽然RecyclerView面世有一段时间了,但由于它的学习成本相对较高,很多码友只是粗略的认识了一下而没有细致的品味RecyclerView的真谛. 那么从现在开始我将带你装逼带你飞,一起领略Goo ...