#Leetcode# 917. Reverse Only Letters
https://leetcode.com/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:
S.length <= 10033 <= S[i].ASCIIcode <= 122Sdoesn't contain\or"
代码:
class Solution {
public:
string reverseOnlyLetters(string S) {
string ans = "";
vector<char> v;
int len = S.length();
for(int i = len - 1; i >= 0; i --) {
if(S[i] >= 'a' && S[i] <= 'z' || S[i] >= 'A' && S[i] <= 'Z')
v.push_back(S[i]);
}
int rec = 0;
for(int i = 0; i < len; i ++) {
if(S[i] >= 'a' && S[i] <= 'z' || S[i] >= 'A' && S[i] <= 'Z') {
char c = v[rec];
ans += c;
rec ++;
}
else ans += S[i];
}
return ans;
}
};
#Leetcode# 917. Reverse Only Letters的更多相关文章
- [LeetCode] 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, return the "reversed" string where all characters that are not a le ...
- LeetCode 917. Reverse Only Letters (仅仅反转字母)
题目标签:String 利用left, right 两个pointers, 从左右开始 互换 字母.如果遇到的不是字母,那么继续移动到下一个. Java Solution: Runtime beats ...
- 【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 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
随机推荐
- php isset+{} 判断字符串长度比strlen效率高
PHP 变量后面加上一个大括号{},里面填上数字,就是指 PHP 变量相应序号的字符.例如:$str = 'hello';echo $str{0}; // 输出为 hecho $str{1}; // ...
- php 导出导入excel
首先需要去官网https://github.com/PHPOffice/PHPExcel/下载PHPExcel,下载后只需要Classes目录下的文件即可. 链接: https://pan.baidu ...
- WorldWind源码剖析系列:数学引擎类MathEngine
PluginSDK中的MathEngine类是密封类.不可继承,主要完成通用的数学计算功能.由于按平面展开层层划分,所以在WW里用到一个row,col的概念,类MathEngine封装了从行/列到经/ ...
- jsp泛型支持
今天在使用idea做练习时,某个jsp页面报错如下: '<>'operator is not allowed for source level below 1.7 出错代码如下: Map& ...
- Android使用动态代理搭建网络模块框架
1.Java中的动态代理相信大多数朋友都接触过,在此就不再赘述,如果有不明白的朋友,可以到网上搜一下(一搜一大堆,呵呵..) 2.本节主要阐述一下如何使用动态代理框架实现Android应用的瘦身开发. ...
- Makefile 学习记录一
2019年2月5日10:18:13 大年初一 整理 uboot-2018-11 源代码 根目录下的 Makefile . ifeq ("$(origin O)", "c ...
- 利用IDA6.6进行apk dex代码动态调试
网上公开IDA6.6已经有一段时间,这个版本有个好处就是可以动态调试java代码.正好现在需要动态调试,所以顺便练习一下. 根据android的官方文档,如果要调试一个apk里面的dex代码,必须满足 ...
- Django Rest Framework源码剖析(六)-----序列化(serializers)
一.简介 django rest framework 中的序列化组件,可以说是其核心组件,也是我们平时使用最多的组件,它不仅仅有序列化功能,更提供了数据验证的功能(与django中的form类似). ...
- 20155311《网络对抗》PC平台逆向破解(二)
20155311<网络对抗>PC平台逆向破解(二) shellcode注入 什么是shellcode? shellcode是一段代码,溢出后,执行这段代码能开启系统shell. 前期准备- ...
- [python]记录Windows下安装matplot的经历
最近学习在看<机器学习实战>一书,第二章的时候要用到Natplotlib画图,于是便开始安装Matplotlib.本文所用到的所有安装包都可以在文末的链接中找到. 首先从Matplotli ...