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:

  1. S.length <= 100
  2. 33 <= S[i].ASCIIcode <= 122
  3. S doesn'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的更多相关文章

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

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

  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 (仅仅反转字母)

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

  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 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  9. [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 ...

随机推荐

  1. oracle 按条件删除、查询表

    ---查询表的名称,字段信息以及字段注释 select us.table_name, --表名   ut.COLUMN_NAME,--字段名称 uc.comments,--字段注释 ut.DATA_T ...

  2. 20165302Exp0 Kali安装 Week1

    一,下载 下载网址https://www.kali.org/downloads/ 二,安装(安装过程中有一部分没有截图,因此没有贴上) 创建虚拟机 选择Linux,版本Ubuntu 一直下一步,最后点 ...

  3. <数据结构与算法分析>读书笔记--函数对象

    关于函数对象,百度百科对它是这样定义的: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.又称仿函数. 听起来确实很难懂,通过搜索我找到一篇 ...

  4. WorldWind源码剖析系列:影像图层类ImageLayer

    影像图层类ImageLayer 影像图层类ImageLayer将单张影像作为纹理映射到星球表面上去.源影像必须是平面笛卡尔坐标系.该类的类图如下. 影像图层类ImageLayer提供的主要字段.属性和 ...

  5. OpenCV——Harr特征

  6. kubernetes 生命周期问题分析

    1.Failed  -- pod里至少一个容器以非0code退出,说明应用有问题,需要debug应用容器 2.pending -- 说明API对象已经被创建和保存在etcd数据库里,但是创建过程出了问 ...

  7. 关于OpenCV2.4.9在VS2012上的配置

    今天写着篇文章是由于自从上次电脑换硬盘今天再次安装OpenCV又遇到了一些问题,最后终于搞定,,,,用的版本是2.4.9,,,因为第一次配置用3.0的没有配置成功,而2.4.9的配置成功. 首先当然是 ...

  8. HO引擎近况20190110

    前两天更新完,挺兴奋 趁着兴奋把虚拟机里面的MACOSX从10.12.6升级到了10.14 然后装XCODE,虽然比较熟悉了,但是架不住慢啊 先下载了一个DMG的镜像文件,用不了,转成ISO也不行 然 ...

  9. 20155301PC平台逆向破解

    20155301PC平台逆向破解 1.掌握NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP:NOP指令即"空指令".执行到NOP指令时,CPU什么也不做,仅仅当 ...

  10. 20155316 Exp1 PC平台逆向破解(5)M

    前绪 实验收获与感想 初步从三个途径了解了什么是缓冲区溢出以及如何简单实现它,对汇编与反汇编有更直观的了解. 什么是漏洞?漏洞有什么危害? 漏洞是指机器体制设计时所没有顾及到的.可以被利用的bug,放 ...