题目

下一个排列

给定一个整数数组来表示排列,找出其之后的一个排列。

样例

给出排列[1,3,2,3],其下一个排列是[1,3,3,2]

给出排列[4,3,2,1],其下一个排列是[1,2,3,4]

注意

排列中可能包含重复的整数

解题

和上一题求上一个排列应该很类似

1.对这个数,先从右到左找到递增序列的前一个位置,peakInd

2.若peakInd = -1 这个数直接逆序就是答案了

3.peakInd>= 0 peakInd这个位置的所,和 peakInd 到nums.size() -1 内的数比较,返回 nums[peakInd] < nums[swapInd]最大的下标 swapInd

nums[swapInd] 是和nums[peakInd] 最接近并且比 nums[peakInd]大的数,这两个数进行交换

4.同样,peakInd + 1 到nums.length() - 1 内的数进行逆序

Python

class Solution:
# @param num : a list of integer
# @return : a list of integer
def nextPermutation(self, nums):
# write your code here
peakInd = len(nums) - 1
while peakInd>0 and nums[peakInd] <= nums[peakInd-1]:
peakInd -=1
peakInd -=1
if peakInd>=0:
swapInd = peakInd + 1
while swapInd< len(nums) and nums[swapInd]> nums[peakInd]:
swapInd +=1
swapInd -=1
nums[swapInd],nums[peakInd] = nums[peakInd],nums[swapInd]
left = peakInd + 1
right = len(nums) - 1
while left < right:
nums[left],nums[right] = nums[right],nums[left]
left +=1
right -=1
return nums

Python Code

Java

public class Solution {
/**
* @param nums: an array of integers
* @return: return nothing (void), do not return anything, modify nums in-place instead
*/
public int[] nextPermutation(int[] nums) {
// write your code here
int peakInd = nums.length-1;
while(peakInd>0 && nums[peakInd-1] >= nums[peakInd]){
peakInd --;
}
peakInd --;
if(peakInd>=0){
int swapInd = peakInd + 1;
while(swapInd< nums.length && nums[swapInd] > nums[peakInd]){
swapInd ++;
}
swapInd --;
swapnums(nums,peakInd,swapInd);
}
int left = peakInd + 1;
int right = nums.length - 1;
while(left< right){
swapnums(nums,left,right);
left +=1;
right -=1;
} return nums;
}
private void swapnums(int[] nums,int left,int right){
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
}
}

Java Code

lintcode:next permutation下一个排列的更多相关文章

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

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

  2. Next Permutation 下一个排列

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

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

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

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

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

  5. 031 Next Permutation 下一个排列

    实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...

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

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

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

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

  8. 力扣——Next Permutation(下一个排列) python实现

    题目描述: 中文: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...

  9. Leetcode31--->Next Permutation(数字的下一个排列)

    题目: 给定一个整数,存放在数组中,求出该整数的下一个排列(字典顺序):要求原地置换,且不能分配额外的内存 举例: 1,2,3 → 1,3,2:  3,2,1 → 1,2,3:  1,1,5 → 1, ...

随机推荐

  1. css style与class之间的区别

    问题描述:    网页点击[导出]按钮后,将页面table内容另存成excel文件,却发现无法保存表格样式 分析过程: 1.table表格用class,而不是style.导出时并没有导出class定义 ...

  2. 初步了解SequoiaDB数据库

    随着企业中日益复杂与多变的需求,以及迅速扩展带来的海量数据的业务,IT部门需要将越来越多的信息提供给用户,同时在现今的全球经济背景环境下,IT部 门还需要在提供高效服务的同时,降低其设备与程序维护成本 ...

  3. 自改xss小平台上线

    原先一直用xss.hk结果不知怎么被关的,正好手上有代码于是自己搭了一个,网上的类似的xss平台大多一样,原先的xss的chrome插件,不适合 "manifest_version" ...

  4. Oracle 中的replace函数的应用

    replace 函数用法如下: replace('将要更改的字符串','被替换掉的字符串','替换字符串') oracle 中chr()函数 CHR() --将ASCII码转换为字符 语法CHR(nu ...

  5. C# 命名参数【转】

    命名参数(Named Arguments)就是说在调用函数时可以通过指定参数名称的方式来调用参数.它最大的好处就是方便调用参数时按调用者的需要来排列顺序,而不必死守函数声明时的顺序(相对于“位置参数” ...

  6. jquery冒泡及阻止

    javascript, jquery的事件中都存在事件冒泡和事件捕获的问题,下面将两种问题及其解决方案做详细总结. 事件冒泡是一个从子节点向祖先节点冒泡的过程: 事件捕获刚好相反,是从祖先节点到子节点 ...

  7. libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.9-1.el6.x86_64

    版本:5.7.9 新装的CentOS 6.3 安装MySQL 5.7.9 出现的问题 1.首先卸载系统自带的mysql 5.1的包    yum   -y  remove   mysql-libs-5 ...

  8. 10、WPF程序集

    WPF核心程序集 PresentationCore.dll:这个程序集定义了许多构成WPF GUI层基础的类型.例如包含WPF Ink API(pc笔针输入,手写输入)的支持.几个动画基元以及几个图形 ...

  9. android控件之EditText

    EditText继承关系:View-->TextView-->EditTextEditText的属性很多,这里介绍几个:android:hint="请输入数字!"//设 ...

  10. Get current time and date on Android

    You could use: Calendar c =Calendar.getInstance();int seconds = c.get(Calendar.SECOND); There are pl ...