-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfAtoms.java
More file actions
147 lines (130 loc) · 4.57 KB
/
NumberOfAtoms.java
File metadata and controls
147 lines (130 loc) · 4.57 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package leetcode;
import java.util.*;
/**
* NumberOfAtoms
* https://leetcode-cn.com/problems/number-of-atoms/
* 726. 原子的数量
* https://leetcode-cn.com/problems/number-of-atoms/solution/zhan-de-shi-yong-jie-jue-gua-hao-pi-pei-esprf/
*
* @author tobin
* @since 2021-07-05
*/
public class NumberOfAtoms {
public static void main(String[] args) {
NumberOfAtoms sol = new NumberOfAtoms();
System.out.println(sol.countOfAtoms("Be32"));
System.out.println(sol.countOfAtoms("K4(ON(SO3)2)2"));
System.out.println(sol.countOfAtoms("Mg(OH)2"));
System.out.println(sol.countOfAtoms("H2O"));
System.out.println(sol.countOfAtoms("Mg(H2O)N"));
}
private class KeyCount {
public String key;
public int count;
public KeyCount(String key, int count) {
this.key = key;
this.count = count;
}
}
public String countOfAtoms(String formula) {
List<KeyCount> stack = new ArrayList<>();
// 表达式解释为数量
StringBuilder curr = new StringBuilder();
int currCount = 0;
boolean waitForNumber = false;
int number = 0;
for (int i = 0; i < formula.length(); i++) {
char c = formula.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// 非终止符号
if (c >= 'a' && c <= 'z') {
curr.append(c);
} else {
if (waitForNumber) {
number = number * 10 + (c - '0');
} else {
currCount = currCount * 10 + (c - '0');
}
}
} else {
// 终止符号
if (waitForNumber) {
if (number < 1) {
number = 1; // bug
}
for (int idx = stack.size() - 1; idx >= 0; idx--) {
if (stack.get(idx).count < 0) {
stack.remove(idx);
break;
}
stack.get(idx).count = stack.get(idx).count * number;
}
waitForNumber = false;
}
if (curr.length() > 0) {
if (currCount == 0) {
currCount = 1;
}
stack.add(new KeyCount(curr.toString(), currCount));
curr = new StringBuilder();
currCount = 0;
}
if (c >= 'A' && c <= 'Z') {
curr.append(c);
} else if (c == '(') {
stack.add(new KeyCount("", -1));
} else if (c == ')') {
waitForNumber = true;
number = 0;
}
}
}
// 表达式收尾
if (waitForNumber) {
if (number < 1) {
number = 1; // bug
}
for (int idx = stack.size() - 1; idx >= 0; idx--) {
if (stack.get(idx).count < 0) {
stack.remove(idx);
break;
}
stack.get(idx).count = stack.get(idx).count * number;
}
}
if (curr.length() > 0) {
if (currCount == 0) {
currCount = 1;
}
stack.add(new KeyCount(curr.toString(), currCount));
}
Map<String, Integer> keyCounts = new HashMap<>();
// 汇总统计
for (KeyCount keyCount : stack) {
int newCount = keyCount.count;
if (keyCounts.containsKey(keyCount.key)) {
newCount += keyCounts.get(keyCount.key);
}
keyCounts.put(keyCount.key, newCount);
}
// 字典序
List<KeyCount> resultList = new ArrayList<>();
for (Map.Entry<String, Integer> entry : keyCounts.entrySet()) {
resultList.add(new KeyCount(entry.getKey(), entry.getValue()));
}
Collections.sort(resultList, new Comparator<KeyCount>() {
@Override
public int compare(KeyCount a, KeyCount b) {
return a.key.compareTo(b.key);
}
});
StringBuilder result = new StringBuilder();
for (KeyCount keyCount : resultList) {
result.append(keyCount.key);
if (keyCount.count > 1) {
result.append(keyCount.count);
}
}
return result.toString();
}
}