ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

LeetCode刷题(python3)|day08 第三章 第四章 字符串part01

2026/9/23 17:31:29 拓冰建站 浏览量
LeetCode刷题(python3)|day08 第三章 第四章 字符串part01

344.反转字符串

class Solution:def reverseString(self, s: List[str]) -> None:"""Do not return anything, modify s in-place instead."""s[0:len(s)] = s[::-1]

541. 反转字符串II

class Solution:def reverseStr(self, s: str, k: int) -> str:result = list(s)for i in range(0, len(result), 2*k):#2. 对于字符串s = 'abc',如果使用s[0:999] ===> 'abc'。字符串末尾如果超过最大长度,则会返回至字符串最后一个值,这个特性可以避免一些边界条件的处理。temp = result[i:i+k]result[i:i+k] = temp[::-1]return ''.join(result)

151.翻转字符串里的单词

class Solution:def reverseWords(self, s: str) -> str:s = s.strip()result = s.split()print(result)result[:] = result[::-1]return " ".join(result)