Next Permutation & Previous Permutation
Next Permutation
Given a list of integers, which denote a permutation.
Find the next permutation in ascending order.
Notice
The list may contains duplicate integers.
For [1,3,2,3], the next permutation is [1,3,3,2]
For [4,3,2,1], the next permutation is [1,2,3,4]
Analysis:
In order to find the next permutation, we need to begin from the right most and find a number which is less than its right neighbor. And then switch it with the smallest number on its right side, but that smallest number must be greater than the number to be switched.
class Solution {
public void nextPermutation(int[] nums) {
int i = nums.length - ;
// 从最右边开始,首先找到一个值而且该值比它右边那个更小,这样我们可以把该值和它右边最小的值交换。
// example: 1329876, the next one is 1362789
while (i >= && nums[i + ] <= nums[i]) {
i--;
}
if (i >= ) {
int j = nums.length - ;
// we need to find the min value from i + 1 to j which is greater than nums[i]
while (j >= && nums[j] <= nums[i]) {
j--;
}
swap(nums, i, j);
}
reverse(nums, i + );
}
private void reverse(int[] nums, int start) {
int i = start, j = nums.length - ;
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
Previous Permutation
Given a list of integers, which denote a permutation.
Find the previous permutation in ascending order.
Notice
The list may contains duplicate integers.
For [1,3,2,3], the previous permutation is [1,2,3,3]
For [1,2,3,4], the previous permutation is [4,3,2,1]
Analysis:
From the right most, find a number which is greater than its right neighbor, then switch it with the largest number on its right side, but that largest number must be less than the number to be switched.
public class Solution {
/**
* @param nums: A list of integers
* @return: A list of integers that's previous permuation
*/
public ArrayList<Integer> previousPermuation(ArrayList<Integer> numss) {
if (numss == null || numss.size() <= )
return numss;
Integer[] nums = new Integer[numss.size()];
numss.toArray(nums);
int k = nums.length - ;
while (k >= && nums[k] <= nums[k + ]) {
k--;
}
// test case 211189
if (k != -) {
int p = nums.length -;
while (p > k) {
if (nums[k] > nums[p]) {
swap(nums, k, p);
break;
}
p--;
}
swapAll(nums, k + , nums.length - );
} else {
swapAll(nums, , nums.length - );
}
return new ArrayList<Integer>(Arrays.asList(nums));
}
public void swap(Integer[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public void swapAll(Integer[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
}
Next Permutation & Previous Permutation的更多相关文章
- leetcode_1053. Previous Permutation With One Swap
1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LintCode "Previous Permutation"
A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...
- Previous Permutation
Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...
- 【leetcode】1053. Previous Permutation With One Swap
题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...
- lintcode:previous permutation上一个排列
题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- lintcode :Permutation Index 排列序号
题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...
- Codeforces 612E - Square Root of Permutation
E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...
随机推荐
- There is no getter for property named 'notice' in 'class com.game.domain.Notices'
在插入数据时报错:There is no getter for property named 'notice' in 'class com.game.domain.Notices' 四月 11, 20 ...
- BZOJ2339 HNOI2011卡农(动态规划+组合数学)
考虑有序选择各子集,最后除以m!即可.设f[i]为选i个子集的合法方案数. 对f[i]考虑容斥,先只满足所有元素出现次数为偶数.确定前i-1个子集后第i个子集是确定的,那么方案数为A(2n-1,i-1 ...
- 学习Spring Boot:(二)启动原理
前言 主要了解前面的程序入口 @@SpringBootApplication 这个注解的结构. 正文 参考<SpringBoot揭秘 快速构建微服务体系>第三章的学习,总结下. Sprin ...
- 【BZOJ3609】人人尽说江南好(博弈论)
[BZOJ3609]人人尽说江南好(博弈论) 题面 BZOJ 洛谷 题解 昨天考试的时候,毒瘤出题人出了一个\(noip\)博弈十合一然后他就被阿鲁巴了,因为画面残忍,就不再展开. 这题是他的十合一中 ...
- 【APIO 2018】铁人两项(圆方树)
题目链接 题意大概是,求有多少三元组$(s,c,f)(s \neq c, c \neq f, s \neq f)$,满足从$s$到$f$有一条简单路径经过$c$. 得到结论: 点双中任意互不相同的三个 ...
- 面向对象高级编程(1)-使用__slots__
使用__slots__ 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> ...
- python学习(十九)常见的第三方库
原文链接:http://www.limerence2017.com/2017/12/28/python19/#more 介绍几个python中常见的第三方库. Pillow Pillow简称PIL,是 ...
- 【Asp.net入门4-04】使用Nuget
- Python入门 语法
Python入门 语法 语言介绍 对象,类型,值 编码规范 https://www.python.org/dev/peps/pep-0008/ 一.语言介绍 编程语言: 机器语言,汇编语言,高级语言 ...
- NATS_02:NATS消息通信模型
消息通信模型 NATS的消息通信是这样的:应用程序的数据被编码为一条消息,并通过发布者发送出去:订阅者接收到消息,进行解码,再处理.订阅者处理NATS消息可以是同步的或异步的. * 异步处理 异步处 ...