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

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

        if(nums == null || nums.size() == 0)
            return nums;

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

        if(i == -1){
            reverse(nums, 0, nums.size() - 1);
            return nums;
        }

        int j = i + 1;
        for(int k = j; k < nums.size(); k++){
            if(nums.get(k) < nums.get(i) && nums.get(k) >= nums.get(j))
                j = k;
        }

        swap(nums, i, j);
        reverse(nums, i + 1, nums.size() - 1);

        return nums;
    }

    public void swap(ArrayList<Integer> nums, int i, int j){
        int temp = nums.get(i);
        nums.set(i, nums.get(j));
        nums.set(j, temp);

        return;
    }

    public void reverse(ArrayList<Integer> nums, int i, int j){
        while(i < j){
            swap(nums, i, j);
            i++;
            j--;
        }

        return;
    }

}
时间: 2024-11-07 15:41:48

lintcode-medium-Previous Permutation的相关文章

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

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

[Lintcode]52. Next Permutation

52. Next Permutation 本题难度: Medium Topic: Greedy Description 52. Next Permutation 本题难度: Medium Topic: Greedy Description Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Example Example 1: Input:[1] O

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

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

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium] digit counts

Digit Counts Count the number of k's between 0 and n. k can be 0 - 9. Example if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12) class Solution { /* * param k : As description. * param n : As description. * r

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium]Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return 2147483647 Example Given dividend = 100 and divisor = 9, return 11. public class Solution { /** * @param dividend the dividend