day 8 : String in Python

Leetcode 344: reverse string

class Solution:
    def reverseString(self, s: List[str]) -> None:
        #two pointer
        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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值