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

Commit 76cfdc9

Browse files
committed
feat: add solution to lc problem: No.1816
No.1816.Truncate Sentence
1 parent 697b934 commit 76cfdc9

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

‎solution/1800-1899/1816.Truncate Sentence/README.md‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ s 中的单词为 ["What", "is" "the", "solution", "to", "this", "problem"]
6464

6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

67+
```python
68+
class Solution:
69+
def truncateSentence(self, s: str, k: int) -> str:
70+
return ' '.join(s.split()[:k])
71+
```
72+
6773
```python
6874
class Solution:
6975
def truncateSentence(self, s: str, k: int) -> str:
@@ -108,6 +114,22 @@ public:
108114
};
109115
```
110116
117+
### **Go**
118+
119+
```go
120+
func truncateSentence(s string, k int) string {
121+
for i, c := range s {
122+
if c == ' ' {
123+
k--
124+
}
125+
if k == 0 {
126+
return s[:i]
127+
}
128+
}
129+
return s
130+
}
131+
```
132+
111133
### **JavaScript**
112134

113135
```js
@@ -116,13 +138,13 @@ public:
116138
* @param {number} k
117139
* @return {string}
118140
*/
119-
var truncateSentence = function (s, k) {
120-
for (let i = 0; i < s.length; ++i) {
121-
if (s[i] == " " && --k == 0) {
122-
return s.substring(0, i);
141+
var truncateSentence = function(s, k) {
142+
for (let i = 0; i < s.length; ++i) {
143+
if (s[i] == ' ' && (--k) == 0) {
144+
return s.slice(0, i);
145+
}
123146
}
124-
}
125-
return s;
147+
return s;
126148
};
127149
```
128150

‎solution/1800-1899/1816.Truncate Sentence/README_EN.md‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ Hence, you should return &quot;What is the solution&quot;.</pre>
5252
<li>There are no leading or trailing spaces.</li>
5353
</ul>
5454

55-
5655
## Solutions
5756

5857
<!-- tabs:start -->
5958

6059
### **Python3**
6160

61+
```python
62+
class Solution:
63+
def truncateSentence(self, s: str, k: int) -> str:
64+
return ' '.join(s.split()[:k])
65+
```
66+
6267
```python
6368
class Solution:
6469
def truncateSentence(self, s: str, k: int) -> str:
@@ -101,6 +106,22 @@ public:
101106
};
102107
```
103108
109+
### **Go**
110+
111+
```go
112+
func truncateSentence(s string, k int) string {
113+
for i, c := range s {
114+
if c == ' ' {
115+
k--
116+
}
117+
if k == 0 {
118+
return s[:i]
119+
}
120+
}
121+
return s
122+
}
123+
```
124+
104125
### **JavaScript**
105126

106127
```js
@@ -109,13 +130,13 @@ public:
109130
* @param {number} k
110131
* @return {string}
111132
*/
112-
var truncateSentence = function (s, k) {
113-
for (let i = 0; i < s.length; ++i) {
114-
if (s[i] == " " && --k == 0) {
115-
return s.substring(0, i);
133+
var truncateSentence = function(s, k) {
134+
for (let i = 0; i < s.length; ++i) {
135+
if (s[i] == ' ' && (--k) == 0) {
136+
return s.slice(0, i);
137+
}
116138
}
117-
}
118-
return s;
139+
return s;
119140
};
120141
```
121142

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func truncateSentence(s string, k int) string {
2+
for i, c := range s {
3+
if c == ' ' {
4+
k--
5+
}
6+
if k == 0 {
7+
return s[:i]
8+
}
9+
}
10+
return s
11+
}

‎solution/1800-1899/1816.Truncate Sentence/Solution.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* @param {number} k
44
* @return {string}
55
*/
6-
var truncateSentence = function(s, k) {
6+
var truncateSentence = function(s, k) {
77
for (let i = 0; i < s.length; ++i) {
8-
if (s[i] == " " && --k == 0) {
9-
return s.substring(0, i);
10-
}
8+
if (s[i] == ' ' && (--k) == 0) {
9+
return s.slice(0, i);
10+
}
1111
}
1212
return s;
1313
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /