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 ...
随机推荐
- java 对象的解释过程
- 【Java并发编程】之一:可重入内置锁
每个Java对象都可以用做一个实现同步的锁,这些锁被称为内置锁或监视器锁.线程在进入同步代码块之前会自动获取锁,并且在退出同步代码块时会自动释放锁.获得内置锁的唯一途径就是进入由这个锁保护的同步代码块 ...
- 自平衡二叉(查找树/搜索树/排序树) binary search tree
在计算机科学中,AVL树是最先发明的自平衡二叉查找树.AVL树得名于它的发明者 G.M. Adelson-Velsky 和 E.M. Landis,他们在 1962 年的论文 "An alg ...
- 利用VRID/VMAC实现更安全的netscaler HA故障切换
利用VRID/VMAC实现更安全的netscaler HA故障切换 virtual MAC在故障切换(failover)中的作用. 在一个HA模式中,首要节点(primary node)拥有所有 ...
- linux 实践到的命令 collection
查看文件夹/文件 大小:du :(disk usage) 要通过 1024 字节块概述一个目录树及其每个子树的磁盘使用情况,请输入: du -k /home/fran/filename 这在/ho ...
- [LOJ3052] [十二省联考 2019] 春节十二响
题目链接 LOJ:https://loj.ac/problem/3052 洛谷:https://www.luogu.org/problemnew/show/P5290 BZOJ:https://www ...
- BZOJ 3282: Tree
3282: Tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1714 Solved: 765[Submit][Status][Discuss ...
- 【BZOJ1029】建筑抢修(贪心)
[BZOJ1029]建筑抢修(贪心) 题面 BZOJ 洛谷 题解 感觉自己已经不会贪心了. 很明显的一个想法是按照终止时间排序,然后能选则选. 但是这样子可能会因为前面选择了一个修理时间很长的,导致现 ...
- uoj318 [NOI2017]蔬菜 【贪心 + 堆 + 并查集】
题目链接 uoj 题解 以前看别人博客,在考场上用费用流做,一直以为这题是毒瘤网络流题 没想到竟然是贪心模拟题... 如果只有一个蔬菜呢?这就是一个经典的普及难度的贪心,正着推面临优先选择的困难,而逆 ...
- BZOJ2213 [Poi2011]Difference 【乱搞】
题目链接 BZOJ2213 题解 考虑任意一对点的贡献,单独拿出那些点所在位置 一个设为\(1\),一个设为\(-1\),从头到尾扫一遍维护前缀和,以及当前最小前缀和 两者相减更新答案 需要注意的是当 ...