Skip to content

Commit b1ddc35

Browse files
authored
Merge pull request halfrost#209 from gostool/leetcode1816
Leetcode1816
2 parents 7d760f1 + 7e4a6e9 commit b1ddc35

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
func truncateSentence(s string, k int) string {
4+
end := 0
5+
for i := range s {
6+
if k > 0 && s[i] == ' ' {
7+
k--
8+
}
9+
if k == 0 {
10+
end = i
11+
break
12+
}
13+
}
14+
if end == 0 {
15+
return s
16+
}
17+
return s[:end]
18+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1816 struct {
9+
para1816
10+
ans1816
11+
}
12+
13+
// para 是参数
14+
type para1816 struct {
15+
s string
16+
k int
17+
}
18+
19+
// ans 是答案
20+
type ans1816 struct {
21+
ans string
22+
}
23+
24+
func Test_Problem1816(t *testing.T) {
25+
26+
qs := []question1816{
27+
28+
{
29+
para1816{"Hello how are you Contestant", 4},
30+
ans1816{"Hello how are you"},
31+
},
32+
33+
{
34+
para1816{"What is the solution to this problem", 4},
35+
ans1816{"What is the solution"},
36+
},
37+
38+
{
39+
para1816{"chopper is not a tanuki", 5},
40+
ans1816{"chopper is not a tanuki"},
41+
},
42+
}
43+
44+
fmt.Printf("------------------------Leetcode Problem 1816------------------------\n")
45+
46+
for _, q := range qs {
47+
_, p := q.ans1816, q.para1816
48+
fmt.Printf("【input】:%v 【output】:%v\n", p, truncateSentence(p.s, p.k))
49+
}
50+
fmt.Printf("\n\n\n")
51+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [1816. Truncate Sentence](https://leetcode-cn.com/problems/truncate-sentence/)
2+
3+
## 题目
4+
5+
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of only uppercase and lowercase English letters (no punctuation).
6+
7+
- For example, "Hello World", "HELLO", and "hello world hello world" are all sentences.
8+
9+
You are given a sentence s and an integer k. You want to truncate s such that it contains only the first k words. Return s after truncating it.
10+
11+
**Example 1**:
12+
13+
Input: s = "Hello how are you Contestant", k = 4
14+
Output: "Hello how are you"
15+
Explanation:
16+
The words in s are ["Hello", "how" "are", "you", "Contestant"].
17+
The first 4 words are ["Hello", "how", "are", "you"].
18+
Hence, you should return "Hello how are you".
19+
20+
**Example 2**:
21+
22+
Input: s = "What is the solution to this problem", k = 4
23+
Output: "What is the solution"
24+
Explanation:
25+
The words in s are ["What", "is" "the", "solution", "to", "this", "problem"].
26+
The first 4 words are ["What", "is", "the", "solution"].
27+
Hence, you should return "What is the solution".
28+
29+
**Example 3**:
30+
31+
Input: s = "chopper is not a tanuki", k = 5
32+
Output: "chopper is not a tanuki"
33+
34+
**Constraints:**
35+
36+
- 1 <= s.length <= 500
37+
- k is in the range [1, the number of words in s].
38+
- s consist of only lowercase and uppercase English letters and spaces.
39+
- The words in s are separated by a single space.
40+
- There are no leading or trailing spaces.
41+
42+
## 题目大意
43+
44+
句子 是一个单词列表,列表中的单词之间用单个空格隔开,且不存在前导或尾随空格。每个单词仅由大小写英文字母组成(不含标点符号)。
45+
46+
- 例如,"Hello World"、"HELLO" 和 "hello world hello world" 都是句子。
47+
48+
给你一个句子 s 和一个整数 k ,请你将 s 截断使截断后的句子仅含前 k 个单词。返回截断 s 后得到的句子。
49+
50+
## 解题思路
51+
52+
- 遍历字符串s,找到最后一个空格的下标end
53+
- 如果end为0,直接返回s,否则返回s[:end]
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
func truncateSentence(s string, k int) string {
61+
end := 0
62+
for i := range s {
63+
if k > 0 && s[i] == ' ' {
64+
k--
65+
}
66+
if k == 0 {
67+
end = i
68+
break
69+
}
70+
}
71+
if end == 0 {
72+
return s
73+
}
74+
return s[:end]
75+
}
76+
```

0 commit comments

Comments
 (0)