add: leetcode 1816 solution

This commit is contained in:
tphyhFighting
2021-12-06 11:09:24 +08:00
parent a45992e7de
commit 5bfcf3f0c1

View 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]
}