【一天一道LeetCode】#345. Reverse Vowels of a String
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = “hello”, return “holle”.Example 2:
Given s = “leetcode”, return “leotcede”.
(二)解题
题目大意:将字符串中的元音字母反转位置。
解题思路:用两个指针i和j,分别从头尾开始向中间扫描,每当碰到一个元音字母就停下来,交换位置,然后两个继续向中间扫描。
具体思路看代码。
class Solution {
public:
string reverseVowels(string s) {
int i =0;
int j =s.length()-1;
while(i<=j)
{
while((s[i]!='a')&&(s[i]!='e')&&(s[i]!='i')&&(s[i]!='o')&&(s[i]!='u')&&(s[i]!='A')&&(s[i]!='E')&&(s[i]!='I')&&(s[i]!='O')&&(s[i]!='U')) i++;//找到第一个元音字母时停止
while((s[j]!='a')&&(s[j]!='e')&&(s[j]!='i')&&(s[j]!='o')&&(s[j]!='u')&&(s[j]!='A')&&(s[j]!='E')&&(s[j]!='I')&&(s[j]!='O')&&(s[j]!='U')) j--;//找到第一个元音字母时停止
if(i<=j){//如果i<=j,则交换位置
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;j--;//继续往中间扫描
}
}
return s;
}
};
【一天一道LeetCode】#345. Reverse Vowels of a String的更多相关文章
- Python [Leetcode 345]Reverse Vowels of a String
题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- LeetCode 345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels(元音字母) of a string. Example ...
- Leetcode 345 Reverse Vowels of a String 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...
- LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- Leetcode 345 Reverse Vowels in a String
两个for 第一个for将每一个元音依次存放进一个char数组 第二个for,每检测到元音,就从char数尾部开始,依次赋值 如何检测元音呢?当然写一个冗长的if(),不过我们有更好的选择 hashs ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- 345. Reverse Vowels of a String【easy】
345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...
随机推荐
- Python Django缓存,信号,序列化,文件上传,Ajax登录和csrf_token验证
本节内容 models操作 Django的缓存 请求方式 序列化 Form 配合Ajax实现登录认证 上传文件 Ajax csrf_token验证方式 1 models操作 单表查询: curd(增 ...
- Python笔记(一)——打印输出
一.输出语句input 输出语句print 例:用户输入 username = input("username:") #变量名 显示的字符 password = input( ...
- c# datatable row
在指定索引位置插入新行 string fzmc = rs["fzmc"].ToString(); string mkmc = rs["mkmc"].ToStri ...
- CMD远程连接服务器上的MySQL
1.打开CMD命令行. 2.输入mysql -h要远程的IP地址 -u设置的MySQL用户名 -p登录用户密码 例如:mysql -h192.168.0.110 -uroot -p1233 (如果不能 ...
- 模块机制 之commonJs、node模块 、AMD、CMD
在其他高级语言中,都有模块中这个概念,比如java的类文件,PHP有include何require机制,JS一开始就没有模块这个概念,起初,js通过<script>标签引入代码的方式显得杂 ...
- mongoDB安装和启动
安装: 1. mongodb(V3.4.7)安装包下载地址:https://www.mongodb.com/download-center#community 下载成功后直接运行,安装模式选择cust ...
- 3-学习GPRS_Air202(需要知道的关于Lua的一些基本的知识)
http://www.cnblogs.com/yangfengwu/p/8948935.html 学东西一定是打破沙锅学到底,有问题就解决问题,不要试图去回避或者放弃解决当前的问题,如果总是回避或 ...
- Linux 新系统个人配置
1,装codeblocks 2,装vim,检查gcc,g++,修改vim环境 cd ~vim .vimrc添加如下几行:set shiftwidth=4 (表示每一级缩进的长度)s ...
- 链表的无锁操作 (JAVA)
看了下网上关于链表的无锁操作,写的不清楚,遂自己整理一部分,主要使用concurrent并发包的CAS操作. 1. 链表尾部插入 待插入的节点为:cur 尾节点:pred 基本插入方法: do{ pr ...
- MySQL EXTRACT() 函数
定义和用法 EXTRACT() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 EXTRACT(unit FROM date) date 参数是合法的日期表达式.unit 参 ...