zoukankan      html  css  js  c++  java
  • 917. 仅仅反转字母

    给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。

    示例 1:

    输入:"ab-cd"
    输出:"dc-ba"
    示例 2:

    输入:"a-bC-dEf-ghIj"
    输出:"j-Ih-gfE-dCba"
    示例 3:

    输入:"Test1ng-Leet=code-Q!"
    输出:"Qedo1ct-eeLg=ntse-T!"
     

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/reverse-only-letters

    class Solution:
        def reverseOnlyLetters(self, S: str) -> str:
            res=[]
            dict={}
            for i in range(len(S)):
                if S[i].isalpha():
                    res.append(S[i])
                else:   
                    dict[i]=S[i]
            res.reverse()
            for i in dict.keys():
                res.insert(i,dict[i])
            ans=''
            for i in res:
                ans+=i
            return ans

    还是想复杂了,虽然time超过了90%多,space不行

  • 相关阅读:
    学习进度总结表
    关于软件工程的问题
    自我介绍
    web安全
    spring profile
    spring 装配
    python Descriptor (描述符)
    python string intern
    Java 对象内存占用
    java jdb命令详解
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13499546.html
Copyright © 2011-2022 走看看