题目要求

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.

题目分析及思路

给定一个字符串,返回“reversed”后的字符串,要求非字母的字符保留原来的位置,字母字符逆序排列。可以先获得原字符串中的全部字母字符列表,然后遍历原字符串,并用一个新的列表存储结果。若某位置是字母字符,就取先前获得的列表中的末尾元素;若是非字母字符,则保留该位置的字符。最后将列表转换成字符串。

python代码

class Solution:

def reverseOnlyLetters(self, S: str) -> str:

letters = [c for c in S if c.isalpha()]

res = []

for c in S:

if c.isalpha():

res.append(letters.pop())

else:

res.append(c)

return "".join(res)

LeetCode 917 Reverse Only Letters 解题报告的更多相关文章

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

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

  2. 【LeetCode】848. Shifting Letters 解题报告(Python)

    [LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

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

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

  4. #Leetcode# 917. Reverse Only Letters

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

  5. LeetCode 206 Reverse Linked List 解题报告

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

  6. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. LeetCode 917. Reverse Only Letters (仅仅反转字母)

    题目标签:String 利用left, right 两个pointers, 从左右开始 互换 字母.如果遇到的不是字母,那么继续移动到下一个. Java Solution: Runtime beats ...

  8. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  9. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

随机推荐

  1. D3.js学习

    // 1.选择d3.select('p')d3.selectAll('p')d3.select('.txt').style('color', '#fff')// 2.支持动态设置属性// a:随机属性 ...

  2. Go Revel - Jobs(任务调度模块)

    revel提供了一个框架用于脱离请求流程的执行异步任务,一般用来执行经常运行的任务.更新缓存数据或发送邮件等. ##启用 该框架作为一个可选的revel模块,默认并不启用.需要更改应用配置来启用它: ...

  3. WebApi XML,Json格式自定义,IEnumerable<T>,ArrayOf

    global.ascx中application-start() GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSeri ...

  4. c# 调用非托管c++dll 参数问题(转)

    在C#中调用C(C++)类的DLL的时候,有时候C的接口函数包含很多参数,而且有的时候这些参数有可能是个结构体,而且有可能是结构体指针,那么在C#到底该如何安全的调用这样的DLL接口函数呢?本文将详细 ...

  5. (笔记)Linux内核学习(一)之内核介绍

    内核与操作系统: 内核是操作系统的核心部分,包含了系统运行的核心过程,决定系统的性能,操作系统启动内核被装入到RAM中: 操作系统与底层硬件设备交互和为运行应用程序提供执行环境. Linux内核与微内 ...

  6. kafka消费数据策略

    单线程消费 以之前生产者中的代码为例,事先准备好了一个 Topic:data-push,3个分区. 先往里边发送 100 条消息,没有自定义路由策略,所以消息会均匀的发往三个分区. 先来谈谈最简单的单 ...

  7. linux memcached开机启动

    方法一: 在/etc/rc.d/rc.local 加入以下代码 /usr/local/memcached/bin/memcached -u root -d -m -l -P /tmp/memcache ...

  8. Kafka 1.1新功能:数据的路径间迁移

    经常有小伙伴有这样的疑问:为什么线上Kafka机器各个磁盘间的占用不均匀,经常出现“一边倒”的情形? 这是因为Kafka只保证分区数量在各个磁盘上均匀分布,但它无法知晓每个分区实际占用空间,故很有可能 ...

  9. Oracle中add_months()函数的用法

    查询当前时间1个月以前的时间: select add_months(sysdate,-1) from dual; 查询当前时间1个月以后的时间: select add_months(sysdate,1 ...

  10. mui---取消掉默认加载框

    我们在进行打开页面新页面的时候,在APP中会在中间有一个加载框,考虑到用户体验,要取消掉,具体方法是,对openWindow进行配置: 具体参考:http://dev.dcloud.net.cn/mu ...