[LeetCode] 917. 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:
S.length <= 10033 <= S[i].ASCIIcode <= 122Sdoesn'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
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 917. Reverse Only Letters 只翻转字母的更多相关文章
- LeetCode 917. Reverse Only Letters (仅仅反转字母)
题目标签:String 利用left, right 两个pointers, 从左右开始 互换 字母.如果遇到的不是字母,那么继续移动到下一个. Java Solution: Runtime beats ...
- LeetCode 917 Reverse Only Letters 解题报告
题目要求 Given a string S, return the "reversed" string where all characters that are not a le ...
- #Leetcode# 917. Reverse Only Letters
https://leetcode.com/problems/reverse-only-letters/ Given a string S, return the "reversed" ...
- 【Leetcode_easy】917. Reverse Only Letters
problem 917. Reverse Only Letters solution: class Solution { public: string reverseOnlyLetters(strin ...
- 【LeetCode】917. Reverse Only Letters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 单指针 双指针 日期 题目地址: https:/ ...
- [LeetCode&Python] Problem 917. Reverse Only Letters
Given a string S, return the "reversed" string where all characters that are not a letter ...
- 【leetcode】917. Reverse Only Letters(双指针)
Given a string s, reverse the string according to the following rules: All the characters that are n ...
- [LeetCode 92] Reverse Linked List II 翻转单链表II
对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...
- Leetcode917.Reverse Only Letters仅仅反转字母
给定一个字符串 S,返回 "反转后的" 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转. 示例 1: 输入:"ab-cd" 输出:" ...
随机推荐
- tensorflow之tf.squeeze()
tf.squeeze()函数的作用是从tensor中删除所有大小(szie)是1的维度. 给定丈量输入, 此操作返回的是相同类型的张量, 并删除所有尺寸为1的维度.如果不想删除所有尺寸为1的维度, 可 ...
- h5py报错:FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
导入h5py的时候,报错: /home/harris/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: ...
- CompletableFuture1
public class CompletableFutureTest { public static void main(String[] args) throws Exception { test5 ...
- Kubernetes SatefulSet(有状态应用部署)
Kubernetes SatefulSet(有状态应用部署) • 部署有状态应用• 解决Pod独立生命周期,保持Pod启动顺序和唯一性1. 稳定,唯一的网络标识符,持久存储2. 有序,优雅的部署和扩展 ...
- 解决Git 克隆代码 The remote end hung up unexpectedly错误
从GitHub上克隆一个项目一直不成功!猜想可能是文件太大超时或者网络太慢超时! 解决方案: 配置 git config -- git config -- 增加最低速时间,but,还是不行! 公司网络 ...
- 使用 Floccus 插件和坚果云同步 Chrome 类浏览器书签
使用 Floccus 插件和坚果云同步 Chrome 类浏览器书签 魏刘宏 2019 年 11 月 22 日 如题,本文讨论在使用 Chromium 内核的浏览器上,使用 Floccus 插件,配合 ...
- python验证码处理(1)
目录 一.普通图形验证码 这篇博客及之后的系列,我会向大家介绍各种验证码的识别.包括普通图形验证码,极验滑动验证码,点触验证码,微博宫格验证码. 一.普通图形验证码 之前的博客已向大家介绍了简 ...
- 十一:外观模式详解(Service,action与dao)
定义:外观模式是软件工程中常用的一种软件设计模式.它为子系统中的一组接口提供一个统一的高层接口.这一接口使得子系统更加容易使用. 该定义引自百度百科,它的表现很简单,将一系列子接口的功能进行整理,从而 ...
- Qt for Android使用grpc探索
利用Qt在Android上使用grpc需要*.a的静态库,Windows上编译的lib库以及linux编译出来的.a,经过尝试,均无法链接成功.本文尝试使用NDK来编译Android版本的grpc静态 ...
- SharpGL之Viewport
视口变换主是将视景体内投影的物体显示到二维的视口平面上. 在计算机图形学中,它的定义是将经过几何变换, 投影变换和裁剪变换后的物体显示于屏幕指定区域内. 前面我们讨论过的透视投影, 正射投影, 它们都 ...