Write a function that reverses a string. The input string is given as an array of characters char[].
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
那么就是一个线性表O(1)的寻找替换
func reverseString(s []byte) {
for i,v := range s {
if i >= len(s)/2 {
break
}
tmp := v
s[i] = s[len(s)-i-1]
s[len(s)-i-1] = tmp
}
}