题意:给定一个字符串,反转字符串中的元音字母。

例如:

Input: "leetcode"
Output: "leotcede"

法一:双指针

class Solution {
public:
string reverseVowels(string s) {
if(s == "") return "";
set <char> st ={'a','e','i','o','u','A','E','I','O','U'};
int head = 0;
int tail = s.size() - 1;
char ans[10000000] = {};
while(head <= tail){
char h = s[head];
char t = s[tail];
if(st.find(h) == st.end()){
ans[head++] = h;
}
else if(st.find(t) == st.end()){
ans[tail--] = t;
}
else{
ans[head++] = t;
ans[tail--] = h;
}
}
return string(ans);
}
};

法二:首先将字符串中所有元音字母按顺序记录在v中,然后逆序遍历字符串,将v中的元音字母依次替换到逆序遍历过程中遍历到的元音字母中即可。

class Solution {
public:
string reverseVowels(string s) {
if(s == "") return "";
set <char> st ={'a','e','i','o','u','A','E','I','O','U'};
vector<char> v;
int len = s.size();
for(int i = 0; i < len; ++i){
if(st.find(s[i]) != st.end()){
v.push_back(s[i]);
}
}
int vowel_len = v.size();
for(int i = len - 1, j = 0; i >= 0 && j < vowel_len; --i){
if(st.find(s[i]) != st.end()){
s[i] = v[j++];
}
}
return s;
}
};

  

LeetCode 345. Reverse Vowels of a String(双指针)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Leetcode 345 Reverse Vowels of a String 字符串处理

    题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...

  4. Leetcode 345 Reverse Vowels in a String

    两个for 第一个for将每一个元音依次存放进一个char数组 第二个for,每检测到元音,就从char数尾部开始,依次赋值 如何检测元音呢?当然写一个冗长的if(),不过我们有更好的选择 hashs ...

  5. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  6. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

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

  8. 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 ...

  9. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

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

随机推荐

  1. 「JSOI2015」圈地

    「JSOI2015」圈地 传送门 显然是最小割. 首先对于所有房子,权值 \(> 0\) 的连边 \(s \to i\) ,权值 \(< 0\) 的连边 \(i \to t\) ,然后对于 ...

  2. UVA10600 ACM Contest and Blackout

    用prim算法求最小生成树和次小生成树~ #include<cstdio> #include<algorithm> #include<cstring> using ...

  3. springMVC的概念

    1,完成一次web请求的过程 Web浏览器发起请求 Web服务器接收请求并处理请求,最后产生响应(一般为html).web服务器处理完成后,返回内容给web客户端,客户端对接收的内容进行处理并显示出来 ...

  4. Python实现重命名一个文件夹下的图片

    在网上查了一下python实现的图片重命名,工作中刚好用一下. # -*- coding:utf8 -*- import os path = '新建文件夹 (2)/' filelist = os.li ...

  5. php 基础 自动类型转换

    1.自动类型转换:表示运算的时候,Boolean,Null,String等类型,会先自动转为Integer或Float类型 null-->0 true-->1 false-->0 S ...

  6. 工具 - 正则Cheat sheet

  7. docker安装-单机/多机安装

      操作系统ubuntu14.04 16.04 v单机安装步骤: #安装httpsca证书 apt-get install apt-transport-https ca-certificates #添 ...

  8. matlab学习记录

    1.在命令框输入preferences,可以调整字体大小 2.产生正太分布函数 参考:https://blog.csdn.net/s334wuchunfangi/article/details/816 ...

  9. Python:列表类型

    概念 列表:有序的,可变的,元素集合 因为列表和字符串都是序列类型,所以很多操作和字符串很相似 但是注意:列表是可变类型,字符串是不可变类型 定义 基本定义 定义方法:[ 元素1, 元素2, .... ...

  10. CSS3绘制不规则图形,代码收集

    三角形系列(三角形.倒三角.左三角.右三角.左上三角.右上三角.左下三角.右下三角) 主要用到的是:宽度高度设置为0, border的各个边的设置(各个边的透明或不透明): .triangle-up ...