leetcode 345Reverse vowels of a string
2016-04-25 19:44
375 查看
元音字母有大小写
整体思路,顺序度string,碰到元音字母就存起来,读完再顺序读一遍,这次碰到元音字母就用存起来的数组倒序替换
class Solution {
public:
string reverseVowels(string s) {
istringstream str(s);//将string字符绑定到输入流
vector<char>vowel;
char ch;
while(str>>ch){
if(isVowel(ch))
vowel.push_back(ch);//如果是元音字母就保存
}
for(int i = 0,j = 0;i != s.size();++i)
if(isVowel(s[i])){
s[i] = vowel[vowel.size() - 1 - j];
j += 1;
}
return s;
}
//判断是否为元音字母,含大小写
bool isVowel(char ch)
{
int flag = 0;
switch(ch){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
flag = 1;
break;
default:
flag = 0;
break;
}
if(flag)
return 1;
else return 0;
}
};
整体思路,顺序度string,碰到元音字母就存起来,读完再顺序读一遍,这次碰到元音字母就用存起来的数组倒序替换
class Solution {
public:
string reverseVowels(string s) {
istringstream str(s);//将string字符绑定到输入流
vector<char>vowel;
char ch;
while(str>>ch){
if(isVowel(ch))
vowel.push_back(ch);//如果是元音字母就保存
}
for(int i = 0,j = 0;i != s.size();++i)
if(isVowel(s[i])){
s[i] = vowel[vowel.size() - 1 - j];
j += 1;
}
return s;
}
//判断是否为元音字母,含大小写
bool isVowel(char ch)
{
int flag = 0;
switch(ch){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
flag = 1;
break;
default:
flag = 0;
break;
}
if(flag)
return 1;
else return 0;
}
};
相关文章推荐
- Impala与Hive的比较
- Java面试中的多线程问题
- 第一次冲刺阶段(八)
- react-redux单元测试(基于react-addons-test-utils,mocha)
- vs中正常IIS发布网站后css样式、图片丢失jQuery报错 $ is not defined
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
- 拆点最大流-POJ-3436-ACM Computer Factory
- bnu 51638 Air Hockey(三分+二分)(北师16校赛)
- [动态树分治]
- 地图使用-----MapKit介绍
- Git远程和分支管理
- 大数据 > 数据平台方案评估
- 新闻发布系统——触发器和存储过程的区别
- listview
- spring-boot 初学一
- 5-1 Verilog Moore状态机之十字路口
- Oracle中insert into select和select into的用法(异常0RA-00905:missing keyword的解决)
- C语言练习之单词首字母大写
- UVA_11157_Dynamic Frog
- GSM Hacking:使用BladeRF、树莓派、YatesBTS搭建便携式GSM基站