Skip to content

Commit 6343775

Browse files
authored
Merge branch 'doocs:main' into main
2 parents f6ac07a + 6966d28 commit 6343775

File tree

73 files changed

+2303
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2303
-3
lines changed

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

Lines changed: 43 additions & 1 deletion
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

Lines changed: 43 additions & 1 deletion
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++
Lines changed: 43 additions & 0 deletions
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+
}

solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3184.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20I/README.md
5+
rating: 1149
6+
source: 第 402 场周赛 Q1
57
tags:
68
- 数组
79
- 哈希表

solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3184.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20I/README_EN.md
5+
rating: 1149
6+
source: Weekly Contest 402 Q1
57
tags:
68
- Array
79
- Hash Table

solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3185.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20II/README.md
5+
rating: 1385
6+
source: 第 402 场周赛 Q2
57
tags:
68
- 数组
79
- 哈希表

solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3185.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20II/README_EN.md
5+
rating: 1385
6+
source: Weekly Contest 402 Q2
57
tags:
68
- Array
79
- Hash Table

solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3186.Maximum%20Total%20Damage%20With%20Spell%20Casting/README.md
5+
rating: 1840
6+
source: 第 402 场周赛 Q3
57
tags:
68
- 数组
79
- 哈希表

solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3186.Maximum%20Total%20Damage%20With%20Spell%20Casting/README_EN.md
5+
rating: 1840
6+
source: Weekly Contest 402 Q3
57
tags:
68
- Array
79
- Hash Table

solution/3100-3199/3187.Peaks in Array/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3187.Peaks%20in%20Array/README.md
5+
rating: 2154
6+
source: 第 402 场周赛 Q4
57
tags:
68
- 树状数组
79
- 线段树

0 commit comments

Comments
 (0)