Skip to content

Commit 68fadcd

Browse files
authored
Merge pull request #1273 from 0xff-dev/1805
Add solution and test-cases for problem 1805
2 parents 0b7dc09 + bb5aa04 commit 68fadcd

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

leetcode/1801-1900/1805.Number-of-Different-Integers-in-a-String/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [1805.Number of Different Integers in a String][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+
You are given a string `word` that consists of digits and lowercase English letters.
5+
6+
You will replace every non-digit character with a space. For example, `"a123bc34d8ef34"` will become `" 123 34 8 34"`. Notice that you are left with some integers that are separated by at least one space: `"123"`, `"34"`, `"8"`, and `"34"`.
7+
8+
Return the number of **different** integers after performing the replacement operations on `word`.
9+
10+
Two integers are considered different if their decimal representations **without any leading zeros** are different.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: word = "a123bc34d8ef34"
16+
Output: 3
17+
Explanation: The three different integers are "123", "34", and "8". Notice that "34" is only counted once.
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Number of Different Integers in a String
23-
```go
2422
```
23+
Input: word = "leet1234code234"
24+
Output: 2
25+
```
26+
27+
**Example 3:**
2528

29+
```
30+
Input: word = "a1b01c001"
31+
Output: 1
32+
Explanation: The three integers "1", "01", and "001" all represent the same integer because
33+
the leading zeros are ignored when comparing their decimal values.
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(word string) int {
4+
count := make(map[string]struct{})
5+
firstMeet := true
6+
foundNumber := false
7+
start := -1
8+
for i, b := range word {
9+
if b >= '0' && b <= '9' {
10+
foundNumber = true
11+
if firstMeet && b == '0' {
12+
continue
13+
}
14+
if start == -1 {
15+
start = i
16+
}
17+
firstMeet = false
18+
continue
19+
}
20+
if foundNumber {
21+
if start == -1 {
22+
count["0"] = struct{}{}
23+
} else {
24+
count[word[start:i]] = struct{}{}
25+
}
26+
}
27+
foundNumber = false
28+
firstMeet = true
29+
start = -1
30+
}
31+
if foundNumber {
32+
if start == -1 {
33+
count["0"] = struct{}{}
34+
} else {
35+
count[word[start:]] = struct{}{}
36+
}
37+
}
38+
return len(count)
539
}

leetcode/1801-1900/1805.Number-of-Different-Integers-in-a-String/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "a123bc34d8ef34", 3},
17+
{"TestCase2", "leet1234code234", 2},
18+
{"TestCase3", "a1b01c001", 1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)