Letter Combinations of a Phone Number
来源:互联网 发布:erp系统数据录入 编辑:程序博客网 时间:2024/05/20 11:46
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
这是我一开始的思路,穷举而已。
String[] myMap=new String[]{"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; public List<String> letterCombinations(String digits) { List<String> result=new ArrayList<String>(); int pointer=(int)Math.pow(4,digits.length()); if(pointer==1){return result;} for(int i=0;i<pointer;i++){ String tmp=""; for(int j=0;j<digits.length();j++){ int index=3&(i>>j*2); if(index>=myMap[digits.charAt(j)-'2'].length()){ tmp=null; break; } tmp+=myMap[digits.charAt(j)-'2'].charAt(index); } if(tmp!=null){ result.add(tmp); } } return result; }
看到了一个别人用LinkedList完成的,觉得非常棒:
public List<String> letterCombinations(String digits) { LinkedList<String> ans = new LinkedList<String>(); String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; ans.add(""); for(int i =0; i<digits.length();i++){ int x = Character.getNumericValue(digits.charAt(i)); while(ans.peek().length()==i){ String t = ans.remove(); for(char s : mapping[x].toCharArray()) ans.add(t+s); } } return ans;}
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
- LeetCode18:Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- 谈谈WebService开发-基础篇
- 视图拖动返回原位和高斯模糊效果
- iOS开发个人独立博客收集
- Solar energy enjoys a glowing outlook
- Hadoop错误集锦(持续更新)
- Letter Combinations of a Phone Number
- 第8周-项目0-简单C++学生信息管理系统
- Android-百度地图的调用(检索功能)
- 在IOS中设置文本框字体自适应高度
- C++求两个数的最大公约数。
- jboss的缓存问题
- ZOJ 3469 - Food Delivery(区间DP)
- 乐观锁和悲观锁
- 系统安装那点事~