给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:输入:digits = ""
输出:[]
示例 3:输入:digits = "2"
输出:["a","b","c"]
提示:
0 <= digits.length <= 4
digits[i] 是范围 ['2', '9'] 的一个数字。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:我讨厌递归,我是不会用递归的,sowaht?
class Solution {
String[] strings = new String[]{"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<>();
List<String> listb = new ArrayList<>();
List<String> listc = new ArrayList<>();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < digits.length(); i++) { //找到所有的组合
if (digits.charAt(i) <= '9' && digits.charAt(i) > '1') {
stringBuffer.append(digits.charAt(i));
}
}
stringBuffer.toString();
for (int i = 0; i < stringBuffer.length(); i++) {
listc.clear();
for(int l=0;l<listb.size();l++)
listc.add(listb.get(l));
listb.clear();
for (int j = 0;j==0||j < listc.size(); j++) {
for (int k = 0; k < strings[stringBuffer.charAt(i) - '0' - 2].length(); k++) {
String stringBuffer1 = new String();
if (j != 0 || listc.size()!=0) {
stringBuffer1= String.valueOf(listc.get(j));
}
stringBuffer1=stringBuffer1+strings[stringBuffer.charAt(i) - '0' - 2].charAt(k);
listb.add(stringBuffer1);
}
}
}
return listb;
}
}