LeetCode 46. Permutations
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
public class Permutations { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> list = new ArrayList<>() ; //Arrays.sort(nums); List<Integer> temp = new ArrayList<>(); backtrack(list,temp,nums); return list; } private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums){ if(temp.size()==nums.length) { list.add(new ArrayList<>(temp)); }else { for(int i=0;i<nums.length;i++){ if(temp.contains(nums[i])) continue; temp.add(nums[i]); backtrack(list,temp,nums); temp.remove(temp.size()-1); } } } }
47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:
[ [1,1,2], [1,2,1], [2,1,1] ]
131. Palindrome Partitioningpublic class PermutationsII { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> list = new ArrayList<>() ; Arrays.sort(nums); List<Integer> temp = new ArrayList<>(); backtrack(list,temp,nums,new boolean[nums.length]); return list; } private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums,boolean[] used){ if(temp.size()==nums.length) { list.add(new ArrayList<>(temp)); }else { for(int i=0;i<nums.length;i++){ //if(temp.contains(nums[i]) || nums[i]==nums[i+1]) continue; if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue; used[i]=true; temp.add(nums[i]); backtrack(list,temp,nums,used); used[i]=false; temp.remove(temp.size()-1); } } } }
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]
public class PalindromePartitioning { List<List<String>> res; List<String> cur; public List<List<String>> partition(String s) { res=new ArrayList<List<String>>(); cur = new ArrayList<String>(); backTrack(s,0); return res; } public void backTrack(String a,int l){ if(cur.size()>0&& l>=a.length()) res.add(new ArrayList<>(cur)); for(int i=l;i<a.length();i++){ if(isPalindrome(a,l,i)){ if(i==l){ cur.add(Character.toString(a.charAt(i))); }else{ cur.add(a.substring(l,i+1)); } backTrack(a,i+1); cur.remove(cur.size()-1); } } } public boolean isPalindrome(String str, int l, int r){ if(l==r) return true; while(l<r){ if(str.charAt(l)!=str.charAt(r)) return false; l++;r--; } return true; } }
78. Subsets
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
public class Subsets { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> list = new ArrayList<>() ; List<Integer> temp = new ArrayList<>(); backtrack(list,temp,nums,0); return list; } private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums,int start){ list.add(new ArrayList<>(temp)); for(int i=start;i<nums.length;i++){ temp.add(nums[i]); backtrack(list,temp,nums,i+1); temp.remove(temp.size()-1); } } }
90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
public class SubsetsII { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> list = new ArrayList<>() ; Arrays.sort(nums); List<Integer> temp = new ArrayList<>(); backtrack(list,temp,nums,0); return list; } private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums,int start){ list.add(new ArrayList<>(temp)); for(int i=start;i<nums.length;i++){ if(i > start && nums[i] == nums[i-1]) continue; temp.add(nums[i]); backtrack(list,temp,nums,i+1); temp.remove(temp.size()-1); } } }