题目链接: https://leetcode.cn/problems/length-of-last-word
- 遍历字符串,查找两段空格之间的长度即可
func lengthOfLastWord(s string) int {
length := 0
tmpLength := 0
for _, item := range s {
if item == ' ' {
if tmpLength != 0 {
length = tmpLength
}
tmpLength = 0
continue
} else {
tmpLength++
}
}
if tmpLength != 0 {
length = tmpLength
}
return length
}
也可以直接使用内置函数
func lengthOfLastWord(s string) int {
arr := strings.Fields(s)
return len(arr[len(arr) - 1])
}
-
时间复杂度: 只遍历了一遍数组
$$s$$ ,因此时间复杂度为$$O(n)$$ -
空间复杂度: 只使用了常数个变量,因此空间复杂度为
$$O(1)$$