代码随想录训练营第八天 | 344. 反转字符串 541. 反转字符串II 卡码 54. 替换数字

前言

今天的题目都是简单的模拟,需要注意的只有:python中的str不能直接修改值

一、反转字符串

leetcode344 反转字符串
简单的头尾指针,原地反转字符串

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

            head += 1
            tail -= 1
        

二、反转字符串II

leetcode 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)

三、卡码54 替换数字

卡码网54 替换数字

def exchange(str s):
    result = ""
    for i in range(len(s)):
        if s[i] >= '0' and s[i] <= '9':
            result += "number"
        else:
            result += "s[i]"
    
    return result
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值