设置头尾两个指针,依靠中间变量temp交换头尾指针所指元素,头指针后移,尾指针前移,直到头尾指针重合或者头指针在尾指针后面一个元素
class Solution(object):
def reverseString(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
temp = 0
i,j = 0,len(s)-1
while i<j:
temp = s[i]
s[i] = s[j]
s[j] = temp
i += 1
j -= 1
if j == i-1 or i == j:
return s
class Solution(object):
def reverseString(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
return s.reverse()