# Leetcode 344. Reverse String 給定一個string的array將此array原地反轉 ## 想法 ### (1)使用python函式 python的list格式,預設有反轉list的函式 程式碼: ``` def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s.reverse() ``` ### (2)使用兩個指針的概念 將長度切為一半,逐一交換,得到最後結果 程式碼: (1)使用for in ``` def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ length = len(s) for i in range(length//2): tag = s[i] s[i] = s[length-i-1] s[length-i-1] = tag ``` (2)使用while ``` def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ left = 0 right = len(s)-1 while(right>left): tag = s[left] s[left] = s[right] s[right] = tag left,right = left+1,right-1 ```