Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

  1. Input: "hello"
  2. Output: "holle"

Example 2:

  1. Input: "leetcode"
  2. 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

  1. class Solution:
  2. def reverseVowels(self, s):
  3. indexs, s, vowels, length = [], list(s), "euioaEUIOA", len(s)
  4.  
  5. for i in range(length):
  6. if s[i] in vowels:
  7. indexs.append(i)
  8. l2 = len(indexs)
  9. for j in range(l2//2):
  10. l, r = indexs[j], index[l2-j-1]
  11. s[l], s[r] = s[r], s[l]
  12. 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. 日记整理---->2016-11-25

    2017-03-02开始,记录的一些知识点.岁月长,三更漏.漫漫回廊,依稀人空瘦.借酒消愁入断肠,倚剑笑我,我独自寻殇. 一.vx中的v-bind和{{}}的区别 <td class=" ...

  2. SharpGL学习笔记(三) 投影变换和视点变换

    从本节开始,我们使用SharpGL带的VS2010扩展,来直接生成SharpGL工程. 如果你新建项目时,没有看到下面的SharpGL项目,那么请事先在SharpGL源代码中找到一个叫 ”SharpG ...

  3. Git 多人协作开发的过程

    Git可以完成两件事情: 1. 版本控制 2.多人协作开发 如今的项目,规模越来越大,功能越来越多,需要有一个团队进行开发. 如果有多个开发人员共同开发一个项目,如何进行协作的呢. Git提供了一个非 ...

  4. PostgreSQL索引介绍

    h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...

  5. FPGA时序约束的几种方法 (转)

    FPGA时序约束的几种方法 对自己的设计的实现方式越了解,对自己的设计的时序要求越了解,对目标器件的资源分布和结构越了解,对EDA工具执行约束的效果越了解,那么对设计的时序约束目标就会越清晰,相应地, ...

  6. iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD

    原文出处: 容芳志的博客 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Central Dispatch) 这 ...

  7. tomcat如何配置启动时自动部署webapps下的war包

    1.找到 tomcat安装目录/conf/server.xml 2.修改host元素的配置如下: <Host name="localhost" appBase="w ...

  8. yii---模型的创建

    在 model/ 路径新建 Test.php 模型 我们类的名称一定要与数据表的名称相同. 继承 yii\db\ActiveRecord 类: 在模型类中 声明 tableName() 指定表名 // ...

  9. Spark2 SQL configuration参数配置

    查看当前环境SQL参数的配置 spark.sql("SET -v") key value spark.sql.hive.version 1.2.1 spark.sql.source ...

  10. SVN Hook造成SVN提交速度慢的问题

    单就个人感情来说,我其实喜欢git.但显然subversion才是更普遍的版本控制管理工具,适合用在团队开发中. 那么,有一个很常见的需求就是把工程师提交的代码,更新到htdocs目录,这时候需要用s ...