题目

下一个排列

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

样例

给出排列[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. hover和mouseover,mouseout的区别

    说道hover和mouseover,mouseout的区别,不得不联系到mouseenter,mouseleave. mouseover,mouseout是指鼠标指针在穿过/离开被选元素或其子元素时触 ...

  2. CSS3属性box-shadow使用教程

    CSS3的box-shadow属性可以让我们轻松实现图层阴影效果.我们来实战详解一下这个属性. 1. box-shadow属性的浏览器兼容性先来看一个这个属性的浏览器兼容性: Opera: 不知道是从 ...

  3. Laravel 5 基础(三)- 向视图传送数据(续)

    我们不仅仅可以向视图传送一个数据,同样我们可以传送Array public function about() { return view('pages.about')->with([ 'firs ...

  4. 11g RAC R2 体系结构---用户及用户组

    10.2 RAC 到11.2 RAC 用户及用户组的变化: 在10.2 RAC 的部署中,只需要一个用户(oracle)和一个用户组(dba).Database.Clusterware都是用oracl ...

  5. Python LOGGING使用方法

    Python LOGGING使用方法 1. 简介 使用场景 场景 适合使用的方法 在终端输出程序或脚本的使用方法 print 报告一个事件的发生(例如状态的修改) logging.info()或log ...

  6. 《.NET简单企业应用》项目开发环境

    项目开始,开发团队需要构建一套开发环境,主要包含:开发工具.代码管理/版本控制系统.任务和Bug管理系统和持续集成(CI)系统.本文主要列举项目开发中经常使用的开发工具和第三方库. 本文所列工具根据前 ...

  7. VS编译时自动下载NuGet管理的库

    之前一直使用NuGet来管理一些第三方的库,但是每次check in代码时候为了保证编译通过,都需要把对应的packages check in. 比较耗费时间,特别是往github上同步代码,而且这些 ...

  8. C++中的struct和class的区别

    C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据类型的数据结构了,它已经获取了太多的功能.struct能包含成员函数吗? 能!struct能继承吗? 能!!stru ...

  9. php服务器探针

    <?php /* ---------------------------------------------------- */ /* 程序名称: PHP探针-Yahei /* 程序功能: 探测 ...

  10. Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程

    生命在于折腾. 这个帖子作为我安装xilinx ISE 14.7版本一个记录.希望给需要的人一些帮助,这些内容绝大部分也是来源于互联网. 软硬件: lsb_release -a No LSB modu ...