-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path58-LengthofLastWord.go
More file actions
109 lines (94 loc) · 3.23 KB
/
58-LengthofLastWord.go
File metadata and controls
109 lines (94 loc) · 3.23 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
// 58. Length of Last Word
// Given a string s consisting of some words separated by some number of spaces,
// return the length of the last word in the string.
// A word is a maximal substring consisting of non-space characters only.
// Constraints:
// 1 <= s.length <= 10^4
// s consists of only English letters and spaces ' '.
// There will be at least one word in s.
// Example 1:
// Input: s = "Hello World"
// Output: 5
// Explanation: The last word is "World" with length 5.
// Example 2:
// Input: s = " fly me to the moon "
// Output: 4
// Explanation: The last word is "moon" with length 4.
// Example 3:
// Input: s = "luffy is still joyboy"
// Output: 6
// Explanation: The last word is "joyboy" with length 6.
import "fmt"
import "strings"
// with strings lib
func lengthOfLastWord(s string) int {
s = strings.TrimSpace(s)
if "" == s {
return 0
}
parts := strings.Fields(s)
return len(parts[len(parts) - 1])
}
func lengthOfLastWord1(s string) int {
var w = 0
s = strings.TrimSpace(s)
for i := 0; i < len(s); i++ {
if ' ' == s[i] {
w = 0
continue // 不执行到下面了
}
w++
}
return w
}
// without trim
func lengthOfLastWord3(s string) int {
var w = 0
var m = 0
for i := 0; i < len(s); i++ {
if ' ' == s[i] {
w = 0
} else {
w++
m = w
}
}
return m
}
func lengthOfLastWord2(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
}
func main() {
fmt.Printf("lengthOfLastWord3(\"Hello World\") = %v\n",lengthOfLastWord3("Hello World")) // 5
fmt.Printf("lengthOfLastWord3(\"Hello World \") = %v\n",lengthOfLastWord3("Hello World ")) // 5
fmt.Printf("lengthOfLastWord3(\" \") = %v\n",lengthOfLastWord3(" ")) // 0
fmt.Printf("lengthOfLastWord3(\"a\") = %v\n",lengthOfLastWord3("a")) // 1
fmt.Printf("lengthOfLastWord3(\"a33\") = %v\n",lengthOfLastWord3("a33")) // 3
fmt.Printf("lengthOfLastWord1(\"Hello World\") = %v\n",lengthOfLastWord1("Hello World")) // 5
fmt.Printf("lengthOfLastWord1(\"Hello World \") = %v\n",lengthOfLastWord1("Hello World ")) // 5
fmt.Printf("lengthOfLastWord1(\" \") = %v\n",lengthOfLastWord1(" ")) // 0
fmt.Printf("lengthOfLastWord1(\"a\") = %v\n",lengthOfLastWord1("a")) // 1
fmt.Printf("lengthOfLastWord1(\"a33\") = %v\n",lengthOfLastWord1("a33")) // 3
fmt.Printf("lengthOfLastWord2(\"Hello World\") = %v\n",lengthOfLastWord2("Hello World")) // 5
fmt.Printf("lengthOfLastWord2(\"Hello World \") = %v\n",lengthOfLastWord2("Hello World ")) // 5
fmt.Printf("lengthOfLastWord2(\" \") = %v\n",lengthOfLastWord2(" ")) // 0
fmt.Printf("lengthOfLastWord2(\"a\") = %v\n",lengthOfLastWord2("a")) // 1
fmt.Printf("lengthOfLastWord2(\"a33\") = %v\n",lengthOfLastWord2("a33")) // 3
fmt.Printf("lengthOfLastWord(\"Hello World\") = %v\n",lengthOfLastWord("Hello World")) // 5
fmt.Printf("lengthOfLastWord(\"Hello World \") = %v\n",lengthOfLastWord("Hello World ")) // 5
fmt.Printf("lengthOfLastWord(\" \") = %v\n",lengthOfLastWord(" ")) // 0
fmt.Printf("lengthOfLastWord(\"a\") = %v\n",lengthOfLastWord("a")) // 1
fmt.Printf("lengthOfLastWord(\"a33\") = %v\n",lengthOfLastWord("a33")) // 3
}