首页 > 学院 > 开发设计 > 正文

LeetCode 17. Letter Combinations of a Phone Number

2019-11-08 18:28:42
字体:
来源:转载
供稿:网友

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"].answer:

class Solution {public:    vector<string> letterCombinations(string digits) {        vector<string> letter = {"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};        vector<string> result1,result2;        result1.push_back("");        for(int i = 0; i < digits.length(); i ++){            result2.clear();            vector<string>(result2).swap(result2);            int index = digits[i] - '0';            string temp = letter[index];            for(int j = 0; j < temp.length(); j ++){                for(int k = 0; k < result1.size(); k ++){                    result2.push_back(result1[k] + temp[j]);                }            }            result1 = result2;        }        return result2;    }};


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表