Leet Code 17 Letter Combinations of a Phone Number
来源:互联网 发布:java svg转图片 编辑:程序博客网 时间:2024/06/17 13:14
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.
【算法思路】
利用递归进行排列组合
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
- Leet Code 17 Letter Combinations of a Phone Number
- [leet code] Letter Combinations of a Phone Number
- Leet Code OJ 17. Letter Combinations of a Phone Number
- 17,Letter Combinations of a Phone Number
- #17 Letter Combinations of a Phone Number
- 17Letter Combinations of a Phone Number
- 17 Letter Combinations of a Phone Number
- 17 Letter Combinations of a Phone Number
- 【17】Letter Combinations of a Phone Number
- 17 Letter Combinations of a Phone Number
- 17-Letter Combinations of a Phone Number
- CODE 113: Letter Combinations of a Phone Number
- leetcode 日经贴,Cpp code -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
- Android自定义控件--无限轮播Banner和Indicator实现
- Python介绍和安装
- 解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed
- ubnkwguzerr
- 详解内网IP外网IP的关联及访问互联网原理
- Leet Code 17 Letter Combinations of a Phone Number
- 使用 apache-jmeter 完成WEB压力测试
- oracle新建用户及授权
- java实现excel的导入导出(poi详解)
- Unity3D简单电影视图编辑
- android-整体UI设计-(滑动导航栏+滚动页面)
- 基础知识快速提升
- CentOS 开放 80、8080 端口
- eclipse的svn插件出现问题,64位系统,解决方法