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. ICT638 Mobile and App Development

    Assessment Cover SheetStudent ID CohortStudent NameProgrammeEnrolledDiploma in Information Technolog ...

  2. 【Oracle】 RMAN命令汇总

    RMAN命令汇总 2013年写了关于RMAN命令的汇总,先转换为MD文档,温故而知新. 1.进入RMAN 进入本地数据库 [oracle@oracle-n1 ~]$ rman target / 进入远 ...

  3. 架构设计系列-前端模式的后端(BFF)翻译PhilCalçado

    本文翻译自PhilCalçado的官网:https://philcalcado.com/2015/09/18/the_back_end_for_front_end_pattern_bff.html 对 ...

  4. Hyper V NAT 网络设置 固定IP / DHCP

    Hyper V 默认的Default Switch同时支持了NAT网络以及DHCP,虚拟机能够访问外网. 但使用过程中发现这个IP网段经常变化,而且Hyper V没有提供管理其NAT网络与DHCP的图 ...

  5. dedecms5.7的文章详情页页面标题加载指定txt文本的随机关键字

    dedecms5.7的文章详情页加载指定txt文本的随机关键字 1 实现代码如下 {dede:name runphp='yes'} $file_path = "../test.txt&quo ...

  6. maven聚合项目以及使用dubbo远程服务调用debug操作。

    1.maven聚合项目以及使用dubbo远程服务调用debug操作. 然后操作如下所示: 然后如下所示: 启动断点所在的包的服务.以debug的形式启动. 断点进来的效果如下所示: 接下来请继续你的表 ...

  7. Python对csv排序

    #/usr/bin/evn python # -*- coding: utf-8 -*- import sys from operator import itemgetter # input_file ...

  8. 解决:500 Internal Privoxy Error

    500 Internal Privoxy Error Privoxy encountered an error while processing your request: Could not loa ...

  9. JDK1.8新特性——Optional类

    JDK1.8新特性——Optional类 摘要:本文主要学习了JDK1.8新增加的Optional类. 部分内容来自以下博客: https://www.cnblogs.com/1ning/p/9140 ...

  10. SPFA板子

    #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; ; vector<pair<int, ...