[牛客] 算法基础-字符移位 腾讯2017暑期实习生编程题 python实现

链接:https://www.nowcoder.com/questionTerminal/7e8aa3f9873046d08899e0b44dac5e43 

答案:

# -*- coding:utf-8 -*-
import sys

def yiwei(str):
    """
    这个开辟了新空间,不符合题意
    """
    xiaoxie = ''
    daxie = ''
    for i in str:
        if i >= 'a' and i <= 'z':
            xiaoxie += i
        else:
            daxie += i
    return xiaoxie+daxie

def yiwei2(strs):
    """
    基本思路:
    将string转化成list,利用列表的pop和append操作实现就地删除和插入
    最后用join操作把list转回string
    """
    l  = list(strs)
    index = 0
    count = 0
    while count < len(strs):
        if l[index].isupper():
            l.append(l.pop(index))
            count += 1
            continue
        index += 1
        count += 1
    return ''.join(l)


if __name__ == '__main__':
    while True:
        line = sys.stdin.readline().strip()
        lens = len(line)
        if not line:
            break
        print(yiwei2(line))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值