题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

def transfer(s:str)->str:
    t=list(s)     //变成列表可修改
    num = len(t) 
    temp=[]
    target =""
    index=0      //temp的下标
    for i in range(26):       //实现条件1和条件2 装到temp列表中
        for j in range(num):
            if ord(t[j])-ord('a')==i or ord(t[j])-ord('A')==i:
               temp.append(t[j])
    
    for i in range(num):    //实现条件3 把排好序的temp装到t里  
        if  (ord(t[i])>=ord('a') and ord(t[i])<=ord('z')) or(ord(t[i])>=ord('A') and ord(t[i])<=ord('Z')): 
              t[i]=temp[index]  
              index+=1        
        else:
             continue
    for i in t:   //把排好的t装到target里
        target+=i
    return target
    

s=input()
print(transfer(s))

ord()->看ASCII码

ASCII码值转化成字符的函数

chr()

全部评论

相关推荐

12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务