新博文地址:[leetcode]Letter Combinations of a Phone Number
新博文中采用了DFS算法,代码简洁易懂
http://oj.leetcode.com/problems/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.
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.
这段代码自己看着都感觉恶心。。。。下次再优化吧,不多说,迭代
StringBuilder str = new StringBuilder(); ArrayList<String> result = new ArrayList<String>(); public ArrayList<String> letterCombinations(String digits) { char[][] charTable = { {}, {}, { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' }, { 'j', 'k', 'l' }, { 'm', 'n', 'o' }, { 'p', 'q', 'r', 's' }, { 't', 'u', 'v' }, { 'w', 'x', 'y', 'z' } }; List<Integer> numList = new ArrayList<Integer>(); for (int i = 0; i < digits.length(); i++) { if (digits.charAt(i) > '1') {//这个numList完全没必要,懒得改了 numList.add(digits.charAt(i) - '0'); } } if (numList.isEmpty()) { result.add("");//结果为空时,一定要加个空串。。。。何必呢 return result; } if (numList.get(0) < 2) { return result; } else { if (numList.size() == 1) { for (char tem : charTable[numList.get(0)]) { str.append(tem); result.add(str.toString()); str.deleteCharAt(str.length() - 1); } } else { for (char tem : charTable[numList.get(0)]) { str.append(tem); letterCombinations(digits.substring(1)); str.deleteCharAt(str.length() - 1); } } } return result; }
相关推荐
https://leetcode.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 ...
c c语言_leetcode 0017_letter_combinations_of_a_phone_number.zip
java入门 java_leetcode题解之17_Letter_Combinations_of_a_Phone_Number
c语言入门 C语言_leetcode题解之17-letter-combinations-of-a-phone-number.c
js js_leetcode题解之17-letter-combinations-of-a-phone-number.js
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怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...
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
Leetcode扑克 Leetcode Starting from 2020/08/02 Leetcode practice with Python (for now) Daily Challenge + Selected Questions From Using Leetcode Plugin for Solutions ID English Title 中文题目名称 ...
leetcode 分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, ...
leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript 难度 1 Two Sum 简单 2 Add Two Numbers 中等 3 Longest Substring Without Repeating... 中等 5...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 005 Longest Palindromic Substring 006 ZigZag Conversion 007 Reverse Integer 008 ...
leetcode二维数组搜索tags: Leetcode [toc] Leetcode list 一、DataStructure Array Linked_list Stack Queue BT BST Set 二、brute search 穷举(DFS) 17.Letter Combinations of a Phone Number backtracking int ...
leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...
17. Letter Combinations of a Phone Number:电话号码的字母组合。这是回溯算法的一个典型应用。 18. 4Sum:找到所有和为特定值的不重复的四元组。 19. Remove Nth Node From End of List:移除链表的倒数第N个...
leetcode怎么销号 记录我在用python刷leetcode中各个题的解题思路 the answers for leetcode problem by python 10.Regular Expression Matching: 递归的方法:当前正则第二个字符不为'*',很简单,比较当前,两个...
17. letter Combinations of a Phone Number(电话号码的字母组合):通过回溯算法生成所有可能的字母组合。 18. 4Sum II(4Sum II):与4Sum类似,但需要寻找四个数的两对和相等的情况。 19. 删除链表的倒数第N个...
题目来源:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number 题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 ...