class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
front = 0
n = len(s)
rear = n - 1
for i in range(n):
if front < rear:
temp = s[rear]
s[rear] = s[front]
s[front] = temp
front += 1
rear -= 1
Leetcode 344.反转字符串 Reverse String - Python
最新推荐文章于 2024-11-19 15:47:58 发布
这篇博客介绍了如何使用Python实现一个名为'Solution'的类,其中的reverseString方法通过交换字符串首尾字符实现原地反转。核心代码展示了如何在不创建新字符串的情况下,高效地对输入字符串`s`进行操作。
1453

被折叠的 条评论
为什么被折叠?



