[LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers
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".
思路用一个indexs来存是vowels的index, 然后分别对调index的l, r, 最后返回字符串. Note: array可以modify, 但是string不能, 所以要将string变为array, 最后再转换为string.
Code
class Solution:
def reverseVowels(self, s):
indexs, s, vowels, length = [], list(s), "euioaEUIOA", len(s) for i in range(length):
if s[i] in vowels:
indexs.append(i)
l2 = len(indexs)
for j in range(l2//2):
l, r = indexs[j], index[l2-j-1]
s[l], s[r] = s[r], s[l]
return "".join(s)
[LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers的更多相关文章
- 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 ...
- 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 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...
- LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- 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 ...
随机推荐
- 【Mac】安装MAMP的PHPredis扩展
1 下载phpredis扩展安装包 cd /usr/local git clone https://github.com/nicolasff/phpredis.git 2 依次执行以下操作完成安装 $ ...
- Qt编写的RTSP播放器+视频监控(ffmpeg版本)
记得四年前就写了个简易版本的,当时写得非常粗糙,代码实在惨不忍睹,时隔多年后,重新写了个版本,同时还解决了以前不支持6画面8画面切换等异形布局的问题.1:可以用过目录下的rtsp.txt设置要显示的视 ...
- 转载>>ASCII、UTF8、Uncicode编码下的中英文字符大小
原地址:http://www.tracefact.net/CSharp-Programming/Network-Programming-Part2.aspx ASCII.UTF8.Uncicode编码 ...
- jQuery队列(二)
继续阅读队列提供的方法. jQuery.extend({ queue: function( elem, type, data ) {}, // 将data按照某种类型存储到elem对应的队列中, ...
- SharpGL学习笔记(一) 平台构建与Opengl的hello World
(一)平台构建与Opengl的hello World OpenGL就是3d绘图的API,微软针和它竞争推出D3D,也就是玩游戏时最常见的DirectorX组件中的3d功能. 所以不要指望windows ...
- Android中openmax实现框架
OMX中 OMXNodeInstance 负责创建并维护不同的实例,这些实例是根据上面需求创建的,以node作为唯一标识.这样播放器中每个OMXCodec在OMX服务端都对应有了自己的OMXNodeI ...
- C语言位操作--逻辑运算符组合
假设读者熟悉普通代数与布尔代数,下面是部分常见的涉及到加法.减法与逻辑运算符的组合: a. -x=~x+1 b. =~(x-1) c. ~x=-x-1 ...
- [工具] multidesk
MultiDesk 是一个选项卡(TAB标签)方式的远程桌面连接 (Terminal Services Client). http://www.hoowi.com/multidesk/index_ch ...
- fis前端开发框架
FIS是专为解决前端开发中自动化工具.性能优化.模块化框架.开发规范.代码部署.开发流程等问题的工具框架,相比gulp和grunt更傻瓜化,上手更容易,最近抽空学习了一下,分享一下心得. FIS官网: ...
- 数据库操作相关(sql语句-命令行)
创建数据库: create database books; 创建用户: mysql> grant select,insert,delete,uptate -> on books.* ...