Skip to content

Commit 35799fb

Browse files
authored
Merge pull request 6boris#241 from 0xff-dev/763
Add solution and test-cases for problem 763
2 parents af242e1 + fe90675 commit 35799fb

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

leetcode/701-800/0763.Partition-Labels/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [763.Partition Labels][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 `s`. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
5+
6+
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be `s`.
7+
8+
Return a list of integers representing the size of these parts.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "ababcbacadefegdehijhklij"
14+
Output: [9,7,8]
15+
Explanation:
16+
The partition is "ababcbaca", "defegde", "hijhklij".
17+
This is a partition so that each letter appears in at most one part.
18+
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Partition Labels
23-
```go
2423
```
25-
24+
Input: s = "eccbbbbdec"
25+
Output: [10]
26+
```
2627

2728
## 结语
2829

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) []int {
4+
partLength := make([]int, 0)
5+
charsLastIndex := make([]int, 26)
6+
for idx, b := range s {
7+
charsLastIndex[b-'a'] = idx
8+
}
9+
10+
lastIdx, length := -1, 0
11+
for idx := 0; idx < len(s); idx++ {
12+
if charsLastIndex[s[idx]-'a'] > lastIdx {
13+
lastIdx = charsLastIndex[s[idx]-'a']
14+
}
15+
if idx == lastIdx {
16+
partLength = append(partLength, length+1)
17+
length = 0
18+
continue
19+
}
20+
length++
21+
}
22+
return partLength
523
}

leetcode/701-800/0763.Partition-Labels/Solution_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ 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", "ababcbacadefegdehijhklij", []int{9, 7, 8}},
17+
{"TestCase2", "eccbbbbdec", []int{10}},
18+
{"TestCase3", "abc", []int{1, 1, 1}},
19+
{"TestCase4", "aabbadec", []int{5, 1, 1, 1}},
1920
}
2021

2122
// 开始测试

0 commit comments

Comments
 (0)