Given a string s, reverse the string according to the following rules:
  • All the characters that are not English letters remain in the same position.
  • All the English letters (lowercase or uppercase) should be reversed.
class Solution {
public:
string reverseOnlyLetters(string s) {
//用栈或者双指针即可
//two-pointer
int n=s.size();
int left=0,right=n-1;
while(left<right)
{
//这样写是错误的:~((s[left]>='A'&&s[left]<='Z')||(s[left]>='a'&&s[left]<='z') 按位取反以及逻辑取非不一样
//写法和快排思路相似
while(left<right && !((s[left]>='A'&&s[left]<='Z')||(s[left]>='a'&&s[left]<='z')))
{
//cout<<~((s[left]>='A'&&s[left]<='Z')||(s[left]>='a'&&s[left]<='z'))<<endl;
left++;
}
cout<<left<<endl;
while(left<right && !((s[right]>='A'&&s[right]<='Z')||(s[right]>='a'&&s[right]<='z')))
{
right--;
}
//cout<<left<<"-"<<right<<endl;
if(left<right)
{
//cout<<left<<"-"<<right<<endl;
char tmp=s[left];
s[left]=s[right];
s[right]=tmp;
left++;
right--;
}
}
return s;
}
};

【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

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

  3. LeetCode 917 Reverse Only Letters 解题报告

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

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

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

  5. 【Leetcode_easy】917. Reverse Only Letters

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

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

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

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

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

  8. LeetCode 345. Reverse Vowels of a String(双指针)

    题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...

  9. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

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

随机推荐

  1. Java安全之Thymeleaf SSTI分析

    Java安全之Thymeleaf SSTI分析 写在前面 文章首发:https://www.anquanke.com/post/id/254519 最近看了一遍Thymeleaf,借此机会学习一下Th ...

  2. 使用getopt 解析参数

    getopt被用来解析命令行选项参数. #include <unistd.h> extern char *optarg; //选项的参数指针 extern int optind, //下一 ...

  3. C 函数指针语法总结

    C 函数指针语法总结 函数指针 定义 每一个函数都占用一段内存单元,它们有一个起始地址,指向函数入口地址的指针称为函数指针. 注意:函数指针的本质是一个指针变量,且指针指向的函数的入口地址. 语法 返 ...

  4. forceUpdate() & set

    前言 在开发过程中,我们时常会遇到这样一种情况:当vue的data里边声明或者已经赋值过的对象或者数组(数组里边的值是对象)时,向对象中添加新的属性,如果更新此属性的值,是不会更新视图的. 根据官方文 ...

  5. 05 | 箭头函数 | es6

    基本用法 参数列表)=> {函数体} var f = v => v; 上面的箭头函数等同于: var f = function(v) { return v; }; 如果箭头函数不需要参数或 ...

  6. Sqlserver中判断表是否存在

    在sqlserver(应该说在目前所有数据库产品)中创建一个资源如表,视图,存储过程中都要判断与创建的资源是否已经存在  在sqlserver中一般可通过查询sys.objects系统表来得知结果,不 ...

  7. Python爬虫中的URLError\HTTPError异常类,异常的抛出

    # _*_ coding : utf-8 _*_# @Time : 2021/11/2 14:20# @Author : 秋泊酱 import urllib.request import urllib ...

  8. Nginx支持WebSocket反向代理

    WebSocket是目前比较成熟的技术了,WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选择.其为HTML5的一部分,WebSocket相较于原来开发这类app的 ...

  9. CTF入门学习3->Web通信基础

    Web安全基础 01 Web通信 这个部分重点介绍浏览器与Web服务器的详细通信过程. 01-00 URL协议 只要上网访问服务器,就离不开URL. URL是什么? URL就是我们在浏览器里输入的站点 ...

  10. [atAGC014E]Blue and Red Tree

    不断删除重边,然后将两个点的边集启发式合并(要考虑到两棵树),合并时发现重边就加入队列,最后判断是否全部删完即可 1 #include<bits/stdc++.h> 2 using nam ...