LeetCode 344 _ 反转字符
1. 题目描述
2. 代码
1 class Solution: 2 def reverseString(self, s: 'List[str]') -> None: 3 """ 4 Do not return anything, modify s in-place instead. 5 """ 6 n = len(s) 7 i,j = 0,n-1 8 while i < j: 9 s[j],s[i] = s[i],s[j] 10 i += 1 11 j -= 1
思路: 定义双指针, 头尾交换值.
3. 整理
1 a = 10 2 b = 5 3 a,b = b,a 4 print(a,b)
1 5 10
Python可以直接交换2个值.