@@ -0,0 +1,1765 @@
+package org.speedfirst.leetcode.combination;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0A%0A/**%0A * Created by jiankuan on 1/22/15.%0A */%0Apublic class PreviousPermutation %7B%0A%0A    /**%0A     * @param nums: A list of integers%0A     * @return: A list of integers that's previous permuation%0A     */%0A    public ArrayList%3CInteger%3E previousPermuation(ArrayList%3CInteger%3E nums) %7B%0A        // write your code%0A        if (nums.isEmpty() %7C%7C nums.size() == 1) %7B%0A            return nums;%0A        %7D%0A%0A        int i;%0A        for (i = nums.size() - 2; i %3E= 0; i--) %7B%0A            if (nums.get(i) %3E nums.get(i + 1)) %7B%0A                break;%0A            %7D%0A        %7D%0A%0A        int pos1 = -1, pos2;%0A        if (i %3E= 0) %7B%0A            pos1 = i;%0A%0A            for (i = nums.size() - 1; i %3E pos1; i--) %7B%0A                if (nums.get(i) %3C  nums.get(pos1)) %7B%0A                    break;%0A                %7D%0A            %7D%0A            pos2 = i;%0A            // swap pos1 and pos2%0A            swap(nums, pos1, pos2);%0A        %7D%0A%0A        // reverse %5Bpos + 1, end of nums%5D%0A        int s = pos1 + 1;%0A        int e = nums.size() - 1;%0A        while (s %3C e) %7B%0A            swap(nums, s, e);%0A            s++;%0A            e--;%0A        %7D%0A%0A        return nums;%0A    %7D%0A%0A    private void swap(ArrayList%3CInteger%3E nums, int pos1, int pos2) %7B%0A        int tmp = nums.get(pos1);%0A        nums.set(pos1, nums.get(pos2));%0A        nums.set(pos2, tmp);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        PreviousPermutation pp = new PreviousPermutation();%0A        ArrayList%3CInteger%3E nums = new ArrayList(Arrays.asList(1, 2, 5, 3, 4));%0A        System.out.println(pp.previousPermuation(nums));%0A        nums = new ArrayList%3C%3E(Arrays.asList(1, 2, 3, 4, 5));%0A        System.out.println(pp.previousPermuation(nums));%0A    %7D%0A%7D%0A

add previous permutation