LeetCode----17. Letter Combinations of a Phone Number(遍历树+多重循环)
来源:互联网 发布:网页电子书制作软件 编辑:程序博客网 时间:2024/06/05 04:18
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.
Subscribe to see which companies asked this question
遍历树:for + 递归
Input:""
Output:[""]
Expected:[]
public class Solution {public List<String> letterCombinations(String digits) { List<String> result = new ArrayList<String>(); if(digits==null||digits.length()==0){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"; char[] middleTemp = new char[digits.length()]; dfsGetStr(digits, 0, middleTemp, map, result); return result; } private void dfsGetStr(String digits,int index, char[] middleStr, String[] map, List<String> result) { if(index == digits.length()) { result.add(new String(middleStr)); return ; } char strChar = digits.charAt(index); for(int i=0; i<map[strChar-'0'].length(); i++) { middleStr[index] = map[strChar-'0'].charAt(i); dfsGetStr(digits, index+1, middleStr, map, result); } } }
普通循环:
public List<String> letterCombinations(String digits) {List<String> result = new ArrayList<String>();if(digits.isEmpty()) {result.add("");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; }
public class Solution { public ArrayList<String> letterCombinations(String digits) { ArrayList<String> result = new ArrayList<String>(); if (digits == null || digits.equals("")) { return result; } Map<Character, char[]> map = new HashMap<Character, char[]>(); map.put('0', new char[] {}); map.put('1', new char[] {}); map.put('2', new char[] { 'a', 'b', 'c' }); map.put('3', new char[] { 'd', 'e', 'f' }); map.put('4', new char[] { 'g', 'h', 'i' }); map.put('5', new char[] { 'j', 'k', 'l' }); map.put('6', new char[] { 'm', 'n', 'o' }); map.put('7', new char[] { 'p', 'q', 'r', 's' }); map.put('8', new char[] { 't', 'u', 'v'}); map.put('9', new char[] { 'w', 'x', 'y', 'z' }); StringBuilder sb = new StringBuilder(); helper(map, digits, sb, result); return result; } private void helper(Map<Character, char[]> map, String digits, StringBuilder sb, ArrayList<String> result) { if (sb.length() == digits.length()) { result.add(sb.toString()); return; } for (char c : map.get(digits.charAt(sb.length()))) { sb.append(c); helper(map, digits, sb, result); sb.deleteCharAt(sb.length() - 1); } }}
0 0
- LeetCode----17. Letter Combinations of a Phone Number(遍历树+多重循环)
- (Java)LeetCode-17. 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
- 7.producer
- 集群的简单理解
- c++枚举和搜索注册表
- UICollectionViewLayout
- Android开发之解决APP启动白屏或者黑屏闪现的问题
- LeetCode----17. Letter Combinations of a Phone Number(遍历树+多重循环)
- java实现文件复制
- session防止表单重复提交
- Android launcher相关的一些知识
- ProgressDialog的使用
- 事件分发:弹性滑动、滑动冲突
- ThumbnailMenu 一个简单而精致的 Fragment 菜单控件,以缩略图的形式实现 Fragment 切换
- 最新iPhone7苹果手机ios10系统如何使上国外网站,关注明星Instagram和推特动态
- java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to