zoukankan      html  css  js  c++  java
  • 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.

      

      这道题不算难,想想就出来了。

    solution:

    vector<string> letterCombinations(string digits) {
        int n = digits.size();
        vector<string> res;
        if(n == 0)
            return res;
        res.push_back("");
        string numap[] = {"#","#","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        for (int i = 0;i < n;++i)
        {
            vector<string> tmp;
            for(int j = 0;j < res.size();++j)
                for(int k = 0;k < numap[digits[i]-'0'].size();++k)
                    tmp.push_back(res[j] + numap[digits[i]-'0'][k]);
            res = tmp;
        }
        return res;
    }

      此题还有其他解法,《编程之美》中也有相似题目,暂时先这样了。

  • 相关阅读:
    第五周学习进度条
    课堂实验4.1(环数组)
    每日站立会议(3)
    每日站立会议(2)
    找水王
    购买一批书的最低价格
    每日站立会议(1)
    NABCD分析
    团队开发博客
    返回一个二维整数数组中的最大子数组之和(环)
  • 原文地址:https://www.cnblogs.com/gattaca/p/4315884.html
Copyright © 2011-2022 走看看