Letter Combinations of a Phone Number - LeetCode
来源:互联网 发布:苹果手机价格评估软件 编辑:程序博客网 时间:2024/05/01 14:54
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.
本题使用了深度优先遍历的思想,与之前的Permutation十分相似。
遍历过程:
DFS 在访问图中某一起始顶点 v 后,由 v 出发,访问它的任一邻接顶点 w1;再从 w1 出发,访问与 w1邻 接但还没有访问过的顶点 w2;然后再从 w2 出发,进行类似的访问,… 如此进行下去,直至到达所有的邻接顶点都被访问过的顶点 u 为止。
接着,退回一步,退到前一次刚访问过的顶点,看是否还有其它没有被访问的邻接顶点。如果有,则访问此顶点,之后再从此顶点出发,进行与前述类似的访问;如果没有,就再退回一步进行搜索。重复上述过程,直到连通图中所有顶点都被访问过为止。
在这里,回退一步十分重要,这里是让一个点尝试其他可能。
代码以及DFS思想步骤如下:
public class Solution { public ArrayList<String> letterCombinations(String digits) { String[] ss = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; ArrayList<String> ret = new ArrayList<String>(); rec(ret, digits.length(), ss, digits, new StringBuffer()); return ret; } public void rec(ArrayList<String> ret, int remain, String[] ss, String digits, StringBuffer sb){ if(remain == 0){ ret.add(sb.toString()); return; } String s = ss[digits.charAt(0)-'0']; //the element we need to deal with // DFS for(int i=0; i<s.length(); i++){ sb = sb.append(s.charAt(i));//Add new element rec(ret, remain-1, ss, digits.substring(1), sb);//Traversal one substring sb.deleteCharAt(sb.length()-1); //trace back for other solutions } }}
我们可以看到DFS主要分为四个步骤:
1、分析是否遍历,如果是一种遍历可能就停止,输出
2、开始遍历,将新元素连接到结果串最后
3、遍历剩下的子串(一种可能)
4、删除最后一位的遍历尝试其他可能,直到所有可能(for循环)都被遍历
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- leetcode Letter Combinations of a Phone Number
- Leetcode: Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- 为什么用Win32forth编程的程序员不多
- 在Ubuntu 13.10 下安装支持SSL的Apache
- 关于HBase的一些零碎事
- 编译原理学习笔记05——(识别孙悟空72变之魔鬼特训—递归下降分析程序构造)——2014_1_20
- Maximum Subarray
- Letter Combinations of a Phone Number - LeetCode
- [MySQL Delay]生产环节主从延迟的问题解决过程记录: sync_binlog=0
- 关于 hadoop-2.2 配置中的 yarn.nodemanager.aux-services 项
- SAP Cloud offering (网络搜集)
- Binary Tree Inorder Traversal - LeetCode
- Search Insert Position
- Android Studio java.lang.RuntimeException: Unable to start activity ComponentInfo
- 用C/C++设计Lu结构、字典、类等高级数据类型
- C/C++使用MLu简化Lu脚本系统的使用