Skip to content

Add solution and test-cases for problem 726 #933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions leetcode/701-800/0726.Number-of-Atoms/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
# [726.Number of Atoms][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given a string `formula` representing a chemical formula, return the count of each atom.

The atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.

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.

- For example, `"H2O"` and `"H2O2"` are possible, but `"H1O2"` is impossible.

Two formulas are concatenated together to produce another formula.

- For example, `"H2O2He3Mg4"` is also a formula.

A formula placed in parentheses, and a count (optionally added) is also a formula.

- For example, `"(H2O2)"` and `"(H2O2)3"` are formulas.

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.

The test cases are generated so that all the values in the output fit in a **32-bit** integer.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: formula = "H2O"
Output: "H2O"
Explanation: The count of elements are {'H': 2, 'O': 1}.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Number of Atoms
```go
```
Input: formula = "Mg(OH)2"
Output: "H2MgO2"
Explanation: The count of elements are {'H': 2, 'Mg': 1, 'O': 2}.
```

**Example 3:**

```
Input: formula = "K4(ON(SO3)2)2"
Output: "K4N2O14S4"
Explanation: The count of elements are {'K': 4, 'N': 2, 'O': 14, 'S': 4}.
```

## 结语

Expand Down
82 changes: 80 additions & 2 deletions leetcode/701-800/0726.Number-of-Atoms/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
package Solution

func Solution(x bool) bool {
return x
import (
"fmt"
"sort"
"strings"
)

type tmp726 struct {
k string
c int
}

func Solution(formula string) string {
stack := make([]map[string]int, 0)
count := make(map[string]int)
i, l := 0, len(formula)
for i < l {
cur := formula[i]
if cur >= 'A' && cur <= 'Z' {
start := i
i++
// 判断后面是否还跟着小写字母
for ; i < l && formula[i] >= 'a' && formula[i] <= 'z'; i++ {
}
// 一个元素
key := formula[start:i]
// 判断数字
c := 0
for ; i < l && formula[i] >= '0' && formula[i] <= '9'; i++ {
c = c*10 + int(formula[i]-'0')
}

if c == 0 {
c = 1
}
count[key] += c
continue
}
if cur == '(' {
stack = append(stack, count)
count = map[string]int{}
i++
continue
}
pc := 0
i++
for ; i < l && formula[i] >= '0' && formula[i] <= '9'; i++ {
pc = pc*10 + int(formula[i]-'0')
}
if pc == 0 {
pc = 1
}
for k := range count {
count[k] *= pc
}
if tl := len(stack); tl > 0 {
top := stack[tl-1]
stack = stack[:tl-1]
for k, c := range top {
count[k] += c
}
}

}

list := make([]tmp726, 0)
for k, v := range count {
list = append(list, tmp726{k, v})
}
sort.Slice(list, func(i, j int) bool {
return list[i].k < list[j].k
})
buf := strings.Builder{}
for _, i := range list {
w := i.k
if i.c != 1 {
w += fmt.Sprintf("%d", i.c)
}
buf.WriteString(w)
}
return buf.String()
}
16 changes: 9 additions & 7 deletions leetcode/701-800/0726.Number-of-Atoms/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "H2O", "H2O"},
{"TestCase2", "Mg(OH)2", "H2MgO2"},
{"TestCase3", "K4(ON(SO3)2)2", "K4N2O14S4"},
{"TestCase4", "H2(H3)2", "H8"},
{"TestCase5", "H11He49NO35B7N46Li20", "B7H11He49Li20N47O35"},
}

// 开始测试
Expand All @@ -30,10 +32,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading