345. Reverse Vowels of a String(python+cpp)
题目:
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:Input: "hello" Output: "holle"
Example 2:
Input: "leetcode" Output: "leotcede"
Note: The vowels does not include the letter “y”.
解释:
仅仅翻转字符串中的元音字母,用双指针即可,注意,用set()
保存元音字母比用list
保存元音字母查询速度更快。
注意,python和c++中都无法直接给string[i]
赋值,所以需要先转换成数组形式。
python代码:
class Solution(object):def reverseVowels(self, s):""":type s: str:rtype: str"""vowels=set("aeiouAEIOU")result=list(s)left,right=0,len(s)-1while left<right:if (result[left] in vowels) and (result[right] in vowels):result[left],result[right]=result[right],result[left]left+=1right-=1else:if result[right] not in vowels:right-=1if result[left] not in vowels :left+=1return ''.join(result)
c++代码:
#include<set>
#include<algorithm>
using namespace std;
class Solution {public:string reverseVowels(string s) {string vowels="aeiouAEIOU";set<char> set_vowels(vowels.begin(),vowels.end());vector<char> list(s.begin(),s.end());int left=0,right=s.size()-1;while (left<right){if (set_vowels.find(list[left])!=set_vowels.end() && set_vowels.find(list[right])!=set_vowels.end()){swap(list[left],list[right]);left++;right--;}else{if (set_vowels.find(list[left])==set_vowels.end())left++;if (set_vowels.find(list[right])==set_vowels.end())right--;}}string result="";//注意是vector<char>转string 不是vector<string>转stringresult.insert(result.begin(),list.begin(),list.end());return result;}
};
总结:
stl中学会set.find()
345. Reverse Vowels of a String(python+cpp)相关推荐
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 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 ...
- leetcode345——Reverse Vowels of a String(C++)
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Gi ...
- LeetCode:345. Reverse Vowels of a String
051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...
- 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 1 ...
- 【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 1 ...
- 345. Reverse Vowels of a String
题目: Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', ' ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51429823 Subject 出处:https://leetcode.com/prob ...
- Leetcode 345: Reverse Vowels of a String
问题描述: Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', ...
最新文章
- 年终福利 | 京东虚拟平台团队问答专场
- 应用商店后台MIS的一些思考
- CSS float的相关图文详解(二)
- PyCharm运行出现 Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run
- php将pdf保存文件到本地,将生成的PDF文件存储在服务器上
- java操作集合中 concurrentModifyException 异常的原因分析
- redis_lua_nginx环境配置过程
- Cron 表达式解析,crontab表达式解析
- 华罗庚数学竞赛如何报名?
- 拼图复原_玩过上百款拼图后,我总结出这份超详细的拼图年龄对照表!(收藏贴)...
- Java技术体系概述
- 逻辑题(持续更新中)
- 2021-04-21微信大数据对于精准营销的意义有哪些?
- 【学习笔记】数组的地址分配及指针数组的使用
- python协同过滤可以预测吗_基于用户的协同过滤推荐算法原理-附python代码实现...
- HDU-6578 Blank(DP)2019暑假杭电多校第一场
- codeforces 1669F
- Jfrog Artifactory安装及备份恢复搭建
- 基础环境搭建——基于Spark平台的协同过滤实时电影推荐系统项目系列博客(五)
- 第四章 Docker镜像
热门文章
- 【git学习】git clone 出错 error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
- p2p服务器的协议,P2P文件传输协议之BitTorrent协议
- 《鸟哥的Linux私房菜》第四版辅助文档
- 重做日志文件(redo log file)
- 配置消息推送服务器,自动化部署和消息推送
- 夕拾朝花——我的2016
- 【渝粤教育】广东开放大学 搜索引擎优化SEO 形成性考核
- 大饼震荡不变,新平台搭建?
- java:字符和字节区别
- 李航《统计学习方法》朴素贝叶斯的python实现