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.

题目大意:

  输入一个数字字符串,返回数字对应的每个按钮上所有字母的组合

思路:

  大体思路是以按键上的数字作为下标,数字键上的字母作为vector的值创建一个vector<string>,然后将传进来的数字字符串替换成数字键上的字母串,然后递归每一个数字字符串中的每一个数字替换之后的字母字符串,每递归一层就在临时字符串的相应位置添加上本次扫描的字符,如果递归到最后一层了则将现在的临时结果字符串添加到结果vector中。

代码:

class Solution {
public:
    std::vector<std::string> letterCombinations(std::string digits) {
        std::vector<std::string> result;
        //将手机的每个按键映射到以数字键为下标的数组中
        std::vector<std::string> phoneMap = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        const int size = digits.size();
        std::vector<std::string> tmp(size);   //将数字字符串替换成每个数字键上的字母

        if (size == 0) {
            return result;
        }

        for (int i = 0; i < size; ++i) {
            if (digits[i] == '0' || digits[i] == '1') {
                return result;
            }
            tmp[i] = phoneMap[digits[i] - '0'];
        }

        func(result, digits, tmp, size, 0);

        return result;
    }
private:
    void func(std::vector<std::string> &result, std::string &tar, std::vector<std::string> &tmp,const int &len, int pos)
    {
        if (pos == len) {
            result.push_back(tar);  //如果到最后一层了则将其压入结果vector
            return;
        }
        for (int i = 0; i < tmp[pos].size(); ++i) {
            tar[pos] = tmp[pos][i];
            func(result, tar, tmp, len, pos + 1);
        }
    }
};

 

时间: 2024-10-12 19:20:58

LeetCode之17----Letter Combinations of a Phone Number的相关文章

[LeetCode][Python]17: Letter Combinations of a Phone Number

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 17: Letter Combinations of a Phone Numberhttps://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ Given a digit string, return all possible letter combinations that the number c

【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", &q

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

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"Ou

Leetcode 17. Letter Combinations of a Phone Number(水)

17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given

刷题17. Letter Combinations of a Phone Number

一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" 二.我的做法 这个题目

Leetcode 17 Letter Combinations of a Phone Number - DFS思想

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", &q

LeetCode #17 Letter Combinations of a Phone Number (M)

[Problem] 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

LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译 给定一个数字字符串,返回所有这些数字可以表示的字母组合. 一个数字到字母的映射(就像电话按钮)如下图所示. 输入:数字字符串"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 备注:尽管以上答案是无序的,如果你想的话你的答案可以是

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"

LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)

Problem: 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&