[LeetCode]17.Letter Combinations of a Phone Number

简介:

【题目】

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



注意一种特殊情况:



目录
相关文章
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
105 1
|
6月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
7月前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
54 0
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
42 0
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero