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 "

这道题给了一个由字母和其他字符组成的字符串,让我们只翻转其中的字母,并不是一道难题,解题思路也比较直接。可以先反向遍历一遍字符串,只要遇到字母就直接存入到一个新的字符串 res,这样就实现了对所有字母的翻转。但目前的 res 中就只有字母,还需要将原字符串S中的所有的非字母字符加入到正确的位置,可以再正向遍历一遍原字符串S,遇到字母就跳过,否则就把非字母字符加入到 res 中对应的位置,参见代码如下:


解法一:

class Solution {
public:
string reverseOnlyLetters(string S) {
string res = "";
for (int i = (int)S.size() - 1; i >= 0; --i) {
if (isalpha(S[i])) res.push_back(S[i]);
}
for (int i = 0; i < S.size(); ++i) {
if (isalpha(S[i])) continue;
res.insert(res.begin() + i, S[i]);
}
return res;
}
};

再来看一种更加简洁的解法,使用两个指针i和j,分别指向S串的开头和结尾。当i指向非字母字符时,指针i自增1,否则若j指向非字母字符时,指针j自减1,若i和j都指向字母时,则交换 S[i] 和 S[j] 的位置,同时i自增1,j自减1,这样也可以实现只翻转字母的目的,参见代码如下:


解法二:

class Solution {
public:
string reverseOnlyLetters(string S) {
int n = S.size(), i = 0, j = n - 1;
while (i < j) {
if (!isalpha(S[i])) ++i;
else if (!isalpha(S[j])) --j;
else {
swap(S[i], S[j]);
++i; --j;
}
}
return S;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/917

参考资料:

https://leetcode.com/problems/reverse-only-letters/

https://leetcode.com/problems/reverse-only-letters/discuss/178419/JavaC%2B%2BPython-Two-Pointers

https://leetcode.com/problems/reverse-only-letters/discuss/200878/easy-C%2B%2B-with-comments-two-pointer-based-approach

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 917. Reverse Only Letters 只翻转字母的更多相关文章

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

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

  2. LeetCode 917 Reverse Only Letters 解题报告

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

  3. #Leetcode# 917. Reverse Only Letters

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

  4. 【Leetcode_easy】917. Reverse Only Letters

    problem 917. Reverse Only Letters solution: class Solution { public: string reverseOnlyLetters(strin ...

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

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

  6. [LeetCode&Python] Problem 917. Reverse Only Letters

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

  7. 【leetcode】917. Reverse Only Letters(双指针)

    Given a string s, reverse the string according to the following rules: All the characters that are n ...

  8. [LeetCode 92] Reverse Linked List II 翻转单链表II

    对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...

  9. Leetcode917.Reverse Only Letters仅仅反转字母

    给定一个字符串 S,返回 "反转后的" 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转. 示例 1: 输入:"ab-cd" 输出:" ...

随机推荐

  1. Beyond Compare的自定义破解方法

    因本人是程序员的缘故,经常时不时就是几千几万行代码找不同,也就时常要用到一个超级无敌好用的文本对比软件:Beyond Compare. 然而破解成了一大问题,网上有很多注册码都已经被封了或者是注销掉了 ...

  2. CentOS 7下KVM挂载物理硬盘/硬盘直通

    使用如下的XML配置 <disk type='block' device='disk'> <driver name='qemu' type='raw'/> <source ...

  3. gitLab 分支保护设置

    一.需求背景 开发当前开发的分支遇到暂时无法解决的问题,现在有需要开发其他应用,所以希望运维这边将当前有问题分支冻结,让其他人无法进行修改,待后续有时间在排查代码问题 二.Gitlab配置步骤 1.搜 ...

  4. 【转载】百度百科:FusionCube超融合

    [转载]百度百科:FusionCube超融合 华为FusionCube融合基础设施一体机(Huawei FusionCube Converged Infrastructure)是华为公司IT产品线云计 ...

  5. [ICP]手推SVD方法

    该方法源于<Least-Squares Rigid Motion Using SVD>,原文推导十分详细,这里自己也仔细推导了一遍,有些地方加以注释整理. 问题定义 假设我们有两个点云集合 ...

  6. Python - Socket网络编程 - 第二十六天

    网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法. 高 ...

  7. python验证码处理(1)

    目录 一.普通图形验证码   这篇博客及之后的系列,我会向大家介绍各种验证码的识别.包括普通图形验证码,极验滑动验证码,点触验证码,微博宫格验证码. 一.普通图形验证码   之前的博客已向大家介绍了简 ...

  8. CSS animation属性

    定义和用法 animation属性是下列属性的一个缩写属性: animation-name animation-duration animation-timing-function animation ...

  9. A dependency may only have one source

    在使用Flutter的时候添加依赖报错了 Error on line 21, column 5 of pubspec.yaml: A dependency may only have one sour ...

  10. hadoop 完全分布式集群搭建

    1.在伪分布式基础上搭建,伪分布式搭建参见VM上Hadoop3.1伪分布式模式搭建 2.虚拟机准备,本次集群采用2.8.3版本与3.X版本差别不大,端口号所有差别 192.168.44.10 vmho ...