557. 反转字符串中的单词 III - 力扣(Leetcode)
与代码 [[leetcode-541. 反转字符串 II]] 相关联,swapStrBytes
函数,使用了上次的代码
func reverseWords(s string) string {
sBytes := []byte(s)
for left, right := 0, 0; right < len(sBytes); right++ {
if sBytes[right] == ' ' {
swapStrBytes(sBytes, left, right-1)
left = right+1
}
if right == len(sBytes) - 1 {
swapStrBytes(sBytes, left, right)
}
}
return string(sBytes)
}
func swapStrBytes(s []byte, left, right int) {
if left >= right {
return
}
for left < right {
s[left], s[right] = s[right], s[left]
left++
right--
}
}
标签:right,string,557,sBytes,swapStrBytes,III,leetcode,left
From: https://www.cnblogs.com/wudanyang/p/17014672.html