344.反转字符串
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
541. 反转字符串II
class Solution:
def reverseStr(self, s: str, k: int) -> str:
def reserve_substring(text):
left, right = 0, len(text) - 1
while left < right:
text[left], text[right] = text[right], text[left]
left += 1
right -= 1
return text
res = list(s)
for cur in range(0, len(s), 2 * k):
res[cur: cur + k] = reserve_substring(res[cur: cur + k])
return ''.join(res)
卡码网:54.替换数字
class Solution:
def change(self, s):
lst = list(s)
for i in range(len(lst)):
if lst[i].isdigit():
lst[i] = "number"
return ''.join(lst)
151.翻转字符串里的单词
class Solution:
def reverseWords(self, s: str) -> str:
s = s.strip()
s = s[::-1]
s = ' '.join(word[::-1] for word in s.split())
return s
卡码网:55.右旋转字符串
k = int(input())
s = input()
s = s[len(s) - k:] + s[:len(s) - k]
print(s)