Leetcode Note: Go - Number of Segments in a String

Number of Segments in a String - LeetCode
https://leetcode.com/problems/number-of-segments-in-a-string/

  • Go 言語で取り組んだメモ

所感

  • 渡された non space characters な string をセパレートしてセグメント数を int で return する

回答

Number of Segments in a String - LeetCode
https://leetcode.com/problems/number-of-segments-in-a-string/solution/

Golang 100% - LeetCode Discuss
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1507688/Golang-100

func countSegments(s string) (ans int) {
    s += " "
    for i := 0; i < len(s); i++ {
        if s[i] == ' ' {
            continue
        }
        for s[i] != ' ' {
            i++
        }
        ans++
    }
    return
}