[LeetCode]17. Letter Combinations of a Phone Number
来源:互联网 发布:淘宝翡翠a货是真的吗 编辑:程序博客网 时间:2024/05/16 16:05
Problem Description
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.
[https://leetcode.com/problems/letter-combinations-of-a-phone-number/]
思路
递归循环构造字符串即可…
第二次一遍AC 鼓励自己一下 看会死神去咯~
Code
package Q17;import java.util.ArrayList;import java.util.List;public class Solution { public static List<String> getLetter(List<String> tmp, String digits) { List<String> ans = new ArrayList<String>(); if (tmp.size() > 0) { for (int i = 0; i < digits.length(); i++) { for (int j = 0; j < tmp.size(); j++) { ans.add(tmp.get(j) + digits.charAt(i)); } } } else{ for (int i = 0; i < digits.length(); i++) { ans.add(digits.charAt(i)+""); } } return ans; } public static List<String> letterCombinations(String digits) { List<String> ans = new ArrayList<String>(); List<String> digits2letters = new ArrayList<String>(); digits2letters.add(" "); digits2letters.add(""); digits2letters.add("abc"); digits2letters.add("def"); digits2letters.add("ghi"); digits2letters.add("jkl"); digits2letters.add("mno"); digits2letters.add("pqrs"); digits2letters.add("tuv"); digits2letters.add("wxyz"); if (digits.length() < 1) return ans; for (int i = 0; i < digits.length(); i++) { if(digits.charAt(i) - 48==1||digits.charAt(i) - 48==0) return new ArrayList<String>(); ans = getLetter(ans, digits2letters.get(digits.charAt(i) - 48)); } return ans; }// public static void main(String[] args) {// String a = "123";// System.out.println(letterCombinations(a).toString());// }}
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
- 基于Lua脚本的自动化测试框架设计
- java常用的几种加密的方法
- 异步操作需要的实现的方法
- bzoj1486【HNOI2009】最小圈
- Android 主线程无法访问网络
- [LeetCode]17. Letter Combinations of a Phone Number
- 长城守卫(Beijing Guards,CERC 2004,LA3177)
- HDU 5621 KK's Point(规律)
- 17 网络编程
- Spring、Spring MVC、MyBatis等Java Web框架使用问题集锦
- Scrum团队中的自动化测试
- java学习心得——mybatis配置替换jdbc代码
- Ubuntu_14.04_Server配置FTP、JDK、Tomcat、MySQL、Nginx
- android 双击退出