mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
17 lines
240 B
Go
17 lines
240 B
Go
package leetcode
|
|
|
|
func lengthOfLastWord(s string) int {
|
|
last := len(s) - 1
|
|
for last >= 0 && s[last] == ' ' {
|
|
last--
|
|
}
|
|
if last < 0 {
|
|
return 0
|
|
}
|
|
first := last
|
|
for first >= 0 && s[first] != ' ' {
|
|
first--
|
|
}
|
|
return last - first
|
|
}
|