216. Combination Sum III
Link: https://leetcode.com/problems/combination-sum-iii/
Description
Find all valid combinations of k
numbers that sum up to n
such that the following conditions are true:
- Only numbers
1
through9
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.
Approach
- Initialize two global variables
result
(store the valid combinations) andpath
(store the current combination). - Initialize a recursive function with parameters
k
,n
,sum
(store the current sum of the path) andstartIndex
(store the current starting integer). - If the
sum
larger thantarget
, which means thesum
cannot equal ton
, return. - If the size of
path
equals tok
and thesum
equals totarget
, add thepath
to theresult
. - For integers in
[startIndex, 9 - (k - path.size()) + 1]
: - Add the current integer to
path
and recursively call the function withsum
plus the current integer and newstartIndex
as the current integer plus 1. - After the recursive call, the last element of the path list is removed to backtrack and explore other possible combinations.
Solution
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTracking(k, 1, 0, n);
return result;
}
private void backTracking(int k, int startIndex, int sum, int target) {
if (sum > target)
return;
if (path.size() == k) {
if (sum == target)
result.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
path.add(i);
backTracking(k, i + 1, sum + i, target);
path.remove(path.size() - 1);
}
}
}
17. Letter Combinations of a Phone Number
Link: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
Description
Given a string containing digits from 2-9
inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Approach
- Initialize a global varaible
result
to store the valid combinations. - Initialize a String array
nums
to represent the mapping of digits to letters accroding to the value of digits. - Intialize a recursive function with parameters
digits
,nums
,n
(represent the current index indigits
) andsb
(store the current combination). - If the length of
sb
equals to the length ofdigits
, add the String ofsb
toresult
. - Find the set of letter corresponds to
n
. - The function then iterates through each letter in the set of letters and appends it to the
sb
. - Then recursively call the function with updated
n
(n + 1
). - After the recursive call, remove the last element in
sb
.
Solution
class Solution {
List<String> result = new ArrayList<>();
public List<String> letterCombinations(String digits) {
if (digits.length() == 0)
return result;
String[] nums = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
backtracking(digits, nums, 0, new StringBuilder());
return result;
}
private void backtracking(String digits, String[] nums, int n, StringBuilder sb) {
if (n == digits.length()) {
result.add(sb.toString());
return;
}
String d = nums[digits.charAt(n) - '0'];
for (int i = 0; i < d.length(); i++) {
sb.append(d.charAt(i));
backtracking(digits, nums, n + 1, sb);
sb.deleteCharAt(sb.length() - 1);
}
}
}