Golang Leetcode 434. Number of Segments in a String.go

2019-04-12 14:16:01 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412977

思路

暴力破解

code

代码语言:javascript复制
func countSegments(s string) int {

	count := 1
	found := false
	for i := 0; i < len(s); i   {
		if i > 0 && s[i] == ' ' && s[i-1] != ' ' {
			count  
		}
		if s[i] != ' ' {
			found = true
		}
		if i == len(s)-1 && s[i] == ' ' {
			count--
		}
	}
	if found {
		return count
	}
	return 0
}

0 人点赞