[leetcode]Letter Combinations of a Phone Number
来源:互联网 发布:淘宝有订单怎么发货 编辑:程序博客网 时间:2024/05/16 05:43
题目描述如下:
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”].
虽然是medium难度的题目,但其实实现并不困难,一个递归便可以解决问题,需要一个辅助函数来生成最终的list。代码如下:
public class Solution { List<String> resList = new ArrayList<>(); List<String> subList = new ArrayList<>(); public List<String> letterCombinations(String digits) { if(digits.length() == 0) return resList; String[] arr={"", "" ,"abc", "def", "ghi", "jkl","mno", "pqrs", "tuv", "wxyz" }; for(int i = 0; i < digits.length(); i++){ String s = arr[(int)( digits.charAt(i) - '0')]; subList.add(s); } getResultList( "", 0, digits.length()); return resList; } public void getResultList(String tmpStr, int step, int digitsLen){ if(tmpStr.length() == digitsLen) resList.add(tmpStr); else{ String s = subList.get(step); for(int i = 0; i < s.length(); i++){ tmpStr += s.charAt(i); getResultList(tmpStr, step + 1, digitsLen); tmpStr = tmpStr.substring(0, tmpStr.length() - 1); } } }}
题目链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/
1 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
- 字符串(病毒)
- Flask-SQLAlchemy管理数据库
- 线性DP总结(LIS,LCS,LCIS,最长子段和)
- 如何在.net4.0中使用.net4.5的async/await实现异步
- 凯撒密码--简单的加密与解密
- [leetcode]Letter Combinations of a Phone Number
- Maven环境下easymock开发入门实例
- Day 5:自己编写的mysql类
- 年会感想
- c++回调函数 callback
- python 安装模块的常用方法及如何查看已安装的模块
- iOS 组透明
- java并发编程——ExecutorService\join\yield
- Scrapy 创建 常用命令