Letter Combinations of a Phone Number
来源:互联网 发布:淘宝怎么实名认证激活 编辑:程序博客网 时间:2024/06/06 03:19
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"].分析:
easy
参考代码:
http://www.programcreek.com/2014/04/leetcode-letter-combinations-of-a-phone-number-java/
public class Solution { public List<String> letterCombinations(String digits) { HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(2, "abc"); map.put(3, "def"); map.put(4, "ghi"); map.put(5, "jkl"); map.put(6, "mno"); map.put(7, "pqrs"); map.put(8, "tuv"); map.put(9, "wxyz"); map.put(0, ""); ArrayList<String> result = new ArrayList<String>(); if(digits == null || digits.length() == 0) return result; ArrayList<Character> temp = new ArrayList<Character>(); getString(digits, temp, result, map); return result;} public void getString(String digits, ArrayList<Character> temp, ArrayList<String> result, HashMap<Integer, String> map){ if(digits.length() == 0){ char[] arr = new char[temp.size()]; for(int i=0; i<temp.size(); i++){ arr[i] = temp.get(i); } result.add(String.valueOf(arr)); return; } Integer curr = Integer.valueOf(digits.substring(0,1)); String letters = map.get(curr); for(int i=0; i<letters.length(); i++){ temp.add(letters.charAt(i)); getString(digits.substring(1), temp, result, map); temp.remove(temp.size()-1); } }}
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
- maryland上安装opencv
- 使用PHP生成二维码的两种方法(带logo图像)
- 机器学习算法总览——思维导图
- 数据库(sqlite3)项目实际应用
- 最少找零问题与完全背包模型的一点思考
- Letter Combinations of a Phone Number
- HDU 1085 母函数
- oc学习笔记-protocol代理模式
- 使用netstat检测及监测网络连接
- BFS与DFS
- 004.简单的加减乘除
- Cache工作原理
- XDebug
- iOS 开发之 AutoLayout 自动布局