题目

下一个排列

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

样例

给出排列[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. c#键盘鼠标钩子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. 【原】使用ajax的get异常获取数据的时候,IE浏览器总是有缓存

    //HTML里有下面这样一段代码 //异步获取准备人信息 $.get("PrepSetpNew/PrepareMainCrew.ashx?Method=GetPrepUserInfo&quo ...

  3. 关于Silverlight调用天气预报接口问题

    问题:因Silverlight客户端不能直接调用webservice接口(外网天气接口),调用会出现跨域访问的问题,即使添加了跨域文件也不好使.解决方法如下 解决方法一:1.在服务端建立一个wcf服务 ...

  4. RHEL7 Ansible

    [root@promote tt]# rpm -iUvh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch ...

  5. 51nod1270 数组的最大代价(简单dp)

    ---恢复内容开始--- 1270 数组的最大代价 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 数组A包含N个 ...

  6. PHP中strtotime函数使用方法分享

    在PHP中有个叫做strtotime的函数.strtotime 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳.strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间 ...

  7. PHP获取当前日期或指定日期的所在月的第一天和最后一天

    <?php function getthemonth($date) { $firstday = date('Y-m-01', strtotime($date)); $lastday = date ...

  8. 左右滑动删除ListView条目Item--第三方开源--SwipeToDismiss

    Android的SwipeToDismiss是github上一个第三方开源框架(github上的项目链接地址:https://github.com/romannurik/Android-SwipeTo ...

  9. (转载)delphi文件流

    delphi文件流 [复制链接] 在Delphi中,所有流对象的基类为TStream类,其中定义了所有流的共同属性和方法. TStream类中定义的属性介绍如下: 1.Size: 此属性以字节返回流中 ...

  10. MvvmCross for WPF File Plugin

    本文以MvvmCross为框架基础 最近用了File Plugin插件,一开始也是没用明白,写一下记录下来,也方便需要的人吧 首先这个File Plugin需要先在UI项目里创建一个Bootstrap ...