欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

(java)leetcode-17,

来源: javaer 分享于  点击 23892 次 点评:97

(java)leetcode-17,


Letter Combinations of a Phone Number

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.


解题思路:

这道题,大概就是将string转为int,然后找到对应的字符串进行添加就好了。

大概就是一个组合的东西,我也没想到什么简要的办法,所以就不断循环添加。


public class Solution {
    public List<String> letterCombinations(String digits) {
        String[] ans = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        List<String> result = new ArrayList<String>();
        if(digits == "" || digits.length() == 0)
        	return result;
        result.add("");
        int[] num = new int[digits.length()];
        for(int i = 0;i<digits.length();i++)
        {
        	num[i] = digits.charAt(i)-'0';
        }
        for(int k = 0;k<digits.length();k++)
        {
        	if(num[k]<2)
        		continue;
        	List<String> midans = new ArrayList<String>();
        	String s1 = ans[num[k]];
        	for(int i = 0;i<result.size();i++)
        	{
        		for(int j = 0;j<s1.length();j++)
        		{
        			midans.add(result.get(i)+s1.charAt(j));
        		}
        	}
        	result = midans;
        }
        return result;
    }
}


相关文章

    暂无相关文章
相关栏目:

用户点评