Skip to content

Commit 3705153

Browse files
authored
Merge pull request #1050 from 0xff-dev/1324
Add solution and test-cases for problem 1324
2 parents 8b2d9dc + 0a7b582 commit 3705153

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

leetcode/1301-1400/1324.Print-Words-Vertically/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [1324.Print Words Vertically][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 `s`. Return all the words vertically in the same order in which they appear in `s`.
5+
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
6+
Each word would be put on only one column and that in one column there will be only one word.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: s = "HOW ARE YOU"
12+
Output: ["HAY","ORO","WEU"]
13+
Explanation: Each word is printed vertically.
14+
"HAY"
15+
"ORO"
16+
"WEU"
1317
```
1418

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

20-
### 思路1
21-
> ...
22-
Print Words Vertically
23-
```go
21+
```
22+
Input: s = "TO BE OR NOT TO BE"
23+
Output: ["TBONTB","OEROOE"," T"]
24+
Explanation: Trailing spaces is not allowed.
25+
"TBONTB"
26+
"OEROOE"
27+
" T"
2428
```
2529

30+
**Example 3:**
31+
32+
```
33+
Input: s = "CONTEST IS COMING"
34+
Output: ["CIC","OSO","N M","T I","E N","S G","T"]
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(s string) []string {
6+
words := strings.Split(s, " ")
7+
ml := 0
8+
for _, w := range words {
9+
ml = max(ml, len(w))
10+
}
11+
ans := make([]string, 0)
12+
buf := strings.Builder{}
13+
for index := 0; index < ml; index++ {
14+
tailSpaces := 0
15+
for _, w := range words {
16+
if index < len(w) {
17+
buf.WriteByte(w[index])
18+
tailSpaces = 0
19+
continue
20+
}
21+
buf.WriteByte(' ')
22+
tailSpaces++
23+
}
24+
cur := buf.String()
25+
l := len(cur) - tailSpaces
26+
cur = cur[:l]
27+
ans = append(ans, cur)
28+
buf.Reset()
29+
}
30+
return ans
531
}

leetcode/1301-1400/1324.Print-Words-Vertically/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 []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "HOW ARE YOU", []string{"HAY", "ORO", "WEU"}},
17+
{"TestCase2", "TO BE OR NOT TO BE", []string{"TBONTB", "OEROOE", " T"}},
18+
{"TestCase3", "CONTEST IS COMING", []string{"CIC", "OSO", "N M", "T I", "E N", "S G", "T"}},
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)