@@ -0,0 +1,1652 @@
+package Permutations;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;%0A%0A/**%0A * Given a collection of distinct numbers, return all possible permutations.%0A *%0A * %3Cpre%3E%0A * For example,%0A * %5B1,2,3%5D have the following permutations:%0A * %5B%0A * %5B1,2,3%5D,%0A * %5B1,3,2%5D,%0A * %5B2,1,3%5D,%0A * %5B2,3,1%5D,%0A * %5B3,1,2%5D,%0A * %5B3,2,1%5D%0A * %5D%0A * %3C/pre%3E%0A */%0Apublic class Solution %7B%0A%0A    private boolean permuteNums(int%5B%5D nums) %7B%0A        int n = nums.length;%0A        int minmium = -1, loc = -1;%0A%0A        for (int i = n - 1; i %3E= 0; i--) %7B%0A            for (int j = i + 1; j %3C n; j++) %7B%0A                if (nums%5Bi%5D %3C nums%5Bj%5D && (loc == -1 %7C%7C minmium %3E nums%5Bj%5D)) %7B%0A                    minmium = nums%5Bj%5D;%0A                    loc = j;%0A                %7D%0A            %7D%0A            if (loc %3E -1) %7B%0A                int t = nums%5Bi%5D;%0A                nums%5Bi%5D = nums%5Bloc%5D;%0A                nums%5Bloc%5D = t;%0A                Arrays.sort(nums, i + 1, n);%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    private List%3CInteger%3E array2List(int%5B%5D nums) %7B%0A        return Arrays.stream(nums).boxed().collect(Collectors.toList());%0A    %7D%0A%0A    public List%3CList%3CInteger%3E%3E permute(int%5B%5D nums) %7B%0A        int n = nums.length;%0A        List%3CList%3CInteger%3E%3E permutations = new ArrayList%3C%3E();%0A        if (n == 0) %7B%0A            return permutations;%0A        %7D%0A        Arrays.sort(nums);%0A        permutations.add(array2List(nums));%0A%0A        while (true) %7B%0A            // Find next permutation%0A            if (!permuteNums(nums)) break;%0A            permutations.add(array2List(nums));%0A        %7D%0A        return permutations;%0A    %7D%0A%7D

46. Permutations