题目要求

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. Asp.Net WebApi swagger使用教程

    swagger简介 别名:丝袜哥 功能:用于生产api文档 swagger安装 Nuget搜索swagger,然后安装Swashbuckle swagger使用 生成api的xml文档 webapi项 ...

  2. Selenium Web 自动化 - 如何找到元素

    Selenium Web 自动化 - 如何找到元素 2016-07-29 1. 什么是元素? 元素:http://www.w3school.com.cn/html/html_elements.asp ...

  3. 【转】Error:JAVA_HOME is not set and could not be found

    如果stop-dfs.sh也报了这个错,把$HADOOP_HOME/libexec/hadoop-config.sh中的如下内容之前加上 export JAVA_HOME=/home/lqr/Tool ...

  4. Apache Hadoop YARN: 背景及概述

    从2012年8月开始Apache Hadoop YARN(YARN = Yet Another Resource Negotiator)成了Apache Hadoop的一项子工程.自此Apache H ...

  5. 【iCore1S 双核心板_ARM】例程十一:RTC实时时钟实验——显示时间和日期

    实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ RTC_DateTypeDef sDate; RTC_TimeTypeDef sTime; u ...

  6. 命名实体识别,使用pyltp提取文本中的地址

    首先安装pyltp pytlp项目首页 单例类(第一次调用时加载模型) class Singleton(object): def __new__(cls, *args, **kwargs): if n ...

  7. 不平衡学习 Learning from Imbalanced Data

    问题: ICC警情数据分类不均,30+分类,最多的分类数据数量1w+条,只有10个类别数量超过1k,大部分分类数量少于100条. 解决办法: 下采样:通过非监督学习,找出每个分类中的异常点,减少数据. ...

  8. /proc详解

    内容摘要:Linux系统上的/proc目录是一种文件系统,即proc文件系统. Linux系统上的/proc目录是一种文件系统,即proc文件系统.与其它常见的文件系统不同的是,/proc是一种伪文件 ...

  9. ubuntu: apt-get update的时候遇到“Hash Sum mismatch”错误

    http://blog.csdn.net/xjwjava/article/details/8543768 http://askubuntu.com/questions/140246/how-do-i- ...

  10. snopy 数据库简介

    snoRNA ,small nucleolar RNA, 核仁小分子RNA, 是一类在细胞核内的small non-coding RNA, 长度在60-300nt, 其主要功能是调控其他小RNA分子( ...