๐ 17. ็ต่ฏๅท็ ็ๅญๆฏ็ปๅ
2022ๅนด6ๆ9ๆฅ
- algorithm
๐ 17. ็ต่ฏๅท็ ็ๅญๆฏ็ปๅ
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
List<String> res = new ArrayList<>();
String[] array = new String[] {"#", "#", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
// ๆ่ทฏ:
// ๅฐ็ต่ฏๅท็ ไฟกๆฏๅญๅ
ฅๆฐ็ป
// ๅๆบฏ
int len = digits.length();
// ็นๆฎๆ
ๅต็นๅค
if(len == 0) {
return res;
}
LinkedList<Character> path = new LinkedList<>();
mySol(digits, len, 0, path);
return res;
}
private void mySol(String digits, int len, int index, LinkedList<Character> path) {
// ้ๅฝ็ปๆญขๆกไปถ
if(index == len) {
res.add(getStr(path));
return;
}
// index < len
String str = array[digits.charAt(index) - '0'];
for(int i = 0; i < str.length(); i ++) {
path.addLast(str.charAt(i));
mySol(digits, len, index + 1, path);
path.removeLast();
}
}
private String getStr(LinkedList<Character> path) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < path.size(); i ++) {
sb.append(path.get(i));
}
return sb.toString();
}
}