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的更多相关文章

  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 ...

  2. 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 ...

  3. Leetcode 345 Reverse Vowels of a String 字符串处理

    题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...

  4. LeetCode 345. Reverse Vowels of a String(双指针)

    题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...

  5. Leetcode 345 Reverse Vowels in a String

    两个for 第一个for将每一个元音依次存放进一个char数组 第二个for,每检测到元音,就从char数尾部开始,依次赋值 如何检测元音呢?当然写一个冗长的if(),不过我们有更好的选择 hashs ...

  6. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  7. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【Spring源码分析系列】bean的加载

    前言 以 BeanFactory bf  = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过 ...

  2. slideout

    这里在原有slideout.js增加了shade的遮罩功能 核心: 1,此插件的使用需要配合dom来用: <!-- 左边 --> <nav id="menu" c ...

  3. LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)

    题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description   Problem :已知字符串s,求出其 ...

  4. 如何启动、关闭和设置ubuntu防火墙

    如何启动.关闭和设置ubuntu防火墙 引自:http://www.cnblogs.com/jiangyao/archive/2010/05/19/1738909.html 就这句话就够了,下面的可以 ...

  5. 未能加载文件或程序集“XX.XXX.Web”或它的某一个依赖项。试图加载格式不正确的程序

    IIS应用程序池->右键高级设置->启用32位应用程序  设置为true

  6. numpy生成随机数组

    python想要生成随机数的话用使用random库很方便,不过如果想生成随机数组的话,还是用numpy更好更强大一点. 生成长度为10,在[0,1)之间平均分布的随机数组: rarray=numpy. ...

  7. GitHub 终端加速最佳实践

    终端加速 GitHub 方法的前置条件, 一是购买了加速服务或者租用 VPS 搭建加速服务, 二是系统是 macOS, 三是终端是 iTerm, 四是 Shell 是 zsh. 终端加速 GitHub ...

  8. poj3417Network【LCA】【树形DP】

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

  9. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  10. XTU 1267 - Highway - [树的直径][2017湘潭邀请赛H题(江苏省赛)]

    这道题可能有毒……总之一会儿能过一会儿不能过的,搞的我很心烦…… 依然是上次2017江苏省赛的题目,之前期末考试结束了之后有想补一下这道题,当时比较懵逼不知道怎么做……看了题解也不是很懂……就只好放弃 ...