【leetcode】917. Reverse Only Letters(双指针)
- 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(双指针)的更多相关文章
- [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
https://leetcode.com/problems/reverse-only-letters/ Given a string S, return the "reversed" ...
- 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 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
随机推荐
- 2021.11.4测试T1-妹子
题目 今天测试,直接挂完了 写了四个小时,最后发现自己题目理解错误了 有两个区间,在输入了 \(l\) 和 \(r\) 以后,进行查询 \[ min(max(a_1,a_2,...a_p,b_{p+1 ...
- journalctl常用命令
journalctl -xe 查看全部日志# 以flow形式查看日志 $ journalctl -f # 查看内核日志 $ journalctl -k # 查看指定服务日志 $ journalctl ...
- IP基础 & 子网划分 & 路由寻址
IP地址详解 IP地址概念 就像用身份证号码来区别毎个人一样,为了区别 网上的每台计算机,我们给因特网上的每一台计算机一个唯一的编号 ,我们把它称为IP地址 IP地址就是一个唯一标识 ,是一段网络编码 ...
- uni-app app端设置全屏背景色
设置page:{样式},博主调试的时候在app端不起作用,设置配置文件的backgroundColor也没有用,所以博主就使用了一个稍微比较偏的办法解决了,没有用获取设备信息的api来实现 具体操作就 ...
- Apache Shiro 反序列化漏洞分析
Shiro550 环境搭建 参考:https://www.cnblogs.com/twosmi1e/p/14279403.html 使用Docker vulhub中的环境 docker cp 将容器内 ...
- spring boot+vue实现H5聊天室客服功能
spring boot+vue实现H5聊天室客服功能 h5效果图 vue效果图 功能实现 spring boot + webSocket 实现 官方地址 https://docs.spring.io/ ...
- 微信小程序(六)
MINA 框架: 启动: 冷启动,热启动 加载: 生命周期 路由: 事件: 事件时视图层到逻辑层的通信方式 事件可以将用户的行为反馈到逻辑层进行处理 事件可以绑定在组件上,触发事件后就会执行逻辑层对应 ...
- SpringCloud升级之路2020.0.x版-37. 实现异步的客户端封装配置管理的意义与设计
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 为何需要封装异步 HTTP 客户端 WebClient 对于同步的请求,我们使用 spri ...
- deepin系统安装与linux磁盘分区
制作系统盘工具 链接:https://pan.baidu.com/s/1zcV0oulUErUdU0PAGxTDdw 提取码:1111 链接:https://pan.baidu.com/s/13zBd ...
- 8.5 Ingress实现基于域名的多虚拟主机、URL转发、及多域名https实现等案例
1.什么是Ingress Ingress 公开了从k8s集群外部到集群内服务的 HTTP 和 HTTPS 路由. 流量路由由 Ingress 资源上定义的规则控制. 可以将 Ingress 配置为服务 ...