Skip to content

Commit fadd744

Browse files
committed
feat: Solved 17
1 parent 5139aff commit fadd744

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fghpdf.LetterCombinationsOfAPhoneNumber;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* @author fghpdf
8+
* @date 2019/11/14
9+
* https://leetcode.com/problems/letter-combinations-of-a-phone-number/
10+
*
11+
* FIFO
12+
* we first build a queue to save letter
13+
* when first letter pushed into queue, we should remove it and get it and then
14+
* loop next char array
15+
* like a -> queue
16+
* remove a <- queue
17+
* a + d -> queue
18+
*
19+
* notice the judge is queue.peek().length == i
20+
* i is the letter position number
21+
* like before first letter join, the i is 0, the queue peek is empty string
22+
**/
23+
public class Solution {
24+
public List<String> letterCombinations(String digits) {
25+
LinkedList<String> result = new LinkedList<>();
26+
if (digits.isEmpty()) {
27+
return result;
28+
}
29+
30+
String[] digitsMap = new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
31+
result.add("");
32+
for (int i = 0; i < digits.length(); i++) {
33+
int x = Character.getNumericValue(digits.charAt(i));
34+
while (result.peek().length() == i) {
35+
String temp = result.remove();
36+
for (char s : digitsMap[x].toCharArray()) {
37+
result.add(temp + s);
38+
}
39+
}
40+
}
41+
return result;
42+
}
43+
44+
public static void main(String[] args) {
45+
Solution sol = new Solution();
46+
System.out.println(sol.letterCombinations("235"));
47+
}
48+
}

0 commit comments

Comments
 (0)