344.反转字符串
class Solution:
def reverseString(self, s: List[str]) -> None:
l=0
r=len(s)-1
while l<r:
s[l],s[r]=s[r],s[l]
l+=1
r-=1
541. 反转字符串II
class Solution:
def reverseStr(self, s: str, k: int) -> str:
t=list(s)
for i in range(0,len(t),2*k):
t[i:i+k]=reversed(t[i:i+k])
return "".join(t)
剑指Offer 05.替换空格
class Solution:
def replaceSpace(self, s: str) -> str:
#这个方法非常暴力 但是我为什么没有想到呢
#明天再看看官方答案
return "%20".join(s.split(" "))
151.翻转字符串里的单词
class Solution:
def reverseWords(self, s: str) -> str:
nums=self.delspace(s)
nums=self.reverse_str(nums,0,len(nums)-1)
self.reverse_each_words(nums)
return ''.join(nums)
def delspace(self,s):
l=0
r=len(s)-1
while l<=r and s[l]==' ':
l+=1
while l<=r and s[r]==' ':
r-=1
temp=[]
while l<=r:
if s[l]!=' ':
temp.append(s[l])
elif temp[-1]!=' ':
temp.append(s[l])
l+=1
return temp
def reverse_str(self,nums,l,r):
while l<r:
nums[l],nums[r]=nums[r],nums[l]
l+=1
r-=1
return nums
def reverse_each_words(self,nums):
l=0
r=0
end=len(nums)
while l<end:
while r<end and nums[r]!=' ':
r+=1
t=self.reverse_str(nums,l,r-1)
l=r+1
r+=1
return None
剑指Offer58-II.左旋转字符串
return s[n:] + s[0:n]