作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/contest/weekly-contest-105/problems/reverse-only-letters/

题目描述

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 "

题目大意

对字符串进行逆序排序,要求只把字母的顺序翻转,而其他字符原地不动。

解题方法

周赛第一题,做法很简单了,先把所有的字母保存下来,然后再次对字符串进行遍历,如果源字符串的某个位置是字母,那么把字母列表中最后一个元素换过来,否则就还是原来的字符。

时间复杂度是O(N),空间复杂度是O(N)。

class Solution(object):
def reverseOnlyLetters(self, S):
"""
:type S: str
:rtype: str
"""
letters = []
N = len(S)
for i, s in enumerate(S):
if s.isalpha():
letters.append(s)
res = ""
for i, s in enumerate(S):
if s.isalpha():
res += letters.pop()
else:
res += s
return res

单指针

也可以不用把所有的字符保存下来,直接使用一个指针从后向前扫描,可以把空间复杂度降到O(1)。

时间复杂度是O(N),空间复杂度是O(1)。

class Solution(object):
def reverseOnlyLetters(self, S):
"""
:type S: str
:rtype: str
"""
N = len(S)
l = N - 1
res = ""
for i, s in enumerate(S):
if s.isalpha():
while not S[l].isalpha():
l -= 1
res += S[l]
l -= 1
else:
res += s
return res

双指针

双指针版本,使用两个指针分别前面和后面的两个字母位置。然后将两个字母翻转,其他的位置不用动。这个做法比较难点,里面的两个while循环都需要加上边界判断,里面的if还需要边界判断以及left和right的判断。

时间复杂度是O(N),空间复杂度是O(1)。打败100%的提交。

class Solution(object):
def reverseOnlyLetters(self, S):
"""
:type S: str
:rtype: str
"""
N = len(S)
left, right = 0, N - 1
slist = list(S)
while left < right:
while left < N and (not S[left].isalpha()):
left += 1
while right >= 0 and (not S[right].isalpha()):
right -= 1
if left < N and right >= 0 and left < right:
slist[left], slist[right] = slist[right], slist[left]
left, right = left + 1, right - 1
return "".join(slist)

参考资料:

日期

2018 年 10 月 7 日 —— 假期最后一天!!

【LeetCode】917. Reverse Only Letters 解题报告(Python)的更多相关文章

  1. LeetCode 917 Reverse Only Letters 解题报告

    题目要求 Given a string S, return the "reversed" string where all characters that are not a le ...

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

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

  3. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

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

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

  5. #Leetcode# 917. Reverse Only Letters

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

  6. LeetCode 206 Reverse Linked List 解题报告

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

  7. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

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

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

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

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

随机推荐

  1. 剖析ApplicationRunner、CommandLineRunner

    需求:SpringBoot项目启动成功后执行某方法 方案:在spring-boot中提供了两种Runner接口:ApplicationRunner和CommandLineRunner,编写自己的类实现 ...

  2. POLLOUT/POLLINT事件触发测试

    一般poll使用过程中都是检测POLLIN事件,表示描述符有事件可以处理了,需要我们处理.对POLLOUT事件触发的方式相对较少,网上也有很多对此触发的疑问,利用实际项目中用的一个用法,下面做了个测试 ...

  3. MapReduce02 序列化

    目录 MapReduce 序列化 概述 自定义序列化 常用数据序列化类型 int与IntWritable转化 Text与String 序列化读写方法 自定义bean对象实现序列化接口(Writable ...

  4. A Child's History of England.29

    You have not forgotten the New Forest which the Conqueror made, and which the miserable people whose ...

  5. 【leetcode】952. Largest Component Size by Common Factor(Union find)

    You are given an integer array of unique positive integers nums. Consider the following graph: There ...

  6. 内存管理——new delete expression

    C++申请释放内存的方法与详情表 调用情况 1.new expression new表达式在申请内存过程中都发生了什么? 编译器将new这个分解为下面的主要3步代码,①首先调用operator new ...

  7. ORACLE profile含义,修改,新增

    profiles文件是口令和资源限制的配置集合,包括CPU的时间.I/O的使用.空闲时间.连接时间.并发会话数量.密码策略等对于资源的使用profile可以做到控制会话级别或语句调用级别.oracle ...

  8. 关于form表单提交ajaxForm和ajaxSubmit的用法与区别

    前几天在学习form表单提交时看到这两种方法,这两种方法都是实现form的ajax提交的方法,看了很多资料还是不太明白其用法和区别,最后直接自己写demo,很快就理解,所以说实操是学习的最快捷直接的途 ...

  9. 【Linux】【Services】【NetFileSystem】Samba

    1. 简介 1.1. 背景:case is initiative by 某windows无良人事,需求是需要一整块4T的硬盘,由于ESXi5最大支持一块盘是2T大小,而且不可以使用windows动态卷 ...

  10. 【Services】【Web】【apr】安装apr

    1. 基础: 1.1 描述:apr全称Apache Portable Runtime,常用于与ssl相关的环境支持,比如openssl,httpd,nginx,tomcat 1.2 链接: 官方网站: ...