Subsets II
Subsets IILeetcode 90.
123456789101112131415161718192021222324252627282930313233343536373839class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<Integer> list = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>(); boolean[] visited = new boolean[nums.length]; Arrays.sort(nums); for(int i = 0; i <= nums.length;i++){ backtricking(nums,list,ans,0,i,visited); } ...
Subsets
Subsets Leetcode 78.
123456789101112131415161718192021222324252627282930313233343536class Solution { public List<List<Integer>> subsets(int[] nums) { List<Integer> list = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>(); ans.add(list); boolean[] visited = new boolean[nums.length]; for(int i = 1; i <= nums.length;i++){ backtricking(nums,list,ans,0,i,visited); } ...
Combination Sum III
Combination Sum IIILeetcode 216.
123456789101112131415161718192021222324252627282930313233343536373839class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<Integer> list = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>(); int[] nums = new int[9]; for(int i = 1; i < 10;i++){ nums[i-1] = i; } boolean[] visited = new boolean[nums.length]; backtric ...
Combination Sum II
Combination Sum IILeetcode 40.
find all unique combinations in candidates through pruning:
key steps:
sort candidates first
//pruningif (i != 0 && candidates[i] == candidates[i - 1] && !visited[i - 1]) continue;
1234567891011121314151617181920212223242526272829303132333435363738class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<Integer> list = new ArrayList<>(); List<List ...
Combination Sum
Combination SumLeetcode 39.
In order to achieve the unique combinations of candidates, for loop should start with the specified value named start. This operation is usually called Pruning
1234567891011121314151617181920212223242526272829303132class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> ans = new ArrayList<>(); List<Integer> list = new ArrayList<>(); ...
Java
Java common operationsListList Reverse 1234List<Integer> list = new ArrayList<>();list.add(1);list.add(2);Collections.reverse(list); //list: [2,1]
List contains12345List<Integer> ans = new ArrayList<>();//add element ans.add(1);ans.add(2);boolean result = ans.contains(1); // true
List Shallow cloneFor the reference data type, the copy is its reference, and a new object is not created, that is, no new memory space is allocated. Such a copy is called a shallow copy.
Sha ...
Combinations
CombinationsLeetcode 77.
12345678910111213141516171819202122232425262728293031class Solution { public List<List<Integer>> combine(int n, int k) { int[] nums = new int[n]; for(int i = 1;i <= n;i++){ nums[i -1] = i; } List<List<Integer>> ans = new ArrayList<>(); List<Integer> list = new ArrayList<>(); backtricking(nums,k,ans,list,0); return ans; } public void backtricki ...
Permutations II
Permutations IILeetcode 47.
123456789101112131415161718192021222324252627282930313233343536373839404142434445class Solution { public List<List<Integer>> permuteUnique(int[] nums) { int numsLength = nums.length; List<List<Integer>> ans = new ArrayList<>(); List<Integer> list = new ArrayList<Integer>(); if(numsLength == 1){ list.add(nums[0]); ans.add(list); return ans; } boolean[] vis ...
Permutations
PermutationsLeetcode 46.
12345678910111213141516171819202122232425262728293031323334353637383940class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); int numsLength = nums.length; if(numsLength == 1){ List<Integer> list = new ArrayList<Integer>(); list.add(nums[0]); ans.add(list); return ans; } List<Integer> store ...
Binary Tree Paths
Binary Tree PathsLeetcode 257.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * ...