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 (just like on the telephone buttons) is given below.Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
思路:這個題目有個特點就是每一個結果的長度都等於輸入的digits的長度,每繼續按一個鍵都是前面所有的結果與當前這個鍵對應的所有字母的組合。比方說一開始按了”2”,對應的結果是[“a”,”b”,”c”],再按一個”3”,就是前面的產生的”a”與”d”、”e”、”f”的組合[“ad”,”ae”,”af”],前面的產生的”b”與”d”、”e”、”f”的組合[“bd”,”be”,”bf”],前面的產生的”c”與”d”、”e”、”f”的組合[“cd”,”ce”,”cf”]。
程式碼如下:
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if len(digits) == 0: return []
mapping = ('0', '1', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz')
result = ['', ]
for digit in digits:
temp, result = result, []
for item in temp:
for letter in mapping[int(digit)]:
result.append(item + letter)
return result
時間效能如下圖所示:
相關推薦
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
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
/** * Index: 17 * Title: Letter Combinations of a Phone Number * Author: ltree98 **/ 這道題應該是
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