Skip to content

Commit eca2a78

Browse files
authored
feat: add java solution to lc problem: No.0726 (doocs#3271)
1 parent 296498d commit eca2a78

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

solution/0700-0799/0726.Number of Atoms/README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,49 @@ tags:
9898
#### Java
9999

100100
```java
101-
101+
class Solution {
102+
public String countOfAtoms(String formula) {
103+
Map<String, Integer> map = new HashMap<>();
104+
int[] stack = new int[1000];
105+
int top = 0, multiplier = 1, freq = 0;
106+
char[] c = formula.toCharArray();
107+
for (int i = c.length - 1; i >= 0; i--) {
108+
if (c[i] >= 'a' && c[i] <= 'z') {
109+
int end = i--;
110+
while (i >= 0 && c[i] >= 'a' && c[i] <= 'z') i--;
111+
String key = new String(c, i, end - i + 1);
112+
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
113+
freq = 0;
114+
} else if (c[i] >= 'A' && c[i] <= 'Z') {
115+
String key = new String(c, i, 1);
116+
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
117+
freq = 0;
118+
} else if (c[i] >= '0' && c[i] <= '9') {
119+
freq = c[i] - '0';
120+
int p = 10;
121+
while (i - 1 >= 0 && c[i - 1] >= '0' && c[i - 1] <= '9') {
122+
freq += p * (c[--i] - '0');
123+
p *= 10;
124+
}
125+
} else if (c[i] == ')') {
126+
stack[top++] = multiplier;
127+
multiplier *= Math.max(freq, 1);
128+
freq = 0;
129+
} else {
130+
multiplier = stack[--top];
131+
}
132+
}
133+
List<String> keys = new ArrayList<>(map.keySet());
134+
Collections.sort(keys);
135+
StringBuilder sb = new StringBuilder();
136+
for (String key : keys) {
137+
sb.append(key);
138+
int f = map.get(key);
139+
if (f > 1) sb.append(f);
140+
}
141+
return sb.toString();
142+
}
143+
}
102144
```
103145

104146
#### C++

solution/0700-0799/0726.Number of Atoms/README_EN.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,49 @@ tags:
9898
#### Java
9999

100100
```java
101-
101+
class Solution {
102+
public String countOfAtoms(String formula) {
103+
Map<String, Integer> map = new HashMap<>();
104+
int[] stack = new int[1000];
105+
int top = 0, multiplier = 1, freq = 0;
106+
char[] c = formula.toCharArray();
107+
for (int i = c.length - 1; i >= 0; i--) {
108+
if (c[i] >= 'a' && c[i] <= 'z') {
109+
int end = i--;
110+
while (i >= 0 && c[i] >= 'a' && c[i] <= 'z') i--;
111+
String key = new String(c, i, end - i + 1);
112+
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
113+
freq = 0;
114+
} else if (c[i] >= 'A' && c[i] <= 'Z') {
115+
String key = new String(c, i, 1);
116+
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
117+
freq = 0;
118+
} else if (c[i] >= '0' && c[i] <= '9') {
119+
freq = c[i] - '0';
120+
int p = 10;
121+
while (i - 1 >= 0 && c[i - 1] >= '0' && c[i - 1] <= '9') {
122+
freq += p * (c[--i] - '0');
123+
p *= 10;
124+
}
125+
} else if (c[i] == ')') {
126+
stack[top++] = multiplier;
127+
multiplier *= Math.max(freq, 1);
128+
freq = 0;
129+
} else {
130+
multiplier = stack[--top];
131+
}
132+
}
133+
List<String> keys = new ArrayList<>(map.keySet());
134+
Collections.sort(keys);
135+
StringBuilder sb = new StringBuilder();
136+
for (String key : keys) {
137+
sb.append(key);
138+
int f = map.get(key);
139+
if (f > 1) sb.append(f);
140+
}
141+
return sb.toString();
142+
}
143+
}
102144
```
103145

104146
#### C++
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public String countOfAtoms(String formula) {
3+
Map<String, Integer> map = new HashMap<>();
4+
int[] stack = new int[1000];
5+
int top = 0, multiplier = 1, freq = 0;
6+
char[] c = formula.toCharArray();
7+
for (int i = c.length - 1; i >= 0; i--) {
8+
if (c[i] >= 'a' && c[i] <= 'z') {
9+
int end = i--;
10+
while (i >= 0 && c[i] >= 'a' && c[i] <= 'z') i--;
11+
String key = new String(c, i, end - i + 1);
12+
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
13+
freq = 0;
14+
} else if (c[i] >= 'A' && c[i] <= 'Z') {
15+
String key = new String(c, i, 1);
16+
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
17+
freq = 0;
18+
} else if (c[i] >= '0' && c[i] <= '9') {
19+
freq = c[i] - '0';
20+
int p = 10;
21+
while (i - 1 >= 0 && c[i - 1] >= '0' && c[i - 1] <= '9') {
22+
freq += p * (c[--i] - '0');
23+
p *= 10;
24+
}
25+
} else if (c[i] == ')') {
26+
stack[top++] = multiplier;
27+
multiplier *= Math.max(freq, 1);
28+
freq = 0;
29+
} else {
30+
multiplier = stack[--top];
31+
}
32+
}
33+
List<String> keys = new ArrayList<>(map.keySet());
34+
Collections.sort(keys);
35+
StringBuilder sb = new StringBuilder();
36+
for (String key : keys) {
37+
sb.append(key);
38+
int f = map.get(key);
39+
if (f > 1) sb.append(f);
40+
}
41+
return sb.toString();
42+
}
43+
}

0 commit comments

Comments
 (0)