-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution434.go
More file actions
44 lines (37 loc) · 1.01 KB
/
solution434.go
File metadata and controls
44 lines (37 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package solution434
import "strings"
// ============================================================================
// 434. Number of Segments in a String
// URL: https://leetcode.com/problems/number-of-segments-in-a-string/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/434---Number-of-Segments-in-a-String
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_missingNumber-24 56915252 20.70 ns/op 0 B/op 0 allocs/op
Benchmark_missingNumber_strings_fields-24 22404073 57.33 ns/op 80 B/op 1 allocs/op
PASS
*/
func countSegments(s string) int {
if len(s) == 0 {
return 0
}
ans := 0
flag := false
for _, ch := range s {
if !flag && ch != ' ' {
flag = true
ans++
continue
}
if flag && ch == ' ' {
flag = false
}
}
return ans
}
func countSegments_strings_fields(s string) int {
return len(strings.Fields(s))
}