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

Source: Internet
Author: User

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

Given a digit string, return all possible letter combinations that the number coshould 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 cocould be in any order you want.

[Analysis]

None

[Code]

/********************************** Date: * Author: SJF0115 * Subject: 17. letter Combinations of a Phone Number * URL: https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/* result: AC * Source: LeetCode * blog: * *********************************/# 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"}; // a combination of 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 // number int num = digits [curLen]-'0 '; // The letter length corresponding to the number 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); // output for (int I = 0; I <result. size (); ++ I) {cout <result [I] <endl ;}// for return 0 ;}


Code 2]

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    }};



Note a special situation:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.