Journey of LeetCode|DAY 22
Preface
This is a new day to continue my backtracking algorithm journey.
Learn something new and keep reviewing what I learnt before.
1. Combination Sum III
LeetCode Link: 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.
Analysis and Solution
Recursion
LeetCode C++ as followings Recursion
class Solution {
private:
vector<vector<int>> result; // global variation; set that store results meet the criteria
vector<int> path; // global variation; store the single result meet the criteria
void backtracking(int targetSum, int k, int sum, int startIndex) {//recursion function with targetSum k and sum(process sum) startIndex(indicate next loop, incase repeating loop)
if (sum > targetSum) { // trim the branch
return; // return if path.size() == k BUT sum != targetSum
}
if (path.size() == k) {//terminate condition
if (sum == targetSum) result.push_back(path);
return;
}
for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) { // for loop with trim the branch
sum += i; // plus the value
path.push_back(i); // plus the value
backtracking(targetSum, k, sum, i + 1); // recursion i+1 assign to startIndex; next loop
sum -= i; // backtrack
path.pop_back(); // backtrack
}
}
public:
vector<vector<int>> combinationSum3(int k, int n) {//main function with two parameters
result.clear();
path.clear();
backtracking(n, k, 0, 1);//import the parameters
return result;
}
};
2. Letter Combinations of a Phone Number
LeetCode Link: 17. Letter Combinations of a Phone Number
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.
Analysis and Solution
Recursion
LeetCode C++ as followings Recursion
class Solution {
private:
const string letterMap[10] = {//build a map to store the numbers and letters with relationships;Two-dimensional array
"", // 0
"", // 1
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz", // 9
};
public:
vector<string> result;//define an array
string s;//define a string
void backtracking(const string& digits, int index) {//recursion function with string digits adn index as parameters; index indicates the number that was processed
if (index == digits.size()) {//terminate condition;
result.push_back(s);//collect the result
return;
}
int digit = digits[index] - '0'; // convert the number to int
string letters = letterMap[digit]; // Get the character set corresponding to the number
for (int i = 0; i < letters.size(); i++) {//for loop to traverse
s.push_back(letters[i]); // collect the result
backtracking(digits, index + 1); // recursion with next loop "index+1"
s.pop_back(); // backtrack
}
}
vector<string> letterCombinations(string digits) {
s.clear();
result.clear();
if (digits.size() == 0) {//remove the case0
return result;
}
backtracking(digits, 0);//import the parameters
return result;
}
};