-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSolution726.java
117 lines (97 loc) · 3.52 KB
/
Solution726.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package leetcode.stack;
import java.util.*;
public class Solution726 {
public static void main(String[] args) {
System.out.println(new Solution726().countOfAtoms("H50"));
}
/**
* 定义节点,保存对应的字母和出现的次数
*/
static class Node implements Comparable<Node> {
String name;
int num;
public Node(String name) {
this.name = name;
}
@Override
public int compareTo(Node o) {
return Character.compare(this.name.charAt(0), o.name.charAt(0));
}
@Override
public String toString() {
if (num == 1) {
return name;
} else {
return name + num;
}
}
}
public String countOfAtoms(String formula) {
// 遇到字母判断后边是否有小写字母,再判断完是否有数字,之后入栈
// 遇到左括号直接入栈,遇到右括号则计算
Stack<Node> stack = new Stack<>();
for (int i = 0; i < formula.length();) {
char c = formula.charAt(i);
if (c == '(') {
stack.push(new Node("("));
i++;
} else if (c >= 'A' && c <= 'Z') {
// 遇到字母开始处理
int nameBeginIndex = i;
i++;
// 是小写字母的话则不断向后匹配
while (i < formula.length() && formula.charAt(i) >= 'a' && formula.charAt(i) <= 'z') {
i++;
}
// 匹配完名字创建该节点
Node node = new Node(formula.substring(nameBeginIndex, i));
// 开始匹配数字
int num = 0;
while (i < formula.length() && formula.charAt(i) >= '0' && formula.charAt(i) <= '9') {
num = num * 10 + (formula.charAt(i) - '0');
i++;
}
num = num == 0 ? 1 : num;
node.num = num;
// 入栈
stack.push(node);
} else if (c == ')') {
ArrayList<Node> temp = new ArrayList<>();
i++;
// 右括号开始计算,直到碰到左括号则停止
int multi = 0;
while (i < formula.length() && formula.charAt(i) >= '0' && formula.charAt(i) <= '9') {
multi = multi * 10 + (formula.charAt(i) - '0');
i++;
}
multi = multi == 0 ? 1 : multi;
while (!"(".equals(stack.peek().name)) {
Node pop = stack.pop();
pop.num = pop.num * multi;
temp.add(pop);
}
// 计算完成后左括号出栈
stack.pop();
// 将节点重新添加到栈中
for (Node node : temp) {
stack.push(node);
}
}
}
TreeMap<String, Node> treeMap = new TreeMap<>();
while (!stack.isEmpty()) {
Node pop = stack.pop();
if (treeMap.containsKey(pop.name)) {
Node node = treeMap.get(pop.name);
node.num += pop.num;
} else {
treeMap.put(pop.name, pop);
}
}
StringBuilder res = new StringBuilder();
for (Node node : treeMap.values()) {
res.append(node.toString());
}
return res.toString();
}
}