LeetCode:917. Reverse Only Letters
051201
题目
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!"
解题思路
第一反应,这道题和345. Reverse Vowels of a String一模一样呀,只是一个是元音字母交换,一个是字母交换。
第一种!
class Solution {
public:string reverseOnlyLetters(string S) {if(S.length()<=1) return S;int head=0, tail=S.length()-1;while(head<tail){head = S.find_first_of("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm", head);tail = S.find_last_of("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm", tail); if (head < tail) swap(S[head++], S[tail--]); }return S;}
};
第二种
class Solution {
public:string reverseOnlyLetters(string S) {if(S.length()<=1) return S;int head=0, tail=S.length()-1;while(head<tail){while(head<tail && !isalpha(S[head])) head++;while(head<tail && !isalpha(S[tail])) tail--;swap(S[head++], S[tail--]); }return S;}
};
原来是不是字母可以用isalpha这个函数,python和c++都有,学到了!
LeetCode:917. Reverse Only Letters相关推荐
- LeetCode:345. Reverse Vowels of a String
051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...
- LeetCode:917. 仅仅反转字母————简单
题目 917. 仅仅反转字母 给你一个字符串 s ,根据下述规则反转字符串: 所有非英文字母保留在原有位置. 所有英文字母(小写或大写)位置反转. 返回反转后的 s . 示例 1: 输入:s = &q ...
- LeetCode算法入门- Reverse Integer-day6
LeetCode算法入门- Reverse Integer-day6 Given a 32-bit signed integer, reverse digits of an integer. Exam ...
- LeetCode 150. Evaluate Reverse Polish Notation
LeetCode 150. Evaluate Reverse Polish Notation Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/42 ...
- LeetCode:贪心算法(30道经典题目)
LeetCode:贪心算法 求解最优化的问题常常会有一系列的步骤,而每个步骤往往会面临着选择.贪心算法在每一步都做出最优解,寄希望于通过局部最优解来获得全局最优解.贪心算法往往是这种自顶向下的设计,先 ...
- LeetCode:汇总区间【228】
LeetCode:汇总区间[228] 题目描述 给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2&quo ...
- LeetCode:跳跃游戏【55】
LeetCode:跳跃游戏[55] 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.判断你是否能够到达最后一个位置. 示例 1: 输入: ...
- LeetCode:贪婪算法
LeetCode:贪婪算法 贪婪算法基础 我 717. 1-bit and 2-bit Characters class Solution {public boolean isOneBitCharac ...
- Java黑皮书课后题第6章:**6.3(回文整数)使用下面的方法头编写两个方法:……使用reverse方法实现isPalindrome。如果一个数字的逆序数和它自身相等,这个数就称为回文数。
6.3(回文整数)使用下面的方法头编写两个方法:--使用reverse方法实现isPalindrome.如果一个数字的逆序数和它自身相等,这个数就称为回文数. 题目 题目概述 破题:假设没有提示语句( ...
最新文章
- 返回顶部的js实现(jQuery/MooTools)
- protoc支持c_protoc 命令参数
- AI产品之路:神经元与神经网络
- delphi解决:Thread error 拒绝访问(5)
- visio二次开发___事件篇___事件分类
- 手把手教你使用 1D 卷积和 LSTM 混合模型做 EEG 信号识别
- 单片机彩灯移动实验_单片机课程设计彩灯实验.doc
- iphone备忘录突然没了_苹果突然下架12 天猫:双11有惊喜!iPhone12 mini配色缩水
- SpringBoot-视图解析与模板引擎
- php 获取手机设备的ID,开源 | 通过提取神经元知识实现人脸模型压缩:MobileID可在移动设备上快速运行...
- Linux多网卡配置高级策略路由---从哪里来就回哪里去~!
- Linux 开源词典工具及下载链接
- ubuntu 好用的桌面小工具
- 基金收益率计算5:金额加权收益率(MWRR)和时间加权收益率(TWRR)
- latex如何更改某一段落的字体_LaTeX基础四:字体字号设置
- 国网大数据中心是如何进行数据治理的?
- opencv--图像色彩和对比度
- 企业QQ找回聊天记录
- 文件用手机拍照片打印时,打印出来总是有黑阴影,如何去掉黑色阴影打印清晰的图片
- 表达式的操作(UF函数)