-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path186-ReverseWordsInAStringII.go
More file actions
89 lines (79 loc) · 2.66 KB
/
186-ReverseWordsInAStringII.go
File metadata and controls
89 lines (79 loc) · 2.66 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
package main
// 186. Reverse Words in a String II
// Given a character array s, reverse the order of the words.
// A word is defined as a sequence of non-space characters. The words in s will be separated by a single space.
// Your code must solve the problem in-place, i.e. without allocating extra space.
// Example 1:
// Input: s = ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
// Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]
// Example 2:
// Input: s = ["a"]
// Output: ["a"]
// Constraints:
// 1 <= s.length <= 10^5
// s[i] is an English letter (uppercase or lowercase), digit, or space ' '.
// There is at least one word in s.
// s does not contain leading or trailing spaces.
// All the words in s are guaranteed to be separated by a single space.
import "fmt"
func reverseWords(s []byte) {
reverse := func (s []byte, from, to int) { // 从 from 反转到 to 位置
for from < to {
s[from], s[to] = s[to], s[from]
from++
to--
}
}
reverse(s, 0,len(s) - 1)
start := 0
for i ,ch := range s { // 反转每个单词
if ch == ' ' {
reverse(s,start,i - 1) // 反转单词
start = i + 1
}
}
reverse(s, start, len(s) - 1) // 最后一个单词之后没有" ",所以最后一个单词不会被反转
}
func reverseWords1(s []byte) {
n := len(s)
for i := 0; i < n / 2; i++ {
s[i], s[n-i-1] = s[n-i-1], s[i]
}
i, j := 0, 0
for j < n {
for j < n && s[j] != ' ' {
j++
}
r := j-1
for ;i < r; i++ {
s[i], s[r] = s[r], s[i]
r--
}
j++
i = j
}
}
func main() {
// Example 1:
// Input: s = ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
// Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]
s1 := []byte{'t','h','e',' ','s','k','y',' ','i','s',' ','b','l','u','e'}
fmt.Println("before: ", string(s1)) // the sky is blue
reverseWords(s1)
fmt.Println("after: ", string(s1)) // blue is sky the
// Example 2:
// Input: s = ["a"]
// Output: ["a"]
s2 := []byte{'a'}
fmt.Println("before: ", string(s2)) // a
reverseWords(s2)
fmt.Println("after: ", string(s2)) // a
s11 := []byte{'t','h','e',' ','s','k','y',' ','i','s',' ','b','l','u','e'}
fmt.Println("before: ", string(s11)) // the sky is blue
reverseWords1(s11)
fmt.Println("after: ", string(s11)) // blue is sky the
s12 := []byte{'a'}
fmt.Println("before: ", string(s12)) // a
reverseWords1(s12)
fmt.Println("after: ", string(s12)) // a
}