17. Letter Combinations of a Phone Number
来源:互联网 发布:淘宝卖家都在哪里进货 编辑:程序博客网 时间:2024/06/05 09:07
简单的递归题
import java.util.*;public class Solution { public List<String> letterCombinations(String digits) { String[] map = new String[10]; map[0] = ""; map[1] = ""; map[2] = "abc"; map[3] = "def"; map[4] = "ghi"; map[5] = "jkl"; map[6] = "mno"; map[7] = "pqrs"; map[8] = "tuv"; map[9] = "wxyz"; int len = digits.length(); List<String> result = new ArrayList<>(); dfsStr(result, new String(""), 0, len, map, digits); return result; } public void dfsStr(List<String> result,String tmp, int index, int length, String[] map, String digits) { if(digits.length()==0) return; int k = Integer.valueOf(digits.charAt(index)-'0'); String values = map[k]; for(int i = 0; i < values.length(); ++i) { if(tmp.length() == length-1) { result.add(new String(tmp + values.charAt(i))); } else { dfsStr(result, tmp + values.charAt(i), index + 1,length, map, digits ); } } }//// public static void main(String[] args) {// Solution solution = new Solution();// List<String> str = solution.letterCombinations("23");// for(int i = 0; i < str.size(); ++i) {// System.out.println(str.get(i));// }// }}
阅读全文
0 0
- 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
- 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
- 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
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- Linux进程间通信之消息队列
- json应用场景与实战
- eclipse引入maven工程的正确姿势
- tfRecord写入
- 【codeforces】Heidi and Library (贪心STL)
- 17. Letter Combinations of a Phone Number
- Linux-脚本安装软件
- 【YOLO学习】召回率(Recall),精确率(Precision),平均正确率(Average_precision(AP) ),交除并(Intersection-over-Union(IoU))
- Maven学习(七)
- Lintcode64 Merge Sorted Array solution 题解
- 【拓扑排序模板题DFS方法】UVA
- 【DP】TEST20170429
- 莲子百合银耳羹
- leetcode404. Sum of Left Leaves