Skip to content

Commit afc37f9

Browse files
Add Solution.go for 0058.Length of Last Word
1 parent 0ad0fa0 commit afc37f9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func lengthOfLastWord(s string) int {
2+
if len(s) == 0 {
3+
return 0
4+
}
5+
space := []byte(" ")[0]
6+
for len(s) != 0 && s[len(s)-1] == space {
7+
s = s[:len(s)-1]
8+
}
9+
ret := 0
10+
for i := len(s) - 1; i >= 0; i-- {
11+
if s[i] != space {
12+
ret++
13+
} else {
14+
return ret
15+
}
16+
}
17+
return ret
18+
}

0 commit comments

Comments
 (0)