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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
这个可以用递归做,主要思想是尝试,也就是Backtracking,这个还是比较好理解的。
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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
这个可以用递归做,主要思想是尝试,也就是Backtracking,这个还是比较好理解的。
public List<String> letterCombinations(String digits) { List<String> result = new ArrayList<String>(); if (digits == null || digits.length() == 0) { return result; } String[] strs = { "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; List<String> ls = new ArrayList<String>(); String temp = digits; if (digits.length() > 1) { temp = digits.substring(0,digits.length() - 1); List<String> lett = letterCombinations(temp); int tem = Integer.valueOf(digits.substring(digits.length() - 1)); if (tem != 1 && tem != 0) { for (String str : lett) { for (int i = 0; i < strs[tem-2].length(); i++) { String s = str + strs[tem - 2].charAt(i); ls.add(s); } } }else{ ls = lett; } } if (digits.length() == 1) { String temp1 = digits; if (Integer.valueOf(temp1) != 1 && Integer.valueOf(temp1) != 0) { for (int i = 0; i < strs[Integer.valueOf(temp1) - 2].length(); i++) { String s = "" + strs[Integer.valueOf(temp1) - 2].charAt(i); ls.add(s); } } } return ls; }
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 323Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 362Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 305Given a string containing just ... -
Remove Nth Node From End of List
2015-03-05 22:31 336Given a linked list, remove the ... -
4Sum
2015-03-05 22:26 309Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 287Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 316Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 324Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 323Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 287Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 319Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 362Implement regular expression ma ... -
Palindrome Number
2015-02-13 22:08 329Determine whether an integer is ... -
String to Integer (atoi)
2015-02-13 11:07 336Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 224Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 251The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 330Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 297You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 423[size=24px;]Longest Substring W ...
相关推荐
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 possible ...
js代码-17. Letter Combinations of a Phone Number
17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap ...
leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 ...242 Letter Combinations of a Phone Number 587 243 Restore IP Addresses 589 244 Reverse Integer 591 245 Palindrome Number 593
of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. * Difficulty:Medium * Classification:String,...
Letter Combinations of a Phone Number 广度优先搜索 Word Ladder Word Ladder II Surrounded Regions 总结 深度优先搜索 Additive Number Palindrome Partitioning Unique Paths Unique Paths II N-Queens N-...
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持...Combinations of a Phone Number 018 4Sum 020 Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat
Combinations of a Phone Number Medium 回溯、暴力 0034 Find First and Last Position of Element in Sorted Array Medium 二分 0039 Combination Sum Medium 回溯 0040 Combination Sum II Medium 回溯 0046 ...
第 338 章力码 LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating ...of ...Number ...17.Letter Combinations of a Phone Number 18.4Sum 19.Remove Nth Node From End
leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript ...Number ...Letter Combinations of a Phone Number DFS 中等 18 4Sum 中等 19 Remo
Combinations of a Phone Number 电话号码的字母组合 回溯法,递归 / Backtrack,Recursion 回溯+递归 Daily Challenge 2020/08/26 20 Valid Parentheses 有效的括号 Stack / 栈 用栈实现配对 Daily Challenge 2020/...
Combinations of a Phone Number backtracking int solution[MAX_DIMENSION]; void Backtracking(int dimension) { if(solution is well-generated) { process solution return; } for( x = each value of current ...
Combinations of a Phone Number 093:Restore IP Addresses 树的遍历问题也可以用这种思想来解释。只不过是特殊的递归而已。(只有两路,不用循环) 题型二:动态规划(要整理搜索和DP的区别,都可以用一个状态转移...
Combinations of a Phone Number: 这题是要找到号码对应字符串的所有组合。用字典来表示数字到字符串的组合。然后遍历数字串。使其对应的字母list与前面已有的组合进行 连接即可。 19.Remove Nth Node From End of ...
However, if we are searching for multiple rows, such as duplicate values, or keys in a range, anything more than a small number of rows will make the nonclustered index search very inefficient. ...
017_Letter_Combinations_of_a_Phone_Number 018_4总和 019_Remove_Nth_Node_From_End_of_List 020_Valid_Parentheses 021_Merge_Two_Sorted_Lists 022_Generate_Parentheses 023_Merge_k_Sorted_Lists 024_...
加油站 leetcode ...17-letter-combinations-of-a-phone-number 20 cargo run --bin 20-valid-parentheses 21 cargo run --bin 21-merge-two-sorted-lists 27 cargo run --bin 27-remove-element 28
题目来源:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number 题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 ...
java lru leetcode Leetcode 问题的解决方案 问题 解决方案 0001_Two_Sum 0002_Add_Two_Numbers 0003_Longest_Substring_Without_Repeating_Characters ...0004_Median_of_Two_...0017_Letter_Combinations_of_a_Phone_N