LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母。
例如:
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(双指针)的更多相关文章
- 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 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 ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...
随机推荐
- 第五节: 前后端交互之Promise用法和Fetch用法
一. Promise相关 1.说明 主要解决异步深层嵌套的问题,promise 提供了简洁的API 使得异步操作更加容易 . 2.入门使用 我们使用new来构建一个Promise Promise的构造 ...
- oss创建文件夹
在进行putObject时,第二个参数写成path/your-object即可
- mysql 导出sql文件
1:find / -name mysqldump -print 先找到mysqldump 目录位置 2:/usr/local/mysql/bin/mysqldump -u root -p hanwe ...
- eosio 编译与部署
1. 名词解释 创世节点(BIOS) 用于创建初始区块,加载基础合约,供其它节点同步连接. 矿工节点(BP) 主要用于生产同步区块,计算合约产生的费用归矿工账号所有. 查询节点 不产生区块,同步区块信 ...
- acm数论之旅--欧拉函数的证明
随笔 - 20 文章 - 0 评论 - 73 ACM数论之旅7---欧拉函数的证明及代码实现(我会证明都是骗人的╮( ̄▽ ̄)╭) https://blog.csdn.net/chen_ze_hua ...
- Remmina无法保存的问题
Remmina无法保存的问题 今天用Remmina远程连接,发现密码无法保存,如果保存一直是灰色的 经过一段时间的研究,发现只要把快速连接名字改了就可以!
- Linux创建连接命令 ln -s创建软连接
ln -s 是linux中一个非常重要命令,一定要熟悉.它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s, 具体用法是:ln -s 源文件 目标文件. 当 我们需要在 ...
- 数据库中的null,踩坑笔记
问题来源:查询表中,complete_type 不为2的数据. 我的想法:select * from 表名 where complete_type != '2' (除了第2条,全部显示,没问题呀) ...
- X86汇编指令集大全【转】
[原文地址]https://blog.csdn.net/bjbz_cxy/article/details/79467688[原文地址] ---------- 一.数据传输指令 ------------ ...
- DELPHI开发和使用REDIS
DELPHI开发和使用REDIS REDIS SERVER是独立的存在,支持WINDOWS,LINUXREDIS PUB/SUB 用于聊天 只是其中的一种用法任何消息或其他类型数据 都可以必须安装 ...