|
1 | 1 | ## Algorithm
|
2 | 2 |
|
| 3 | +[46. Permutations](https://leetcode.com/problems/permutations/) |
| 4 | + |
3 | 5 | ### Description
|
4 | 6 |
|
5 |
| -### Solution |
| 7 | +Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. |
| 8 | + |
| 9 | +Example 1: |
| 10 | + |
| 11 | +``` |
| 12 | +Input: nums = [1,2,3] |
| 13 | +Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] |
| 14 | +``` |
| 15 | + |
| 16 | +Example 2: |
| 17 | + |
| 18 | +``` |
| 19 | +Input: nums = [0,1] |
| 20 | +Output: [[0,1],[1,0]] |
| 21 | +``` |
| 22 | + |
| 23 | +Example 3: |
| 24 | + |
| 25 | +``` |
| 26 | +Input: nums = [1] |
| 27 | +Output: [[1]] |
| 28 | +``` |
| 29 | + |
| 30 | +Constraints: |
6 | 31 |
|
7 |
| -```java |
| 32 | +- 1 <= nums.length <= 6 |
| 33 | +- -10 <= nums[i] <= 10 |
| 34 | +- All the integers of nums are unique. |
8 | 35 |
|
| 36 | +### Solution |
| 37 | + |
| 38 | +```java |
| 39 | +class Solution { |
| 40 | + public List<List<Integer>> permute(int[] nums) { |
| 41 | + List<List<Integer>> lists = new ArrayList<List<Integer>>(); |
| 42 | + helper(nums, 0, lists); |
| 43 | + return lists; |
| 44 | + } |
| 45 | + private void helper(int[] nums, int start, List<List<Integer>> lists){ |
| 46 | + if(start==nums.length){ |
| 47 | + List<Integer> temp = new ArrayList<>(); |
| 48 | + for(int num:nums){ |
| 49 | + temp.add(num); |
| 50 | + } |
| 51 | + lists.add(temp); |
| 52 | + }else{ |
| 53 | + for(int i=start;i<nums.length;i++){ |
| 54 | + swap(nums, i, start); |
| 55 | + helper(nums, start+1, lists); |
| 56 | + swap(nums, i, start); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + public static void swap(int[] nums, int start, int end){ |
| 61 | + int temp = nums[start]; |
| 62 | + nums[start] = nums[end]; |
| 63 | + nums[end] = temp; |
| 64 | + } |
| 65 | +} |
9 | 66 | ```
|
10 | 67 |
|
11 | 68 | ### Discuss
|
12 | 69 |
|
| 70 | +Subsets : https://leetcode.com/problems/subsets/ |
| 71 | + |
| 72 | +```Java |
| 73 | +public List<List<Integer>> subsets(int[] nums) { |
| 74 | + List<List<Integer>> list = new ArrayList<>(); |
| 75 | + Arrays.sort(nums); |
| 76 | + backtrack(list, new ArrayList<>(), nums, 0); |
| 77 | + return list; |
| 78 | +} |
| 79 | + |
| 80 | +private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){ |
| 81 | + list.add(new ArrayList<>(tempList)); |
| 82 | + for(int i = start; i < nums.length; i++){ |
| 83 | + tempList.add(nums[i]); |
| 84 | + backtrack(list, tempList, nums, i + 1); |
| 85 | + tempList.remove(tempList.size() - 1); |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +Subsets II (contains duplicates) : https://leetcode.com/problems/subsets-ii/ |
| 91 | + |
| 92 | +```java |
| 93 | +public List<List<Integer>> subsetsWithDup(int[] nums) { |
| 94 | + List<List<Integer>> list = new ArrayList<>(); |
| 95 | + Arrays.sort(nums); |
| 96 | + backtrack(list, new ArrayList<>(), nums, 0); |
| 97 | + return list; |
| 98 | +} |
| 99 | + |
| 100 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start){ |
| 101 | + list.add(new ArrayList<>(tempList)); |
| 102 | + for(int i = start; i < nums.length; i++){ |
| 103 | + if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates |
| 104 | + tempList.add(nums[i]); |
| 105 | + backtrack(list, tempList, nums, i + 1); |
| 106 | + tempList.remove(tempList.size() - 1); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Permutations : https://leetcode.com/problems/permutations/ |
| 112 | + |
| 113 | +```java |
| 114 | +public List<List<Integer>> permute(int[] nums) { |
| 115 | + List<List<Integer>> list = new ArrayList<>(); |
| 116 | + // Arrays.sort(nums); // not necessary |
| 117 | + backtrack(list, new ArrayList<>(), nums); |
| 118 | + return list; |
| 119 | +} |
| 120 | + |
| 121 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){ |
| 122 | + if(tempList.size() == nums.length){ |
| 123 | + list.add(new ArrayList<>(tempList)); |
| 124 | + } else{ |
| 125 | + for(int i = 0; i < nums.length; i++){ |
| 126 | + if(tempList.contains(nums[i])) continue; // element already exists, skip |
| 127 | + tempList.add(nums[i]); |
| 128 | + backtrack(list, tempList, nums); |
| 129 | + tempList.remove(tempList.size() - 1); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +Permutations II (contains duplicates) : https://leetcode.com/problems/permutations-ii/ |
| 136 | + |
| 137 | +```java |
| 138 | +public List<List<Integer>> permuteUnique(int[] nums) { |
| 139 | + List<List<Integer>> list = new ArrayList<>(); |
| 140 | + Arrays.sort(nums); |
| 141 | + backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]); |
| 142 | + return list; |
| 143 | +} |
| 144 | + |
| 145 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, boolean [] used){ |
| 146 | + if(tempList.size() == nums.length){ |
| 147 | + list.add(new ArrayList<>(tempList)); |
| 148 | + } else{ |
| 149 | + for(int i = 0; i < nums.length; i++){ |
| 150 | + if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue; |
| 151 | + used[i] = true; |
| 152 | + tempList.add(nums[i]); |
| 153 | + backtrack(list, tempList, nums, used); |
| 154 | + used[i] = false; |
| 155 | + tempList.remove(tempList.size() - 1); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +Combination Sum : https://leetcode.com/problems/combination-sum/ |
| 162 | + |
| 163 | +```java |
| 164 | +public List<List<Integer>> combinationSum(int[] nums, int target) { |
| 165 | + List<List<Integer>> list = new ArrayList<>(); |
| 166 | + Arrays.sort(nums); |
| 167 | + backtrack(list, new ArrayList<>(), nums, target, 0); |
| 168 | + return list; |
| 169 | +} |
| 170 | + |
| 171 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){ |
| 172 | + if(remain < 0) return; |
| 173 | + else if(remain == 0) list.add(new ArrayList<>(tempList)); |
| 174 | + else{ |
| 175 | + for(int i = start; i < nums.length; i++){ |
| 176 | + tempList.add(nums[i]); |
| 177 | + backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can reuse same elements |
| 178 | + tempList.remove(tempList.size() - 1); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +Combination Sum II (can't reuse same element) : https://leetcode.com/problems/combination-sum-ii/ |
| 185 | + |
| 186 | +```java |
| 187 | +public List<List<Integer>> combinationSum2(int[] nums, int target) { |
| 188 | + List<List<Integer>> list = new ArrayList<>(); |
| 189 | + Arrays.sort(nums); |
| 190 | + backtrack(list, new ArrayList<>(), nums, target, 0); |
| 191 | + return list; |
| 192 | + |
| 193 | +} |
| 194 | + |
| 195 | +private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){ |
| 196 | + if(remain < 0) return; |
| 197 | + else if(remain == 0) list.add(new ArrayList<>(tempList)); |
| 198 | + else{ |
| 199 | + for(int i = start; i < nums.length; i++){ |
| 200 | + if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates |
| 201 | + tempList.add(nums[i]); |
| 202 | + backtrack(list, tempList, nums, remain - nums[i], i + 1); |
| 203 | + tempList.remove(tempList.size() - 1); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +Palindrome Partitioning : https://leetcode.com/problems/palindrome-partitioning/ |
| 210 | + |
| 211 | +```java |
| 212 | +public List<List<String>> partition(String s) { |
| 213 | + List<List<String>> list = new ArrayList<>(); |
| 214 | + backtrack(list, new ArrayList<>(), s, 0); |
| 215 | + return list; |
| 216 | +} |
| 217 | + |
| 218 | +public void backtrack(List<List<String>> list, List<String> tempList, String s, int start){ |
| 219 | + if(start == s.length()) |
| 220 | + list.add(new ArrayList<>(tempList)); |
| 221 | + else{ |
| 222 | + for(int i = start; i < s.length(); i++){ |
| 223 | + if(isPalindrome(s, start, i)){ |
| 224 | + tempList.add(s.substring(start, i + 1)); |
| 225 | + backtrack(list, tempList, s, i + 1); |
| 226 | + tempList.remove(tempList.size() - 1); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +public boolean isPalindrome(String s, int low, int high){ |
| 233 | + while(low < high) |
| 234 | + if(s.charAt(low++) != s.charAt(high--)) return false; |
| 235 | + return true; |
| 236 | +} |
| 237 | +``` |
| 238 | + |
13 | 239 | ## Review
|
14 | 240 |
|
15 | 241 |
|
|
0 commit comments