[Leetcode]_17 Letter Combinations of a Phone Number
/**
* Index: 17
* Title: Letter Combinations of a Phone Number
* Author: ltree98
**/
這道題應該是用遞迴來做
digits.length() > 1 => 將 digits[0]對應的集合 與 digits.substr(1)返回的集合連線
digits.length() == 1 => 返回數字對應的字串集合
然後再處理一些無效值的情況:
- 數字中 0, 1, *, # 等其他值。 —— 這裡用他們 ASCII碼的差值來判斷。
- 如果輸入的數字中存在無效值,整個返回空。 —— 這裡判斷後面字串的返回值是否為空,若為空,直接返回空。
class Solution {
private:
string map[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public:
vector<string> letterCombinations(string digits) {
if(digits.length() == 0) return {};
int temp = digits[0] - '0';
if( temp < 2 || temp > 9 ) return {};
vector<string> ans = {};
string mapStr = map[temp];
if(digits.length() > 1) {
vector<string> preAns = letterCombinations(digits.substr(1));
if(preAns.size() == 0) return {};
for(int i = 0; i < mapStr.length(); i++) {
for (int j = 0; j < preAns.size(); j++) {
ans.push_back(mapStr[i]+preAns[j]);
}
}
}
else {
for(int i = 0; i < mapStr.length(); i++) {
ans.push_back(mapStr.substr(i, 1));
}
}
return ans;
}
};
相關推薦
[Leetcode]_17 Letter Combinations of a Phone Number
/** * Index: 17 * Title: Letter Combinations of a Phone Number * Author: ltree98 **/ 這道題應該是
Leetcode 17. Letter Combinations of a Phone number
res bsp self. col join lee num nat leetcode 求給出的數字串,如果按照電話鍵盤的編譯方式,可以給出多少那些對應的數字組合。例如: Input:Digit string "23" Output: ["ad", "ae", "af"
[LeetCode][Java] Letter Combinations of a Phone Number
article art present net pub spa line note not 題目: Given a digit string, return all possible letter combinations that the number
[leetcode]17. Letter Combinations of a Phone Number手機鍵盤的字母組合
poi clu char evel 個數字 The let alt input Given a string containing digits from 2-9 inclusive, return all possible letter combinations that
leetcode 17-Letter Combinations of a Phone Number(medium)
shm put note 下標 維護 nta style == emp Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the
leetcode#17. Letter Combinations of a Phone Number
commons 註意 構建 media ber str http wiki pan 給定一個僅包含數字 2-9 的字符串,返回所有它能表示的字母組合。 給出數字到字母的映射如下(與電話按鍵相同)。註意 1 不對應任何字母。 示例: 輸入:"23" 輸出:["ad", "
LeetCode(17)-Letter Combinations of a Phone Number
17.Letter Combinations of a Phone Number Given a string containing digits from 2-9 inclusive, return all possible letter combinations that
Leetcode -17. Letter Combinations of a Phone Number
這個問題我最開始的想的是使用樹的結構去做,之後用python實現的時候是使用了幾個for迴圈: class Solution: def letterCombinations(self, digits): """ :type digits: str
[leetcode]17. Letter Combinations of a Phone Number
激動人心啊,終於能以這種操作ac了 第一遍submit忘記處理空,第二遍就ac了 Runtime: 2 ms, faster than 91.37% of Java online submissions for Letter Combinations of a Phone Num
LeetCode 17.Letter Combinations of a Phone Number (電話號碼的字母組合)
題目描述: 給定一個僅包含數字 2-9 的字串,返回所有它能表示的字母組合。 給出數字到字母的對映如下(與電話按鍵相同)。注意 1 不對應任何字母。 示例: 輸入:"23" 輸出:["ad", "ae", "af", "bd", "be", "bf", "cd
LeetCode——17. Letter Combinations of a Phone Number
一.題目連結: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.題目大意: 給定一段數字字串,其中每個數字字元對應瞭如下的字母字元,求出輸入字串對應的所有可能的字母字串集合。 例如,輸入數
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 o
#Leetcode# 17. Letter Combinations of a Phone Number
https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Given a string containing digits from 2-9 inclusive, return all pos
回溯Leetcode 17 Letter Combinations of a Phone Number
Leetcode 17 Letter Combinations of a Phone Number Problem Description: 電話上每個數字都有對應的字母,如下圖所示,數字0和數字1沒有對應的字母,數字2對應的字母包括“abc”,現輸入一串數字要求輸出所
演算法練習--LeetCode--17. Letter Combinations of a Phone Number
Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, return all possible letter combinatio
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 map
每日leetcode--(17) Letter Combinations of a Phone Number
遞迴思想,字串逐步減短 #include <iostream> #include <vector> #include <algorithm> #include <math.h> #include <stdio.h&g
Leetcode dfs 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
LeetCode(17)--Letter Combinations of a Phone Number
轉載自部落格 Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could
LeetCode[Backtracking]: 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