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:

  1. S.length <= 100
  2. 33 <= S[i].ASCIIcode <= 122
  3. S doesn't contain \ or "
class Solution:
def reverseOnlyLetters(self, S):
"""
:type S: str
:rtype: str
""" n=len(S)
s=list(S)
i=0
j=n-1
while i<j:
while i<j and not s[i].isalpha():i+=1
while i<j and not s[j].isalpha():j-=1
s[i],s[j]=s[j],s[i]
i+=1
j-=1
return ''.join(s)

  

[LeetCode&Python] Problem 917. Reverse Only Letters的更多相关文章

  1. [LeetCode&Python] Problem 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  2. [LeetCode&Python] Problem 557. Reverse Words in a String III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  3. [LeetCode&Python] Problem 206. Reverse Linked List

    Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...

  4. 【Leetcode_easy】917. Reverse Only Letters

    problem 917. Reverse Only Letters solution: class Solution { public: string reverseOnlyLetters(strin ...

  5. 【LeetCode】917. Reverse Only Letters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 单指针 双指针 日期 题目地址: https:/ ...

  6. [LeetCode] 917. Reverse Only Letters 只翻转字母

    Given a string S, return the "reversed" string where all characters that are not a letter  ...

  7. LeetCode 917 Reverse Only Letters 解题报告

    题目要求 Given a string S, return the "reversed" string where all characters that are not a le ...

  8. #Leetcode# 917. Reverse Only Letters

    https://leetcode.com/problems/reverse-only-letters/ Given a string S, return the "reversed" ...

  9. 【leetcode】917. Reverse Only Letters(双指针)

    Given a string s, reverse the string according to the following rules: All the characters that are n ...

随机推荐

  1. Java选择结构和数组

    Java选择结构和数组 一.Switch语句 二.if和switch区别 推荐使用if 三.函数 Java中的函数和方法是同一个词 四.数组 4.1.数组常见错误 五.内存机制 六.转换成十六进制 移 ...

  2. python怎样压缩和解压缩ZIP文件

    https://zhidao.baidu.com/question/1498409764366387259.html

  3. 安装 tensorflow 时遇到 OSError: [Errno 1] Operation not permitted 的解决办法

    Installing collected packages: numpy, scipy, six, pyyaml, Keras, opencv-python, h5py, html5lib, blea ...

  4. (转)nginx做转发时,带'_'的header内容丢失

    原本在测试环境测试通过的APP,今天准备切到线上环境做最后测试,结果发现了错误.查看日志发现是APP端发送的http请求中的header内容丢失了.那么代码没有改动,怎么平白无故会丢失头信息? 于是想 ...

  5. Jersey 2.x 前言和约定的文本格式

    这是Jersey 2.x 的用户指南.我们极力将它能与我们新增的功能保持一致.当阅读本指南,作为补充,也请移步至Jersey API documentation查看 Jersey 的特性和 API. ...

  6. robot framework学习笔记2

    声明:本笔记都只是自己根据大牛虫师的robot系列文档学习记录的,学习的话还请移步虫师博客:https://www.cnblogs.com/fnng/ 非常感谢大牛的分享,带小白一步一步入门   F5 ...

  7. Couchbase集群

    Couchbase集群 http://www.cnblogs.com/sunwubin/p/3426801.html Couchbase服务器可以单独运行,也可以作为集群运行.在Couchbase集群 ...

  8. n转m进制标准写法(必须记忆)

    #include <bits/stdc++.h> using namespace std; int main() { int n,m; cin >> n >> m; ...

  9. Application 类

    Application 类具有用于启动和停止应用程序和线程以及处理 Windows 消息的方法,如下所示: Run 在当前线程上启动应用程序消息循环,并可以选择使某窗体可见. Exit 或 ExitT ...

  10. seo-摘自网友

    网页前端制作中的SEO 在SEO盛行的今天到处都在谈优化,对于网站前端制作人员来说,有几点是跟SEO相关的,也就是SEO站内优化中的一部分,下面总结几点: 1.title,<title>页 ...