17. Letter Combinations of a Phone Number

来源:互联网 发布:阿里云如何备份 编辑:程序博客网 时间:2024/06/06 00:07

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.

class Solution {public:vector<string> letterCombinations(string digits) {vector<string> phone = { "", "", "abc", "def", "ghi","jkl", "mno", "pqrs", "tuv", "wxyz" };if(digits=="") return res;string tmp;helper(phone, digits, tmp, 0);return res;}private:void helper(vector<string>& phone,string& digits,string& tmp,int cur){if (cur == digits.size()){res.push_back(tmp);return;}for (char c:phone[digits[cur]-'0']){tmp.push_back(c);helper(phone, digits, tmp, cur + 1);tmp.pop_back();}}vector<string> res;};


0 0