[leetcode] 917. Reverse Only Letters

网友投稿 790 2022-08-22

[leetcode] 917. Reverse Only Letters

[leetcode] 917. Reverse Only Letters

Description

Given a string S, return the “reversed” string where all characters that are not a letter stay in the same place, and all letters reverse their positions.

Example 1:

Input: "ab-cd"Output: "dc-ba"

Example 2:

Input: "a-bC-dEf-ghIj"Output: "j-Ih-gfE-dCba"

Example 3:

Input: "Test1ng-Leet=code-Q!"Output: "Qedo1ct-eeLg=ntse-T!"

Note:

S.length <= 10033 <= S[i].ASCIIcode <= 122S doesn’t contain \ or "

分析

题目的意思是:只将字符串中字母部分进行翻转,其它保持不变。思路也很直接,翻转之后,原位置相应有字母的位置填上翻转的字符就行了。我看了一下答案,用了栈,其他思路基本一致。

代码

class Solution: def solve(self,ch): if((ch>='a' and ch<='z') or (ch>='A' and ch<='Z')): return True return False def reverseOnlyLetters(self, S: str) -> str: n=len(S) S1=list(reversed(S)) i=0 j=0 res='' while(i

参考文献

​​[LeetCode] Solution​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[leetcode] 1592. Rearrange Spaces Between Words
下一篇:[leetcode] 1513. Number of Substrings With Only 1s
相关文章

 发表评论

暂时没有评论,来抢沙发吧~