mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-08 02:15:01 +08:00
19 lines
254 B
Go
19 lines
254 B
Go
package leetcode
|
|
|
|
func countSegments(s string) int {
|
|
segments := false
|
|
cnt := 0
|
|
for _, v := range s {
|
|
if v == ' ' && segments {
|
|
segments = false
|
|
cnt += 1
|
|
} else if v != ' ' {
|
|
segments = true
|
|
}
|
|
}
|
|
if segments {
|
|
cnt++
|
|
}
|
|
return cnt
|
|
}
|