[快手春招] 2020算法类笔试B卷题解

1.删除字符中连续字符长度大于2的字符,在消除的字符串上继续消除。

(1)测试样例

       input1:abbbssa[题目给出]

        output1:assa

       input2:sbbbssa[自己想的]

      output2:a

ac代码:

def remove_str(s):
    for i in range(len(s)-1):
        if s[i] != s[i+1]:  #找到和s[0]不相等的位置,就是打破连续相等现状的位置
            return s[i+1:]
    return None


while(True):
    try:
        tmp_str = input().strip()

        c_count = []

        result_str = []
        i = 0
        while(i < len(tmp_str)):
            if i+1<len(tmp_str) and i+2 < len(tmp_str) and tmp_str[i] == tmp_str[i+1] and tmp_str[i+1] == tmp_str[i+2]:
                lat_str = remove_str(tmp_str[i+1:])  # 去除重复的后半段字符串
                for_str = tmp_str[:i]  # 前半段
                if lat_str:
                    tmp_str = for_str + lat_str  # 修改tmp_str
           
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值