LeetCode17. Letter Combinations of a Phone Number

来源:互联网 发布:linux搭建php开发环境 编辑:程序博客网 时间:2024/05/16 01:54

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”].

class Solution {private:    vector<string> res;    void letterCombinations(string digits,int b,string s,string d[]){        int n=digits.length();        if(b==n){            res.push_back(s);        }else{            for(int i=0;i<d[digits[b]-'0'].length();++i){                letterCombinations(digits,b+1,s+d[digits[b]-'0'][i],d);            }               }           }public:    vector<string> letterCombinations(string digits) {        string strs[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz",};        string s="";        int n=digits.length();        if(n==0) return res;        letterCombinations(digits,0,s,strs);        return res;    }};
0 0
原创粉丝点击