Letter Combinations of a Phone Number
2014-06-08 22:51
253 查看
Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
题目很简单了,就是按每一位遍历,组合。
vector<string> letterCombinations(string digits) {
vector<string> vec, phone(10);
phone[2] = "abc";
phone[3] = "def";
phone[4] = "ghi";
phone[5] = "jkl";
phone[6] = "mno";
phone[7] = "pqrs";
phone[8] = "tuv";
phone[9] = "wxyz";
letters(phone, 0, "", vec, digits);
return vec;
}
void letters(vector<string> &phone, int num, string str, vector<string> &vec, string digits){
if(num == digits.size()){
vec.push_back(str);
return;
}
int c = digits[num] - '0';
for(int i = 0; i < phone[c].size(); i++){
letters(phone, num+1, str + phone[c][i], vec, digits);
}
}
相关文章推荐
- leetcode--Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- leetcode之Letter Combinations of a Phone Number
- LeetCode 17.Letter Combinations of a Phone Number
- leetcode--17. Letter Combinations of a Phone Number
- Leetcode_letter-combinations-of-a-phone-number
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number(middle)
- LeetCode: Letter Combinations of a Phone Number [018]
- LeetCode Letter Combinations of a Phone Number
- Leetcode: 17. Letter Combinations of a Phone Number
- [leedcode 17] Letter Combinations of a Phone Number
- Leetcode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- letter-combinations-of-a-phone-number
- Letter Combinations of a Phone Number
- LeetCode 17. Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number--LeetCode