LeetCode17

来源:互联网 发布:什么是ba无标度网络 编辑:程序博客网 时间:2024/05/15 21:05

【题目】

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.

【思路】

直接采用递归

【Java代码】

public class Solution_17_letter_combinations_of_a_phone_number {String[][] dict = {{"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","l"},{"m","n","o"},{"p","q","r","s"},{"t","u","v"},{"w","x","y","z"}};public List<String> letterCombinations(String digits){if(digits.length() == 0)return new ArrayList<String>();int index = Character.getNumericValue(digits.charAt(0))-2;if(digits.length() == 1){return Arrays.asList(dict[index]);}else{List<String> curr = letterCombinations(digits.substring(1));List<String>result = new ArrayList<String>();for(int i = 0 ; i < dict[index].length ; i++)for(int j = 0 ; j < curr.size() ; j++)result.add(dict[index][i]+curr.get(j));return result;}}}


原创粉丝点击