Merge branch 'master' of github.com:jinbudaily/leetcode-master

This commit is contained in:
jinbudaily
2023-07-28 10:33:34 +08:00
9 changed files with 322 additions and 43 deletions

View File

@@ -547,26 +547,28 @@ func reverseWords(s string) string {
b = b[:slowIndex]
}
//2.反转整个字符串
reverse(&b, 0, len(b)-1)
reverse(b)
//3.反转单个单词 i单词开始位置j单词结束位置
i := 0
for i < len(b) {
j := i
for ; j < len(b) && b[j] != ' '; j++ {
}
reverse(&b, i, j-1)
reverse(b[i:j])
i = j
i++
}
return string(b)
}
func reverse(b *[]byte, left, right int) {
for left < right {
(*b)[left], (*b)[right] = (*b)[right], (*b)[left]
left++
right--
}
func reverse(b []byte) {
left := 0
right := len(b) - 1
for left < right {
b[left], b[right] = b[right], b[left]
left++
right--
}
}
```