LeetCode OJ Letter Combinations of a Phone Number

来源:互联网 发布:捡宝贝淘宝u站 编辑:程序博客网 时间:2024/06/17 04:35

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.

LeetCode OJ Letter Combinations of a Phone Number - Night - Night

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) {        possible_val[2] = "abc";        possible_val[3] = "def";        possible_val[4] = "ghi";        possible_val[5] = "jkl";        possible_val[6] = "mno";        possible_val[7] = "pqrs";        possible_val[8] = "tuv";        possible_val[9] = "wxyz";        go = digits;        dfs(0);        return ans;    }    void dfs(int pos) {        if (pos == go.size()) {            ans.push_back(one_ans);            return;        } else {            for (int i = 0; i < possible_val[go[pos] - '0'].size(); i++) {                one_ans += possible_val[go[pos] - '0'][i];                dfs(pos + 1);                one_ans.erase(pos, 1);            }        }    }private:    string go;    vector<string> ans;    string one_ans;    string possible_val[10];};



0 0