ขนาดวิดีโอ: 1280 X 720853 X 480640 X 360
แสดงแผงควบคุมโปรแกรมเล่น
เล่นอัตโนมัติ
เล่นใหม่
import java.util.*;class Solution { public List letterCombinations(String digits) { List result = new ArrayList(); if (digits == null || digits.length() == 0) { return result; } String[] mapping = { "", // 0 "", // 1 "abc", // 2 "def", // 3 "ghi", // 4 "jkl", // 5 "mno", // 6 "pqrs",// 7 "tuv", // 8 "wxyz" // 9 }; backtrack(result, digits, mapping, 0, new StringBuilder()); return result; } private void backtrack(List result, String digits, String[] mapping, int index, StringBuilder current) { if (index == digits.length()) { result.add(current.toString()); return; } String letters = mapping[digits.charAt(index) - '0']; for (char letter : letters.toCharArray()) { current.append(letter); backtrack(result, digits, mapping, index + 1, current); current.deleteCharAt(current.length() - 1); // التراجع عن آخر خطوة } }}
import java.util.*;
class Solution {
public List letterCombinations(String digits) {
List result = new ArrayList();
if (digits == null || digits.length() == 0) {
return result;
}
String[] mapping = {
"", // 0
"", // 1
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs",// 7
"tuv", // 8
"wxyz" // 9
};
backtrack(result, digits, mapping, 0, new StringBuilder());
return result;
}
private void backtrack(List result, String digits, String[] mapping, int index, StringBuilder current) {
if (index == digits.length()) {
result.add(current.toString());
return;
}
String letters = mapping[digits.charAt(index) - '0'];
for (char letter : letters.toCharArray()) {
current.append(letter);
backtrack(result, digits, mapping, index + 1, current);
current.deleteCharAt(current.length() - 1); // التراجع عن آخر خطوة
}
}
}