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 permutation is [4,3,2,1]

函数头:public List<Integer> previousPermuation(List<Integer> nums)

算法:和前面的类似,就是反一反,关键是明白字典序排列是怎么产生的。原则是维持尽可能多的前缀,所以要尽量换后面的排列。所以从前往后就是在最后找尽量的不能动手脚的下降序列,然后对他们动手(就是迂回通过找最后面出现的上升对来实现,因为这个上升对后面肯定就是降序了,否则这一对不会是从后往前第一个找到的)。反过来如果从后字典序往前找,就是要找落在后头的尽量长的不能动手的上升序列(如果最后下降的话你直接换一下就可以得到前一个了啊),然后对他们动手。

本题具体步骤:

1.从后往前找第一个出现的下降对prev-next。

2.在next~最后中找出第一个比prev小的数字smaller。

3.交换prev和smaller。

4.在现在新的next~最后里排成降序(做逆序操作)

细节:1.对List数据结构操作的话,赋值记得用list.set(idx, obj),不要remove又add,这样很麻烦。当然list.get() = xxx这种对常数赋值的操作是肯定错误的。2.for(int i = 0, j = nums.length -1; ...; ),这里面同时创造ij并赋值,记得写一个int就好了,同行写两个不可能用逗号隔开啊,所以肯定不行的,基础语法。

public class Solution {
    /*
     * @param nums: A list of integers
     * @return: A list of integers that‘s previous permuation
     */
    public List<Integer> previousPermuation(List<Integer> nums) {

        //这里实现类型有讲究吗
        List<Integer> result = new ArrayList<Integer>(nums);

        int prev = 0;
        int next = 0;
        for (int i = nums.size() - 2; i >= 0; i--) {
            if (nums.get(i) > nums.get(i + 1)) {
                prev = i;
                next = i + 1;
                break;
            }
        }

        if (prev == next) {
            reverse(result, 0, result.size() - 1);
            return result;
        }

        int smaller = next;
        for (int i = nums.size() - 1; i > next; i--) {
            if (nums.get(i) < nums.get(prev)) {
                smaller = i;
                break;
            }
        }

        swap(result, prev, smaller);
        reverse(result, next, result.size() - 1);

        return result;
    }

    // 想看swap怎么写的,用set写的吗
    private void swap(List<Integer> nums, int idx1, int idx2) {
        int temp = nums.get(idx1);
        nums.set(idx1, nums.get(idx2));
        nums.set(idx2, temp);
    }

    private void reverse(List<Integer> nums, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            swap(nums, i, j);
        }
    }
}
时间: 2024-10-13 14:42:41

lintcode51- Previous Permutation- medium的相关文章

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

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

[array] leetcode - 31. Next Permutation - Medium

leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode_1053. Previous Permutation With One Swap

1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one-swap/ 题意:Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, t

LeetCode 1053. Previous Permutation With One Swap

原题链接在这里:https://leetcode.com/problems/previous-permutation-with-one-swap/ 题目: Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A

previous and next permutation

previous and next permutation 2763541 (找最后一个正序35) 2763541 (找3后面比3大的最后一个数4) 2764531 (交换3,4的位置) 2764135 (把4后面的5,3,1反转) 2764135 (找最后一个逆序41) 2764135 (找4后面比4小的最后一个数3) 2763145 (交换3,4的位置) 2763541 (把3后面的1,4,5反转) https://github.com/tongzhang1994/Facebook-Inte

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 permutatio

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

Leetcode分类解析:组合算法

Leetcode分类解析:组合算法 所谓组合算法就是指:在解决一些算法问题时,需要产生输入数据的各种组合.排列.子集.分区等等,然后逐一确认每种是不是我们要的解.从广义上来说,组合算法可以包罗万象,甚至排序.各种搜索算法都可以算进去.最近读<The Algorithm Design Manual>时了解到这种归类,上网一查,甚至有专门的书籍讲解,而且Knuth的巨著TAOCP的第四卷就叫组合算法,看来还真是孤陋寡闻了!于是最近着重专攻了一下Leetcode中所有相关题目,在此整理一下学习心得.

C++STL算法速查

  非变易算法 /* 第21章 非变易算法 Non-modifying sequence operations 21.0 advance, distance 为了了解模板,先了解一下这两个迭代器操作函数 21.1 逐个容器元素for_each for_each Apply function to range (template function) 21.2 查找容器元素find find Find value in range (function template) 21.3 条件查找容器元素f