题目要求

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. IBM CE 错误集之(FNRCS0005E)

    // 通过ObjectStore获取所有的StorageArea对象,CEUtil是我封装的一个获取ObjectStore 的工具类 ObjectStore os = CEUtil.getStore( ...

  2. 在layui layer 弹出层中加载 layui table

    layui.use('table', function(){ var table = layui.table; layer.open({ type : 1, area : [ "600px& ...

  3. [备份]EntityFramework

    本视频和分步演练介绍通过 Code First 开发建立新数据库.这个方案包括建立不存在的数据库(Code First 创建)或者空数据库(Code First 向它添加新表).借助 Code Fir ...

  4. 【Linux】防火墙与CentOS中的iptables

    [iptables] 参考好文:http://www.zsythink.net/archives/1199.这个博客的作者写了深入浅出的iptables介绍,基本上我就是做个他的读书笔记. ■ 基本介 ...

  5. 使用echo $? 查看命令是否执行成功

    shell中的特殊变量:变量名含义$0shell或shell脚本的名字$*以一对双引号给出参数列表$@将各个参数分别加双引号返回$#参数的个数$_代表上一个命令的最后一个参数$$代表所在命令的PID$ ...

  6. Java知多少(75)Object类

    Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每个类都由它扩展而来. 定义Java类时如果没有显示的指明父类,那么就默认继承了 Object 类.例如: p ...

  7. CSS3 Flexbox可视化指南

    0. 目录 目录 引言 正文 1 引入 2 基础 3 使用 4 弹性容器Flex container属性 41 flex-direction 42 flex-wrap 43 flex-flow 44 ...

  8. 两种简单实现菜单高亮显示的JS类(转载)

    两种简单实现菜单高亮显示的JS类   近期在写一个博客管理后台的前端,涉及在同一页面两种高亮显示当前菜单的需求.记得当年写静态页时,为了实现高亮都是在每个页面加不同的样式,呵.高亮显示我觉得对于web ...

  9. Django model中设置多个字段联合唯一约束

    Django中model部分的写法, 参见 unique-together 部分文档. class MyModel(models.Model): field1 = models.CharField(m ...

  10. python 多返回值

    多返回值: def count(): fs = [] for i in range(1, 4): def f(j): def g(): return j*j return g fs.append(f( ...