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.

Example

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.

Example

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的更多相关文章

  1. leetcode_1053. Previous Permutation With One Swap

    1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  3. LintCode "Previous Permutation"

    A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...

  4. Previous Permutation

    Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...

  5. 【leetcode】1053. Previous Permutation With One Swap

    题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...

  6. lintcode:previous permutation上一个排列

    题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...

  7. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  8. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  9. 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 ...

随机推荐

  1. mybatis中@Param的使用

    @Param:当映射器方法需要多个参数时,这个注解可以被用于:给映射器方法中的每个参数来取一个名字.否则,多参数将会以它们的顺序位置和SQL语句中的表达式进行映射,这是默认的.    语法要求:若使用 ...

  2. DPDK环境搭建

    一.环境要求  1.内核版本 >= 2.6.34:  2.glibc版本 >= 2.7 (ldd --version 查看glibc版本):  3.gcc版本 >= 4.9,一些gc ...

  3. 记一次java程序out of memory问题

    在一个比较大批量的pdf转String项目中遇到了:java.lang.OutOfMemoryError: Java heap space错误 第一反应肯定是程序没有写好,大量循环时没有把程序中没有用 ...

  4. idea建立web项目servlet映射的地址/jsp访问不到

    用IntelliJ IDEA 2017新建了一个web项目,成功运行后servlet mapping的地址一直访问不到 原因:在配置tomcat的时候Server中Open browser选项勾选之后 ...

  5. Bank Robbery LightOJ - 1163(推方程 注意计算机的计算方式)

    题意:一个数A,如果A去掉它的最后一位就变成了B,即B=A/10,给A - B,求A #include <iostream> #include <cstdio> #includ ...

  6. MT【144】托兰定理【图论】

    平面上$2n$个点$(n>1,n\in N)$,无三点共线,任意两点连线段,将其中任意$n^2+1$条线段染红色. 求证:三边都为红色的三角形至少有$\left[\dfrac{2}{3}(n+\ ...

  7. 【刷题】BZOJ 5008 方师傅的房子

    Description 方师傅来到了一个二维平面.他站在原点上,觉得这里风景不错,就建了一个房子.这个房子是n个点的凸多边形 ,原点一定严格在凸多边形内部.有m个人也到了这个二维平面.现在你得到了m个 ...

  8. 【ZJOI 2018】 历史(lct)

    历史 题目描述 九条可怜是一个热爱阅读的女孩子. 这个世界有 $n$ 个城市,这 $n$ 个城市被恰好 $n-1$ 条双向道路联通,即任意两个城市都可以互相到达.同时城市 $1$ 坐落在世界的中心,占 ...

  9. Codeforces 804D Expected diameter of a tree

    D. Expected diameter of a tree time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  10. CSS--overflow和hover

    一.Overflow overflow 属性规定当内容溢出元素框时发生的事情. 当插入的一张图片大小超过了元素本身大小,就会将元素撑大 <!DOCTYPE html> <html l ...