Lintcode: Previous Permuation

Given a list of integers, which denote a permutation.

Find the previous permutation in ascending order.

Note
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]

Next Permutation很像,只不过条件改成

for (int i=nums.lenth-2; i>=0; i--)

  if (nums[i] > nums[i+1]) break;

for (int j=i; j<num.length-1; j++)

  if (nums[j+1]>=nums[i]) break;

 1 public class Solution {
 2     /**
 3      * @param nums: A list of integers
 4      * @return: A list of integers that‘s previous permuation
 5      */
 6     public ArrayList<Integer> previousPermuation(ArrayList<Integer> nums) {
 7         // write your code
 8         if (nums==null || nums.size()==0) return nums;
 9         int i = nums.size()-2;
10         for (; i>=0; i--) {
11             if (nums.get(i) > nums.get(i+1)) break;
12         }
13         if (i >= 0) {
14             int j=i;
15             for (; j<=nums.size()-2; j++) {
16                 if (nums.get(j+1) >= nums.get(i)) break;
17             }
18             int temp = nums.get(j);
19             nums.set(j, nums.get(i));
20             nums.set(i, temp);
21         }
22         reverse(nums, i+1);
23         return nums;
24     }
25
26     public void reverse(ArrayList<Integer> nums, int k) {
27         int l = k, r = nums.size()-1;
28         while (l < r) {
29             int temp = nums.get(l);
30             nums.set(l, nums.get(r));
31             nums.set(r, temp);
32             l++;
33             r--;
34         }
35     }
36 }
时间: 2024-12-28 05:56:43

Lintcode: Previous Permuation的相关文章

Next Permutation &amp; 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. Example For [1,3,2,3], the next permutation is [1,3,3,2] For [4,3,2,1], the next per

全排列总结

接触全排列已经好长时间了,一直没有抽空总结一下全排列的相关问题,下面来说一下! 排列 一般地,从n个不同元素中取出m(m≤n)个元素,按照一定的顺序排成一列,叫做从n个元素中取出m个元素的一个排列(Arrangement).特别地,当m=n时,这个排列被称作全排列(Permutation). 排列数公式: 特别,当n==m时为全排列的公式! 下一个全排列算法 lintcode链接:http://www.lintcode.com/zh-cn/problem/next-permutation/ 样例

lintcode-medium-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 permutat

lintcode51- Previous Permutation- medium

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 permutat

[LintCode] Permuation Index

Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1. Example Given [1,2,4], return 1. class Solution { public: /** * @param

LintCode链表题总结

由于链表本身结构的单一性,链表的题目很少会有很大的变种,基本都是围绕几个基本的考点出题目.所以链表的题目比较好掌握,但是链表的题目又不太容易一次就AC通过,由于边界情况未考虑.空指针(比如head.next不存在但是却给head.next赋值了,就会抛出nullpointer的错误).越界等边界情况,我们需要在测试用例的时候多考虑边界条件.在模拟计算的时候一定要用纸和笔把中间的操作过程给画出来,这样比较容易形成思路. 在LintCode的ladder1中,链表那一章有如下这一些题目: 此外,Li

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

lintcode 66.67.68 二叉树遍历(前序、中序、后序)

AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The r