mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
add: leetcode 1816 solution
This commit is contained in:
18
leetcode/1816.Truncate-Sentence/1816.Truncate Sentence.go
Normal file
18
leetcode/1816.Truncate-Sentence/1816.Truncate Sentence.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
func truncateSentence(s string, k int) string {
|
||||||
|
end := 0
|
||||||
|
for i := range s {
|
||||||
|
if k > 0 && s[i] == ' ' {
|
||||||
|
k--
|
||||||
|
}
|
||||||
|
if k == 0 {
|
||||||
|
end = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if end == 0 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return s[:end]
|
||||||
|
}
|
Reference in New Issue
Block a user