반응형
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
각 digit에 매핑되는 letters를 전처리해두고, combination을 구해주었습니다.
class Solution {
public:
string letters[10] = {"", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};
vector<string> ret;
string comb = "";
vector<string> letterCombinations(string digits) {
if(digits.size()) letterCombinationsInternal(digits, 0);
return ret;
}
private:
void letterCombinationsInternal(string& digits, int idx) {
if(idx == digits.size()) {
ret.push_back(comb);
return;
}
int lettersIdx = digits[idx] - '0';
int sz = letters[lettersIdx].size();
for(int i=0; i<sz; i++) {
comb.push_back(letters[lettersIdx][i]);
letterCombinationsInternal(digits, idx + 1);
comb.pop_back();
}
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 743 : Network Delay Time (0) | 2022.05.14 |
---|---|
LeetCode 1641 : Count Sorted Vowel Strings (0) | 2022.05.11 |
LeetCode 341 : Flatten Nested List Iterator (1) | 2022.05.08 |
LeetCode 456 : 132 Pattern (0) | 2022.05.07 |
LeetCode 1209 : Remove All Adjacent Duplicates in String II (0) | 2022.05.06 |