Skip to content

Commit ca755e9

Browse files
authored
Merge pull request #933 from 0xff-dev/726
Add solution and test-cases for problem 726
2 parents 8de7366 + 1c0a3fd commit ca755e9

File tree

3 files changed

+123
-22
lines changed

3 files changed

+123
-22
lines changed

leetcode/701-800/0726.Number-of-Atoms/README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
# [726.Number of Atoms][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a string `formula` representing a chemical formula, return the count of each atom.
5+
6+
The atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.
7+
8+
One or more digits representing that element's count may follow if the count is greater than `1`. If the count is `1`, no digits will follow.
9+
10+
- For example, `"H2O"` and `"H2O2"` are possible, but `"H1O2"` is impossible.
11+
12+
Two formulas are concatenated together to produce another formula.
13+
14+
- For example, `"H2O2He3Mg4"` is also a formula.
15+
16+
A formula placed in parentheses, and a count (optionally added) is also a formula.
17+
18+
- For example, `"(H2O2)"` and `"(H2O2)3"` are formulas.
19+
20+
Return the count of all elements as a string in the following form: the first name (in sorted order), followed by its count (if that count is more than `1`), followed by the second name (in sorted order), followed by its count (if that count is more than `1`), and so on.
21+
22+
The test cases are generated so that all the values in the output fit in a **32-bit** integer.
723

824
**Example 1:**
925

1026
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
27+
Input: formula = "H2O"
28+
Output: "H2O"
29+
Explanation: The count of elements are {'H': 2, 'O': 1}.
1330
```
1431

15-
## 题意
16-
> ...
32+
**Example 2:**
1733

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Number of Atoms
23-
```go
34+
```
35+
Input: formula = "Mg(OH)2"
36+
Output: "H2MgO2"
37+
Explanation: The count of elements are {'H': 2, 'Mg': 1, 'O': 2}.
2438
```
2539

40+
**Example 3:**
41+
42+
```
43+
Input: formula = "K4(ON(SO3)2)2"
44+
Output: "K4N2O14S4"
45+
Explanation: The count of elements are {'K': 4, 'N': 2, 'O': 14, 'S': 4}.
46+
```
2647

2748
## 结语
2849

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,83 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
)
8+
9+
type tmp726 struct {
10+
k string
11+
c int
12+
}
13+
14+
func Solution(formula string) string {
15+
stack := make([]map[string]int, 0)
16+
count := make(map[string]int)
17+
i, l := 0, len(formula)
18+
for i < l {
19+
cur := formula[i]
20+
if cur >= 'A' && cur <= 'Z' {
21+
start := i
22+
i++
23+
// 判断后面是否还跟着小写字母
24+
for ; i < l && formula[i] >= 'a' && formula[i] <= 'z'; i++ {
25+
}
26+
// 一个元素
27+
key := formula[start:i]
28+
// 判断数字
29+
c := 0
30+
for ; i < l && formula[i] >= '0' && formula[i] <= '9'; i++ {
31+
c = c*10 + int(formula[i]-'0')
32+
}
33+
34+
if c == 0 {
35+
c = 1
36+
}
37+
count[key] += c
38+
continue
39+
}
40+
if cur == '(' {
41+
stack = append(stack, count)
42+
count = map[string]int{}
43+
i++
44+
continue
45+
}
46+
pc := 0
47+
i++
48+
for ; i < l && formula[i] >= '0' && formula[i] <= '9'; i++ {
49+
pc = pc*10 + int(formula[i]-'0')
50+
}
51+
if pc == 0 {
52+
pc = 1
53+
}
54+
for k := range count {
55+
count[k] *= pc
56+
}
57+
if tl := len(stack); tl > 0 {
58+
top := stack[tl-1]
59+
stack = stack[:tl-1]
60+
for k, c := range top {
61+
count[k] += c
62+
}
63+
}
64+
65+
}
66+
67+
list := make([]tmp726, 0)
68+
for k, v := range count {
69+
list = append(list, tmp726{k, v})
70+
}
71+
sort.Slice(list, func(i, j int) bool {
72+
return list[i].k < list[j].k
73+
})
74+
buf := strings.Builder{}
75+
for _, i := range list {
76+
w := i.k
77+
if i.c != 1 {
78+
w += fmt.Sprintf("%d", i.c)
79+
}
80+
buf.WriteString(w)
81+
}
82+
return buf.String()
583
}

leetcode/701-800/0726.Number-of-Atoms/Solution_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "H2O", "H2O"},
17+
{"TestCase2", "Mg(OH)2", "H2MgO2"},
18+
{"TestCase3", "K4(ON(SO3)2)2", "K4N2O14S4"},
19+
{"TestCase4", "H2(H3)2", "H8"},
20+
{"TestCase5", "H11He49NO35B7N46Li20", "B7H11He49Li20N47O35"},
1921
}
2022

2123
// 开始测试
@@ -30,10 +32,10 @@ func TestSolution(t *testing.T) {
3032
}
3133
}
3234

33-
// 压力测试
35+
// 压力测试
3436
func BenchmarkSolution(b *testing.B) {
3537
}
3638

37-
// 使用案列
39+
// 使用案列
3840
func ExampleSolution() {
3941
}

0 commit comments

Comments
 (0)