Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfe065d8

Browse files
committed
Add solution and test-cases for problem 443
1 parentd6fa07d commitfe065d8

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

‎leetcode/401-500/0443.String-Compression/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
#[443.String Compression][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 an array of characters`chars`, compress it using the following algorithm:
5+
6+
Begin with an empty string s. For each group of**consecutive repeating characters** in`chars`:
7+
8+
- If the group's length is`1`, append the character to`s`.
9+
- Otherwise, append the character followed by the group's length.
10+
11+
The compressed string`s`**should not be returned separately**, but instead, be stored**in the input character array**`chars`. Note that group lengths that are 10 or longer will be split into multiple characters in`chars`.
12+
13+
After you are done**modifying the input array**, return the new length of the array.
14+
15+
You must write an algorithm that uses only constant extra space.
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: chars = ["a","a","b","b","c","c","c"]
21+
Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
22+
Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
1323
```
1424

15-
##题意
16-
>...
25+
**Example 2:**
1726

18-
##题解
19-
20-
###思路1
21-
>...
22-
String Compression
23-
```go
2427
```
28+
Input: chars = ["a"]
29+
Output: Return 1, and the first character of the input array should be: ["a"]
30+
Explanation: The only group is "a", which remains uncompressed since it's a single character.
31+
```
32+
33+
**Example 3:**
2534

35+
```
36+
Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
37+
Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
38+
Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
39+
```
2640

2741
##结语
2842

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

3-
funcSolution(xbool)bool {
4-
returnx
3+
import"fmt"
4+
5+
funcSolution(chars []byte)int {
6+
length:=len(chars)
7+
iflength==0 {
8+
returnlength
9+
}
10+
nowByte,sameCount:=chars[0],1
11+
targetIndex:=0
12+
foridx:=1;idx<=length;idx++ {
13+
ifidx<length&&chars[idx]==nowByte {
14+
sameCount++
15+
continue
16+
}
17+
chars[targetIndex]=nowByte
18+
targetIndex++
19+
ifidx<length {
20+
nowByte=chars[idx]
21+
}
22+
ifsameCount==1 {
23+
continue
24+
}
25+
for_,b:=rangefmt.Sprintf("%d",sameCount) {
26+
chars[targetIndex]=byte(b)
27+
targetIndex++
28+
}
29+
sameCount=1
30+
}
31+
returntargetIndex
532
}

‎leetcode/401-500/0443.String-Compression/Solution_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
funcTestSolution(t*testing.T) {
1010
//测试用例
1111
cases:= []struct {
12-
namestring
13-
inputsbool
14-
expectbool
12+
namestring
13+
inputs []byte
14+
expect1 []byte
15+
expectint
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase1",[]byte{'a','a','b','b','c','c','c'}, []byte{'a','2','b','2','c','3'},6},
18+
{"TestCase2",[]byte{'a'}, []byte{'a'},1},
19+
{"TestCase3",[]byte{'a','b','b','b','b','b','b','b','b','b','b','b','b'}, []byte{'a','b','1','2'},4},
1920
}
2021

2122
//开始测试
@@ -26,14 +27,17 @@ func TestSolution(t *testing.T) {
2627
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2728
c.expect,got,c.inputs)
2829
}
30+
if!reflect.DeepEqual(c.inputs[:got],c.expect1) {
31+
t.Fatalf("expect %v but got: %v",c.expect1,c.inputs[:got])
32+
}
2933
})
3034
}
3135
}
3236

33-
//压力测试
37+
//压力测试
3438
funcBenchmarkSolution(b*testing.B) {
3539
}
3640

37-
//使用案列
41+
//使用案列
3842
funcExampleSolution() {
3943
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp