Leetcode 17 Letter Combinations of a Phone Number
来源:互联网 发布:四川工匠网络投票 编辑:程序博客网 时间:2024/06/05 21:09
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"].
Solution1
- 求字符串之间的所有组合问题是典型的回溯问题。基本思路就是有几个数字便递归调用几轮,在每一轮调用的时候,去遍历所有的情况,如此递归到最深处。然后再回溯。代码如下:
import java.util.*;public class Solution { public List<String> letterCombinations(String digits) { char[][] table = {{'\0'},{'\0'},{'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'}}; ArrayList<String> result = new ArrayList<String>(); help(digits,0,table,result,new StringBuffer()); return result; } public void help(String digits,int index,char[][] table,List<String> result,StringBuffer sb){ if(index>=digits.length()){ if(sb.length()!=0) result.add(sb.toString()); return; } int digit = digits.charAt(index) - '0'; for(int i=0;i<table[digit].length;i++){ StringBuffer nsb = new StringBuffer(sb); if(digit!=0&&digit!=1) nsb.append(table[digit][i]); help(digits,index+1,table,result,nsb); } }}
Solution2
- 下面用迭代的解法来求解这个问题。
import java.util.*;public class Solution { public List<String> letterCombinations(String digits) { String[] table = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; ArrayList<String> result = new ArrayList<String>(); if(digits.length()==0) return result; result.add(""); for(int i=0;i<digits.length();i++){ int digit = digits.charAt(i)-'0'; int size = result.size();//由于后边要更改result,所以必须得提前保存好当前result的长度 for(int k=size;k>0;k--){//每次都移除头部元素,移除k次 String s = result.get(0); if(digit!=0&&digit!=1) result.remove(0);//注意只有在非0和非1的时候才将之前的移除 for(int j=0;j<table[digit].length();j++){ String temp = new String(s); temp += table[digit].charAt(j); result.add(temp); } } } return result; }}
0 0
- 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
- UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)
- Linux下使用Python捕获键盘输入
- Android开发工具---SQLiteManager插件
- Apache 2.4.16、PHP5.6.11安装
- Scala 入门学习
- Leetcode 17 Letter Combinations of a Phone Number
- Spring 之注解事务 @Transactional
- Hello World。
- Linux环境下开发过程中常用的shell命令
- 跟随大神的脚步学习之Handler:Handler详解
- 【整理】CAD常用快捷键
- 多线程消息处理队列
- Portal Server搭建(wifidog安装)
- LAMP+YII框架配置中遇到的问题