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

例如:

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. 每天进步一点点------DE2-70-TV例子说明

    module Reset_Delay(iCLK,iRST,oRST_0,oRST_1,oRST_2); input iCLK; input iRST; output reg oRST_0; outpu ...

  2. static关键字 weak关键字

    1.static关键字 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart){ ...} 在函数前面加了一个stati ...

  3. JAVA基础学习(2)之判断

    2判断 2.1比较 2.1.1比较 System.out.println(amount>=10);输出的值为true或false 2.1.2关系运算 优先级 <算术运算 >赋值运算 ...

  4. hibernate并发时的事务处理

    两个方法 方法一: public void saveTest() { try { System.out.println("saveTest start"); User user = ...

  5. 20-02-27 hive表的几个问题

    1.hive表的动态分区 2.hive  表如何修改列名 3.group  by  对统计指标的影响  (group by 的本质) 4.row_number 对数据的影响

  6. 学习笔记(25)- NLP的几个概念

    NLP的几个概念 从技术研究的角度,简单介绍自然语言处理的几个概念 1. 对抗学习 主要指对抗生成网络. 2个主要构成:判别器.生成器 判别模型尽可能提取特征正确率增加的模型,生成模型尽可能" ...

  7. MySQL导出数据到文件报错

    执行如下语句: mysql> select * from users into outfile "F:\Develop\MySQL57\Uploads\users.txt" ...

  8. Git基础及进阶-系统总结

    Git基础及进阶-系统总结 by 小强 2019-07-01 考虑到入职后不仅需要熟练掌握git的基本使用,在企业实际操作中还涉及一些进阶指令.作为一个程序员,熟练使用工具是一项基本技能,也是程序员的 ...

  9. 「CF815C」Karen and Supermarket

    传送门 Luogu 解题思路 树形背包. 设 \(f[i][j][0/1]\) 表示在以 \(i\) 为根的子树中选 \(j\) 件商品的最少花费. 边界条件: \(f[i][j][0] = \min ...

  10. numpy中的max()函数

    1.ndarray.max([int axis]) 函数功能:求ndarray中指定维度的最大值,默认求所有值的最大值. axis=0:求各column的最大值 axis=1:求各row的最大值