题目要求

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. 蓝牙mesh介绍

    了解一下关于蓝牙Mesh的知识. 蓝牙mesh网络使用,并且依赖于低功耗蓝牙(BLE).低功耗蓝牙技术是蓝牙mesh使用的无线通信协议栈. 蓝牙BR / EDR能够实现一台设备到另一台设备的连接和通信 ...

  2. Java如何检查日期格式是否正确?

    在Java编程中,如何检查日期格式是否正确? 以下示例演示如何使用String类的matches()方法检查日期格式是否正确. package com.yiibai; public class Che ...

  3. tcp 三次握手 转

    转载 记得刚毕业找工作面试的时候,经常会被问到:你知道“3次握手,4次挥手”吗?这时候我会“胸有成竹”地“背诵”前期准备好的“答案”,第一次怎么怎么,第二次……答完就没有下文了,面试官貌似也没有深入下 ...

  4. python中将图片从客户端(client)推到(POST)到服务器端(server)的方法

    从客户端推json到服务器端的工作可以用flask很容易做到,那么需要推送图片的话可以先将图片存到json中再进行操作. 服务器端 from flask import request, Flask i ...

  5. [Converge] Weight Initialiser

    From: http://www.cnblogs.com/denny402/p/6932956.html [, ] fully connected w = tf.Variable(tf.truncat ...

  6. [Algorithm] Asymptotic Growth Rate

    f(n) 的形式 vs 判定形势 但,此题型过于简单,一般不出现在考题中. Extended: link Let's set n = 2^m, so m = log(n) T(n) = 2*T(n^( ...

  7. ImageMagick、imagick和ghostscript三者的关联

    http://467754239.blog.51cto.com/4878013/1602518/ 一.功能概述 ImageMagick是第三方的图片处理软件,功能要比GD强大.建议两者都安装,并不冲突 ...

  8. 配置openssh实现sftp远程文件上传

    客服端:winscp等ftp/sftp客户端 服务器:阿里云默认使用的openssh 需求:可以sftp远程传输文件到服务器固定文件夹下,不可远程ssh登录 步骤: 1. 建立系统用户ftpuser及 ...

  9. linux erase

    map的erase windows和linux不同,而迭代器弄不好就失效 1 #include <iostream> 2 #include <map> 3 #include & ...

  10. smarty模板的内置函数

    内置函数参考:http://www.php100.com/manual/smarty/language.builtin.functions.html 主要就是 foreach 和 if elseif ...