题目:
题解:
题目中说要用O(1)的空间;
因此我们采用双指针法,两个指针一个从头一个从尾分别向中间逼近。在向中间逼近的过程中,头和尾指针的元素相互交换位置,直到头指针和尾指针相等即可。
代码如下:
class Solution:
def reverseString(self, s):
"""
Do not return anything, modify s in-place instead.
"""
left = 0
right = len(s) - 1
while(left < right):
temp = s[left]
s[left] = s[right]
s[right] = temp
left += 1
right -= 1
return s
```