Leetcode 17. Letter Combinations of a Phone Number

题目大意:给你一串电话号码,输出可以由这串电话号码打出的所有字符串。

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 below. Note that 1 does not map to any letters.

Example:

Input: "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.

Difficulty:Medium

Category:

Solution

Solution 1: DFS

class Solution {
 public:
  vector<string> letterCombinations(string digits) {
    if (digits.empty()) return {};
    vector<string> ans;
    string cur;
    string dict[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    letterCombinationsDFS(digits, dict, 0, cur, ans);
    return ans;
  }
  void letterCombinationsDFS(const string& digits, string dict[], int index, string& out, vector<string>& ans) {
    if (index == digits.length()) {
      ans.emplace_back(out);
      return;
    }
    for (char c : dict[digits[index] - '0']) {
      out.push_back(c);
      letterCombinationsDFS(digits, dict, index + 1, out, ans);
      out.pop_back();
    }
  }
};

Solution 2: BSF

这一种解法来自于: 花花酱 LeetCode 17. Letter Combinations of a Phone Number

在这一种解法里面, 使用到了一种API. ans.swap(temp) , 可以用来交换两个 vector.

在这里需要的是將 ans 付一个初值, 这样才能够进入循环进行计算 for (char& digit : digits)

class Solution {
 public:
  vector<string> letterCombinations(string digits) {
    if (digits.empty()) return {};
    vector<string> ans = {""};
    string dict[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    for (char& digit : digits) {
      vector<string> temp;
      for (string s : ans) {
        for (char& c : dict[digit - '0']) {
          temp.emplace_back(s + c);
        }
      }
      ans.swap(temp);
    }
    return ans;
  }
};

必须在最开始给ans一个初始化的数值, 这样它才可以进入到循环里面,否则一直输出的都是空值. vector<string> ans = {""};

Update

03/01/2019 Review (BSF: 10 mins, DFS: 8mins)

By guozetang            Updated: 2020-09-19 13:02:30

results matching ""

    No results matching ""