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.
這個題使用的迭代法,不斷的迭代list中的元素進行新元素的新增,方法挺奇妙
1)
class Solution {
public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<>();
String[] a = new String[]{" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
for(int i = 0 ; i < digits.length() ; i++){
char[] arr = a[digits.charAt(i) - '0'].toCharArray();
List<String> li = new ArrayList<>();
if(list.isEmpty()){
list.add("");
}
for(String s : list){
for(int j = 0 ; j < arr.length;j++){
li.add(s + arr[j]);
}
}
list = li;
}
return list;
}
}
2)
public class Solution {
List<String> res;
public List<String> letterCombinations(String digits) {
// 建立對映表
String[] table = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
StringBuilder tmp = new StringBuilder();
res = new LinkedList<String>();
helper(table, 0, tmp, digits);
return res;
}
private void helper(String[] table, int idx, StringBuilder tmp, String digits){
if(idx == digits.length()){
// 找到一種結果,加入列表中
if(tmp.length()!=0) res.add(tmp.toString());
} else {
// 找出當前位數字對應可能的字母
String candidates = table[digits.charAt(idx) - '0'];
// 對每個可能字母進行搜尋
for(int i = 0; i < candidates.length(); i++){
tmp.append(candidates.charAt(i));
helper(table, idx+1, tmp, digits);
tmp.deleteCharAt(tmp.length()-1);
}
}
}
}
假設總共有n個digit,每個digit可以代表k個字元,那麼時間複雜度是O(k^n),就是結果的數量,空間複雜度也是一樣
相關文章
- Leetcode – 017. Letter Combinations of a Phone NumberLeetCode
- LeetCode Letter Combinations of a Phone Number(017)解法總結LeetCode
- 演算法練習--LeetCode--17. Letter Combinations of a Phone Number: 100%演算法LeetCode
- Codeforces 1017 CThe Phone Number
- Leetcode Number of islandsLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 9 Palindrome NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- 【Leetcode】1180. Count Substrings with Only One Distinct LetterLeetCode
- Leetcode 933 Number of Recent CallsLeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode之Fibonacci Number(Kotlin)LeetCodeKotlin
- Carousel of Combinations
- 關於開發者協議Edit Phone Number終極解決方案協議
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- LeetCode之Number of Recent Calls(Kotlin)LeetCodeKotlin
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 191. Number of 1 BitsLeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- Leetcode 137. Single Number IILeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- [LeetCode] 3238. Find the Number of Winning PlayersLeetCode
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode
- Leetcode 202 Happy Number Javascript 解決方案LeetCodeAPPJavaScript
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- LeetCode演算法題-Number of Boomerangs(Java實現)LeetCode演算法OOMJava
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- [LeetCode] 1953. Maximum Number of Weeks for Which You Can WorkLeetCode
- [LeetCode] 3226. Number of Bit Changes to Make Two Integers EqualLeetCode
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- 2020/11/17·Leetcode·移動零LeetCode
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode