-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpand.java
More file actions
69 lines (62 loc) · 1.86 KB
/
Expand.java
File metadata and controls
69 lines (62 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
/**
* 1087. 花括号展开
*/
public class Expand {
public String[] expand(String s) {
List<List<Character>> temp = new ArrayList<>();
decode(s, temp);
List<String> resList = new ArrayList<>();
backtrack(0, temp, new StringBuilder(),resList);
String[] res = new String[resList.size()];
for(int i=0; i<resList.size(); i++){
res[i] = resList.get(i);
}
return res;
}
void decode(String s, List<List<Character>> temp){
boolean left = false;
Queue<Character> queue = new PriorityQueue<>(
(a, b) -> {
return a-b;
}
);
for(char c: s.toCharArray()){
if(c == '{'){
left = true;
}else if(c == '}'){
List<Character> t = new ArrayList<>();
while (!queue.isEmpty()){
t.add(queue.poll());
}
temp.add(t);
left = false;
}else if(c == ','){
continue;
}else {
if (left) {
// 形如{a,b}c{d,e}中的a,b和d,e
queue.add(c);
}else{
// 形如{a,b}c{d,e}中的c
List<Character> t = new ArrayList<>();
t.add(c);
temp.add(t);
}
}
}
}
void backtrack(int start, List<List<Character>> temp, StringBuilder s, List<String> res){
if(start>temp.size()){
return;
}else if(start==temp.size()){
res.add(new String(s));
return;
}
for(char c: temp.get(start)){
s.append(c);
backtrack(start+1, temp, s, res);
s.deleteCharAt(s.length()-1);
}
}
}