[LeetCode]17.Letter Combinations of a Phone Number,leetcode17.letter

來源:互聯網
上載者:User

[LeetCode]17.Letter Combinations of a Phone Number,leetcode17.letter
【題目】

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.

【分析】

無

【代碼】

/**********************************   日期:2015-01-22*   作者:SJF0115*   題目: 17.Letter Combinations of a Phone Number*   網址:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/*   結果:AC*   來源:LeetCode*   部落格:**********************************/#include <iostream>#include <vector>using namespace std;class Solution {public:    vector<string> letterCombinations(string digits) {        vector<string> vec;        if(digits.length() <= 0){            vec.push_back(digits);            return vec;        }//if        vector<char> number;        DFS(vec,digits,number);        return vec;    }private:    void DFS(vector<string> &vec,string digits,vector<char> &number){        string letters[] = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};        // 一種組合完成        int curLen = number.size();        if(curLen == digits.length()){            string letter;            for(int i = 0;i < curLen;++i){                letter += number[i];            }//for            vec.push_back(letter);            return;        }//if        // 數字        int num = digits[curLen] - '0';        // 數字所對應的字母長度        int len = letters[num].length();        for(int i = 0;i < len;++i){            number.push_back(letters[num][i]);            DFS(vec,digits,number);            number.pop_back();        }//for    }};int main(){    Solution solution;    string number = "2";    vector<string> result = solution.letterCombinations(number);    // 輸出    for(int i = 0;i < result.size();++i){        cout<<result[i]<<endl;    }//for    return 0;}


【代碼二】

class Solution {public:    vector<string> letterCombinations(string digits) {        vector<string> result;        DFS(digits,0,"",result);        return result;    }private:    void DFS(string digits,int cur,string path,vector<string> &result){        string keyboard[] = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};        if(cur == digits.length()){            result.push_back(path);            return;        }//if        int len = keyboard[digits[cur] - '0'].length();        for(int i = 0;i < len;++i){            char c = keyboard[digits[cur] - '0'][i];            DFS(digits,cur + 1,path + c,result);        }//for    }};



注意一種特殊情況:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.