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. TP5框架whereor

    whereOr方法 Db::table('think_user') ->where('name','like','%thinkphp') ->whereOr('title','like', ...

  2. Confluence 6 LDAP 高级设置

    启用嵌套组(Enable Nested Groups) 为嵌套组启用或禁用支持. 一些目录服务器能够允许你在一个组中定义另外一个组.在这种结构下的用户组称为用户组嵌套.嵌套组的配置能够让子用户组继承上 ...

  3. bzoj3944: Sum 杜教筛板子题

    板子题(卡常) 也可能是用map太慢了 /************************************************************** Problem: 3944 Us ...

  4. SourceTree

    MAC上最好的GIT免费GUI工具是SourceTree(没有之一).此外,最好的GIT代码开源网站是GitHub,最好的GIT代码私有库是BitBucket https://www.sourcetr ...

  5. OAF 供应商门户添加功能标签后获取当前供应商VendorId的方法

    一种是参考管理页面 /oracle/apps/pos/supplier/webui/SuppDtPG 在目标页面的AM中添加VO实例,oracle.apps.pos.supplier.server.S ...

  6. Oracle12c新特性之基本操作

    1.  服务器端连接并启动数据库: sqlplus / as sysdba startup; 2.  服务器端连接并关闭数据库: sqlplus / as sysdba shutdown immedi ...

  7. tensoflow学习入门一

    对于如何创建图并开启session,参考如下 # -- encoding:utf-8 -- import tensorflow as tf # 定义常量矩阵a和矩阵b a=tf.constant([[ ...

  8. sqlite 查询数据库中所有的表名,判断某表是否存在,将某列所有数值去重后获得数量

    1.列出当前db文件中所有的表的表名 SQL语句:SELECT * FROM sqlite_master WHERE type='table'; 结构如下: 注:网上有人说可以带上db文件的名称,如: ...

  9. NRF51822+STM32bootload——typedef void (*Fun) (void) 理解

    1.typdef 用法如下所示 typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned ...

  10. 数据库cmd窗口登录

    mysql -uroot -p123 -P3306 -h127.0.0.1 -uroot::root数据库登录用户名 -p123:数据库密码123 -P3306::3306数据库的端口号 -h127. ...