Leetcode 344: reverse string
class Solution:
def reverseString(self, s: List[str]) -> None:
left = 0
right = len(s)-1
while left<right:
s[left], s[right] = s[right], s[left]
left+=1
right-=1
return s
Leetcode 541:reverse string 2
class Solution:
def reverseStr(self, s: str, k: int) -> str:
for i in range(0,len(s),2*k):
s = s[:i]+s[i:i+k][::-1]+s[i+k:]
return s
剑指Offer 05.替换空格
s = list(s.split(' '))
for i in range(len(s)-1):
s[i]+='%20'
return ''.join(s)
Leetcode 151: Reverse Words in a String
class Solution:
def reverseWords(self, s: str) -> str:
s=s.lstrip()
s=s.rstrip()
a = s.split(' ')
left = 0
right = len(a)-1
while left<right:
if a[left]==' ' and a[right]==' ':
a[left], a[right]='', ''
left+=1
right-=1
if a[left]==' ' and a[right]!=' ':
a[left] = ''
left+=1
if a[left]!=' ' and a[right]==' ':
a[right] = ''
right-=1
if a[left]!=' ' and a[right]!=' ':
a[left], a[right] = a[right], a[left]
left+=1
right-=1
for i in range(len(a)-1):
if a[i]!='':
a[i] += ' '
return ''.join(a)
剑指Offer58-II.左旋转字符串
class Solution:
def reverseLeftWords(self, s: str, n: int) -> str:
return s[n:]+s[:n]