?LeetCode刷題實(shí)戰(zhàn)17: 電話號碼的字母組合
算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !
今天和大家聊的問題叫做電話號碼的字母組合?,我們先來看題面:
https://leetcode-cn.com/problems/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.
題意

樣例
輸入:"23"
輸出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].題解


class?Solution {
????public?List<String> letterCombinations(String?digits) {
????????// 數(shù)字與字母的對應(yīng)關(guān)系
????????String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
????????List<String> res = new?ArrayList<>();
????????if(digits.isEmpty())
????????????return?res;
????????// 圖中樹的根節(jié)點(diǎn)
????????res.add("");
????????
????????// 遍歷輸入的數(shù)字
????????for(char c : digits.toCharArray()){
????????????res = combine(map[c-'0'], res);
????????}
????????
????????return?res;
????}
????
????// 根據(jù)數(shù)字組合字母
????private?List<String> combine(String?digits, List<String> list){
????????List<String> res = new?ArrayList<>();
????????for(char c : digits.toCharArray()){
????????????for(String?s : list){
????????????????res.add(s+c);
????????????}
????????}
????????
????????return?res;
????}
}
今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。
上期推文:
LeetCode刷題實(shí)戰(zhàn)1:在數(shù)組上遍歷出花樣
LeetCode刷題實(shí)戰(zhàn)2:用鏈表模擬加法
LeetCode刷題實(shí)戰(zhàn)3:最長不重復(fù)子串
LeetCode刷題實(shí)戰(zhàn)4:兩個正序數(shù)組的中位數(shù)
LeetCode刷題實(shí)戰(zhàn)5:判斷回文子串
LeetCode刷題實(shí)戰(zhàn)6:Z字形變換
LeetCode刷題實(shí)戰(zhàn)7:整數(shù)反轉(zhuǎn)
LeetCode刷題實(shí)戰(zhàn)8:字符串轉(zhuǎn)換整數(shù)
LeetCode刷題實(shí)戰(zhàn)9:求解回文數(shù)
LeetCode刷題實(shí)戰(zhàn)10:字符串正則匹配
LeetCode刷題實(shí)戰(zhàn)11: 盛最多水的容器
LeetCode刷題實(shí)戰(zhàn)12: 整數(shù)轉(zhuǎn)羅馬數(shù)字
LeetCode刷題實(shí)戰(zhàn)13: 羅馬數(shù)字轉(zhuǎn)整數(shù)
LeetCode刷題實(shí)戰(zhàn)14: 最長公共前綴
LeetCode刷題實(shí)戰(zhàn)15:三數(shù)之和
LeetCode刷題實(shí)戰(zhàn)16: 最接近的三數(shù)之和
