- 浏览: 188909 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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.
用一个字符串数组模拟键盘上数字代表的字符串,用回溯法,分别记录每个可能的答案。
代码如下:
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.
用一个字符串数组模拟键盘上数字代表的字符串,用回溯法,分别记录每个可能的答案。
代码如下:
public class Solution { public List<String> letterCombinations(String digits) { List<String> list = new ArrayList<String>(); if(digits == null || digits.length() == 0) return list; String[] str = {"","", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; getLetter(digits, str, 0, "", list); return list; } public void getLetter(String digits, String[] str, int start, String s, List<String> list) { if(s.length() == digits.length()) { list.add(s); } if(start < digits.length()) for(int i = 0; i < str[digits.charAt(start) - '0'].length(); i++) { getLetter(digits, str, start + 1, s + str[digits.charAt(start) - '0'].charAt(i), list); } } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 290Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 297You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 417Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 399Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 529Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 605Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 504Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 699Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 500The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 453Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 614Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 631Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 456All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 925Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 951Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 627Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 720Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 902Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 810You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 753For a undirected graph with tre ...
相关推荐
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 ...
Letter Combinations of a Phone Number" 是一个关于JavaScript编程的挑战,旨在实现一个功能,该功能根据电话号码的数字生成所有可能的字母组合。这个题目常见于算法和编程面试中,目的是考察开发者的逻辑思维和...
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
c c语言_leetcode 0017_letter_combinations_of_a_phone_number.zip
java入门 java_leetcode题解之17_Letter_Combinations_of_a_Phone_Number
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,...
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 ...
【C语言-leetcode题解之17-letter-combinations-of-a-phone-number.c】的具体实现,展示了递归算法在组合问题上的应用,也考验了程序员对字符串操作和递归机制的理解。通过这道题目的练习,可以加深对C语言编程和...
第 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/...
### LeetCode题解:电话号码的字母组合(17题) 在LeetCode的算法题目中,第17题要求编写一个函数,根据不同的数字组合返回所有可能的字母组合。...var letterCombinations = function(digits) { if(digits
17. Letter Combinations of a Phone Number:电话号码的字母组合。这是回溯算法的一个典型应用。 18. 4Sum:找到所有和为特定值的不重复的四元组。 19. Remove Nth Node From End of List:移除链表的倒数第N个...
17. letter Combinations of a Phone Number(电话号码的字母组合):通过回溯算法生成所有可能的字母组合。 18. 4Sum II(4Sum II):与4Sum类似,但需要寻找四个数的两对和相等的情况。 19. 删除链表的倒数第N个...
9. 电话号码的字母组合(Letter Combinations of a Phone Number):使用回溯法穷举所有可能的字母组合。 10. 二叉树的中序遍历(Binary Tree Inorder Traversal):使用递归或迭代方式访问二叉树的所有节点。 11....
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 ...