-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path726.java
More file actions
133 lines (117 loc) · 4.01 KB
/
726.java
File metadata and controls
133 lines (117 loc) · 4.01 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
__________________________________________________________________________________________________
sample 2 ms submission
/*
* @lc app=leetcode id=726 lang=java
* stack + reverse scan 100%(2ms)
* dfs 99% (3ms)
*
* [726] Number of Atoms
*/
class Solution {
//*
public String countOfAtoms(String formula) {
Map<String, Integer> map = new HashMap<>();
char[] f = formula.toCharArray();
int[] stack = new int[f.length / 3 + 1];
stack[0] = 1; // push initial value 1 to stack
int sIndex = 0;
for (int i = f.length - 1; i >= 0; --i) {
int times = 0, pos = 1;
while (Character.isDigit(f[i])) {
times += pos * (f[i--] - '0');
pos *= 10;
}
times = times == 0 ? stack[sIndex] : stack[sIndex] * times;
if (f[i] == ')') {
stack[++sIndex] = times; // stack push
} else if (f[i] == '(') {
--sIndex; // stack pop
} else {
String key;
if (Character.isLowerCase(f[i])) {
key = "" + f[i-1] + f[i--];
} else {
key = "" + f[i];
}
map.put(key, map.getOrDefault(key, 0) + times);
}
}
List<String> list = new ArrayList<>(map.keySet());
Collections.sort(list);
StringBuilder sb = new StringBuilder();
for (String s : list) {
sb.append(s);
int c = map.get(s);
if (c > 1) sb.append(c);
}
return sb.toString();
}
}
__________________________________________________________________________________________________
sample 36604 kb submission
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
class Solution {
public String countOfAtoms(String formula) {
if (formula == null || formula.length() <= 2) {
return formula;
}
Stack<HashMap<String, Integer>> stack = new Stack<>();
HashMap<String, Integer> map = new HashMap<>();
int len = formula.length();
int i = 0;
while (i < len) {
char c = formula.charAt(i);
i++;
if (c == '(') {
stack.push(map);
map = new HashMap<>();
} else if (c == ')') {
int[] cnt = getCount(i, formula);
i = cnt[0];
int val = cnt[1];
HashMap<String, Integer> temp = map;
map = stack.pop();
for (Map.Entry<String, Integer> e : temp.entrySet()) {
map.put(e.getKey(), map.getOrDefault(e.getKey(), 0) + e.getValue() * val);
}
} else {
int start = i - 1;
while (i < len && Character.isLowerCase(formula.charAt(i))) {
i++;
}
String elem = formula.substring(start, i);
int[] cnt = getCount(i, formula);
i = cnt[0];
int val = cnt[1];
map.put(elem, map.getOrDefault(elem, 0) + val);
}
}
StringBuilder sb = new StringBuilder();
List<String> elems = new ArrayList<>(map.keySet());
Collections.sort(elems);
for (String e : elems) {
sb.append(e);
if (map.get(e) > 1) {
sb.append(map.get(e));
}
}
return sb.toString();
}
private int[] getCount(int idx, String formula) {
int val = 0;
while (idx < formula.length() && Character.isDigit(formula.charAt(idx))) {
val = val * 10 + (formula.charAt(idx) - '0');
idx++;
}
if (val == 0) {
val = 1;
}
return new int[] { idx, val };
}
}
__________________________________________________________________________________________________