LeetCode-17 Letter Combinations of a Phone Number(手机拨码-DFS)
来源:互联网 发布:淘宝联盟数据采集 编辑:程序博客网 时间:2024/06/05 04:43
LeetCode-17 Letter Combinations of a Phone Number(手机拨码-DFS)
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.
public class Solution { List<String> list = new ArrayList<String>(); public List<String> letterCombinations(String digits) {if(digits.equals("")) {return list;} String[] map = new String[10]; map[0] = ""; map[1] = ""; map[2] = "abc"; map[3] = "def"; map[4] = "ghi"; map[5] = "jkl"; map[6] = "mno"; map[7] = "pqrs"; map[8] = "tuv"; map[9] = "wxyz"; String[] mstr = new String[digits.length()]; for (int i = 0; i < digits.length(); i++) { mstr[i] = map[digits.charAt(i)-48];} print(mstr, 0, mstr.length, ""); return list; } public void print(String mstr[],int l,int n,String str){if (n <= 0) {list.add(str);} else {for (int i = 0; i < mstr[l].length(); i++) {print(mstr, l+1, n-1, str + mstr[l].charAt(i) + "");}} }}Runtime: 228 ms
深度优先搜索,以上是递归的写法。
看到别人的迭代的写法,在此也记录下来。
public class Solution {//DFS 迭代的方法,记得研究一下。public List<String> letterCombinations(String digits) {List<String> result = new ArrayList<String>();if(digits.equals("")) {return result;}String[] map = new String[10];map[0] = "";map[1] = "";map[2] = "abc";map[3] = "def";map[4] = "ghi";map[5] = "jkl";map[6] = "mno";map[7] = "pqrs";map[8] = "tuv";map[9] = "wxyz";int[] number = new int[digits.length()];//存储digits中每个字符在循环中的编号,初始为0int k = digits.length()-1;while(k>=0) {k = digits.length()-1;char[] charTemp = new char[digits.length()];for(int i=0; i<digits.length(); i++) {charTemp[i] = map[digits.charAt(i)-'0'].charAt(number[i]);}result.add(new String(charTemp));while(k>=0) {if( number[k] < (map[digits.charAt(k)-'0'].length()-1) ) {number[k]++;break;} else {number[k] = 0;k--;}}}return result; }}
0 0
- LeetCode-17 Letter Combinations of a Phone Number(手机拨码-DFS)
- Leetcode dfs Letter Combinations of a Phone Number
- Leetcode Letter Combinations of a Phone Number DFS
- LeetCode Letter Combinations of a Phone Number DFS
- LeetCode OJ-17.Letter Combinations of a Phone Number(DFS)
- 【DFS】Letter Combinations of a Phone Number
- DFS 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
- git 从 ochina 管理代码
- LeetCode (20) house robber (数组不相邻元素最大值)
- ads1.2
- 学习笔记(一)——STL
- 值得推荐的C/C++框架和库
- LeetCode-17 Letter Combinations of a Phone Number(手机拨码-DFS)
- css圆圈360度转
- spring @component的作用
- 使用Kindeditor上传图片
- 7.2 MaxSliceSum
- Infinispan配置文件说明
- iOS使用技巧
- Java 中JTexField组件对键盘的监听keyPresse方法中用consume达不到效果 但是TexField却可以
- 简单的模拟双色球