31. Next Permutation (下一个全排列)
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,23,2,1 → 1,2,31,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 (下一个全排列)的更多相关文章
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 31 Next Permutation(下一个全排列)
题目链接: https://leetcode.com/problems/next-permutation/?tab=Description Problem :寻找给定int数组的下一个全排列(要求 ...
- NextPermutation,寻找下一个全排列
问题描述:给定一个数组是一个全排列,寻找下一个全排列.例如123->132, 321->123, 115->151. 算法分析:从后往前寻找顺序,找到后从往前寻找第一个大于当前元素, ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode(31): 下一个排列
Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- Html制作简单而漂亮的登录页面
先来看看样子. html源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- loadruner11 socket脚本-10053错误
背景: socket 10053异常:软件主动放弃一个连接,原因是超时或协议错误.如果LR客户端报10053异常,说明LR在执行套接字操作时,发生通信超时.网络中断或其它异常,主动将Socket连接断 ...
- python3----requests
import requests def get_html_text(url): try: r = requests.get(url, timeout=30) r.raise_for_status() ...
- angularjs1+nodejs搭建的个人博客 实战个人项目
项目地址:https://github.com/MrZwqShuai/Micro-agency-Demo
- centos6安装系统时选包
date: 2018-06-05 11:44:06 1,系统:minimal 2,包组: Base System : Base.Compatibility libraries.Deb ...
- oracle的后台进程能否杀掉
oracle的后台进程杀掉会有什么影响 说明:本文复制自网友的博客: https://blog.csdn.net/kellyseeme/article/details/8927757 数据库版本为: ...
- 【IIS】模块 DLL C:\Windows\System32\inetsrv\authcert.dll 未能加载。返回的数据为错误信息。
解决方案,check IIS --Client Certificate Mapping Authentication installed?
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- Spring boot:logback文件配置
resources文件夹下:新建logback-spring.xml文件. 文件内容like: <?xml version="1.0" encoding="UTF- ...
- PhoneGap在Mac环境下使用问题小结
PhoneGap在Mac环境下使用问题小结1,问题一:在执行命令phonegap run ios出现“Cordova needs ios-sim version 1.7 or greater, you ...