22. Generate Parentheses

22. Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: Input: n = 1 Output: ["()"] Constraints: 1 <= n <= 8 Solution class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<>(); backtracking("(", 1, 0, n, res); return res; } void backtracking(String s, int open, int closed, int n, List<String> res) { if (open + closed == 2 * n) { res.add(s); return; } if (open < n) backtracking(s + "(", open + 1, closed, n, res); if (open > closed) backtracking(s + ")", open, closed + 1, n, res); } }

June 3, 2021 · 1 min · volyx

90. Subsets II

90. Subsets II Given an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: Input: nums = [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] Example 2: Input: nums = [0] Output: [[],[0]] Constraints: 1 <= nums.length <= 10 -10 <= nums[i] <= 10 Solution class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> res = new ArrayList<>(); List<Integer> current = new ArrayList<>(); Arrays.sort(nums); backtrack(nums, 0, current, res); return res; } void backtrack(int[] nums, int index, List<Integer> current, List<List<Integer>> res) { res.add(new ArrayList<>(current)); for (int i = index; i < nums.length; i++) { if (i > index && nums[i - 1] == nums[i]) { continue; } current.add(nums[i]); backtrack(nums, i + 1, current, res); current.remove(current.size() - 1); } } }

June 3, 2021 · 1 min · volyx

46. Permutations

46. Permutations Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique. Solution class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); backtrack(res, nums, 0); return res; } void backtrack(List<List<Integer>> res, int[] nums, int index) { if (index == nums.length - 1) { res.add(toList(nums)); return; } for (int i = index; i < nums.length; i++) { swap(nums, i, index); backtrack(res, nums, index + 1); swap(nums, i, index); } } List<Integer> toList(int[] nums) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < nums.length; i++) { list.add(nums[i]); } return list; } void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } Solution 2021-11-22 class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); permuteAt(0, nums, res); return res; } void permuteAt(int i, int[] nums, List<List<Integer>> res) { if (i == nums.length - 1) { res.add(toList(nums)); return; } for (int j = i; j < nums.length; j++) { swap(i, j, nums); permuteAt(i + 1, nums, res); swap(i, j, nums); } } void swap(int i, int j, int[] arr) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } List<Integer> toList(int[] values) { List<Integer> res = new ArrayList<>(values.length); for (int val : values) { res.add(val); } return res; } }

May 30, 2021 · 2 min · volyx

78. Subsets

78. Subsets Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Example 2: Input: nums = [0] Output: [[],[0]] Constraints: 1 <= nums.length <= 10 -10 <= nums[i] <= 10 All the numbers of nums are unique. Solution class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> subsets = new ArrayList<>(); int n = nums.length; for (int i = (int) Math.pow(2, n); i < (int) Math.pow(2, n + 1); i++) { // generate bitmasks from 00.000 to 111..111 String bitmask = Integer.toBinaryString(i).substring(1); List<Integer> curr = new ArrayList<>(); for (int j = 0; j < n; j++) { if (bitmask.charAt(j) == '1') curr.add(nums[j]); } subsets.add(curr); } return subsets; } public List<List<Integer>> subsets3(int[] nums) { List<List<Integer>> subsets = new ArrayList<>(); List<Integer> current = new ArrayList<>(); dfs(0, nums, current, subsets); return subsets; } void dfs(int index, int[] nums, List<Integer> current, List<List<Integer>> subsets) { if (index == nums.length) { subsets.add(new ArrayList<>(current)); return; } current.add(nums[index]); dfs(index + 1, nums, current, subsets); current.remove(current.size() - 1); dfs(index + 1, nums, current, subsets); } public List<List<Integer>> subsets2(int[] nums) { List<List<Integer>> subsets = new ArrayList<>(); List<Integer> current = new ArrayList<>(); generateSubsets(0, nums, current, subsets); return subsets; } void generateSubsets(int index, int[] nums, List<Integer> current, List<List<Integer>> subsets) { subsets.add(new ArrayList<>(current)); for (int i = index; i < nums.length; i++) { current.add(nums[i]); generateSubsets(i + 1, nums, current, subsets); current.remove(current.size() - 1); } } } Solution 2021-11-22 class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); List<Integer> curr = new ArrayList<>(); dfs(0, nums, res, curr); return res; } void dfs(int index, int[] nums, List<List<Integer>> res, List<Integer> curr) { if (index == nums.length) { res.add(List.copyOf(curr)); return; } curr.add(nums[index]); dfs(index + 1, nums, res, curr); curr.remove(curr.size() - 1); dfs(index + 1, nums, res, curr); } }

May 30, 2021 · 2 min · volyx

216. Combination Sum III

216. Combination Sum III Find all valid combinations of k numbers that sum up to n such that the following conditions are true: Only numbers 1 through 9 are used. Each number is used at most once. Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Explanation: 1 + 2 + 4 = 7 There are no other valid combinations. Example 2: Input: k = 3, n = 9 Output: [[1,2,6],[1,3,5],[2,3,4]] Explanation: 1 + 2 + 6 = 9 1 + 3 + 5 = 9 2 + 3 + 4 = 9 There are no other valid combinations. Example 3: Input: k = 4, n = 1 Output: [] Explanation: There are no valid combinations. [1,2,1] is not valid because 1 is used twice. Example 4: Input: k = 3, n = 2 Output: [] Explanation: There are no valid combinations. Example 5: Input: k = 9, n = 45 Output: [[1,2,3,4,5,6,7,8,9]] Explanation: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45 ​​​​​​​There are no other valid combinations. Constraints: ...

May 22, 2021 · 2 min · volyx

39. Combination Sum

39. Combination Sum Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order. The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input. ...

May 22, 2021 · 3 min · volyx

40. Combination Sum II

40. Combination Sum II Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. Each number in candidates may only be used once in the combination. Note: The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8 Output: [ [1,1,6], [1,2,5], [1,7], [2,6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5 Output: [ [1,2,2], [5] ] Constraints: ...

May 22, 2021 · 1 min · volyx

473. Matchsticks to Square

473. Matchsticks to Square You are given an integer array matchsticks where matchsticks[i] is the length of the ith matchstick. You want to use all the matchsticks to make one square. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time. Return true if you can make this square and false otherwise. Example 1: Input: matchsticks = [1,1,2,2,2] Output: true Explanation: You can form a square with length 2, one side of the square came two sticks with length 1. ...

May 22, 2021 · 2 min · volyx

494. Target Sum

494. Target Sum You are given an integer array nums and an integer target. You want to build an expression out of nums by adding one of the symbols ‘+’ and ‘-’ before each integer in nums and then concatenate all the integers. For example, if nums = [2, 1], you can add a ‘+’ before 2 and a ‘-’ before 1 and concatenate them to build the expression “+2-1”. Return the number of different expressions that you can build, which evaluates to target. ...

May 22, 2021 · 2 min · volyx