题目

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.

题解

这道题也是来算一种combination的题,跟 CombinationsWord Break II 都比较类似(其实和leetcode里面很多题都相似)。

代码如下:

 1   public ArrayList<String> letterCombinations(String digits) {
 2       ArrayList<String> result=new ArrayList<String>();
 3       if (digits==null)
 4           return result;
 5 
 6       String[] keyboard={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
 7       StringBuilder current=new StringBuilder();
 8       
 9       int index=0;
       buildResult(digits, index, current, keyboard, result);
       return result;
   }
   
   private void buildResult(String digits, int index, StringBuilder current, String[] keyboard, ArrayList<String> result){
       if (index==digits.length()){
         result.add(current.toString());
         return;
         }
         
       int num=digits.charAt(index)-'0';//get integer number
       for (int i=0; i<keyboard[num].length(); i++){
         current.append(keyboard[num].charAt(i));
         buildResult(digits, index+1, current, keyboard, result);
         current.deleteCharAt(current.length()-1);
         }
     }

Refrence:http://rleetcode.blogspot.com/2014/02/letter-combinations-of-phone-number-java.html

最新文章

  1. oracle SEQUENCE 创建, 修改,删除
  2. Duilib源码分析(四)绘制管理器—CPaintManagerUI
  3. 利用DOS批处理实现定时关机操作
  4. ASP.NET页面的字符编码设置
  5. 为什么没调用 didRegisterForRemoteNotificationsWithDeviceToken 和 didFailToRegisterForRemoteNotificationsWithError
  6. Android开发中XML布局的常用属性说明
  7. SQL优化大全
  8. ORM:ODB安装使用过程
  9. Oppotunity land---China
  10. VS2010旗舰版安装图解
  11. Parallels destop8 无法创建bootcamp虚拟机
  12. C - 链表,推荐
  13. [置顶] 关于UBUNTU 12.04, 在THINKPAD E430C上WIFI连接不上的问题
  14. CodeForces 645C Enduring Exodus
  15. org.hibernate.exception.JDBCConnectionException: could not execute query
  16. SQLServer之UNIQUE约束
  17. vue中具名插槽的使用
  18. LeetCode刷题:第七题 整数翻转 第九题 回文数
  19. 下载Android kernel
  20. macOS Sierra 如何卸载.net core 版本

热门文章

  1. Codeforces Beta Round #14 (Div. 2) A. Letter 水题
  2. Java_Certificates does not conform to algorithm constraints
  3. Eclipse 正则表达式 查找与替换
  4. bitnami下webmin安装
  5. go标准库DOC与 raft
  6. Snmp学习总结(三)——Win7安装和配置SNMP
  7. Unity3D实践系列10, Canvas画布的创建和使用
  8. 委托、Lambda表达式、事件系列05,Action委托与闭包
  9. 如何在Windows服务程序中添加U盘插拔的消息
  10. iOS webservice 接口使用方法