题目描述:

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

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

解题思路:

设置两个指针,分别从前往后与从后往前移动

代码如下:

class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
vowels = set(list('aeiouAEIOU'))
s = list(s)
start = 0
end = len(s) - 1 while start <= end:
if s[start] in vowels and s[end] in vowels :
s[start], s[end] = s[end], s[start]
start += 1
end -= 1
else:
if s[start] not in vowels:
start += 1
if s[end] not in vowels:
end -= 1 return ''.join(s)

  

Python [Leetcode 345]Reverse Vowels of a String的更多相关文章

  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. Leetcode 345 Reverse Vowels of a String 字符串处理

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

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

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

  4. Leetcode 345 Reverse Vowels in a String

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

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

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

  6. 345. Reverse Vowels of a String - LeetCode

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

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

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

  9. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

随机推荐

  1. PHP开发入行真功夫 三扬科技

    前言与目录 PHP开发入行真功夫 前言 PHP开发入行真功夫 目录   第2章 基本语法 2.1.1 判断闰年程序 2.1.2 我们现在能做的…… 2.2.1 PHP的语言概貌 2.2.2 为我们的程 ...

  2. .htaccess的基本作用及相关语法介绍

    .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. .htaccess主要的作用有:URL重写.自定义错误页面.MIME类型配置以及访问权限控制等.主要体现在伪静态的应 ...

  3. regexp_substr在oracle9i的替换方案

    regexp_substr()方法在oracle9i尚不存在,是从oracle10g开始新增,如下为替换解决方法. SELECT regexp_substr('|83~GT67XVFU0RCVIV|6 ...

  4. 使用secureCRT远程Linux,出现远程主机拒绝连接

    1.查看是否开启远程连接, 控制面板->程序和功能->打开或关闭windows功能->勾选telnet服务器和telnet客户端2.cmd命令行输入telnet ip地址 端口号(比 ...

  5. awk过滤统计不重复的行

    awk以‘\t’为分隔符区分列 cat logs | grep IconsendRedirect | grep 1752 | awk -F'\t' '{print $8}'| wc -l awk过滤统 ...

  6. 使用 JAX-RS 简化 REST 应用开发

    本文将详细介绍 Java EE 6 中所引入的对 JSR-311 (JAX-RS : Java API for RESTful Web Services) 的支持,并通过一个完整的示例应用程序展示 J ...

  7. android-exploitme(四):参数篡改

    今天我们来测试请求中参数的篡改,这个在web安全测试中是常用的,拦截请求包,修改参数,提交 1.  首先我们需要启动模拟器,并使用本机的代理(加上参数-partition-size的目的是为了可以往a ...

  8. springMVC找不到JS等文件

     应用springMVC时 JS等文件找不到错误 应用springMVC时如果配置URL映射时如下配置 <servlet>        <servlet-name>appSe ...

  9. Java SpringMVC实现国际化整合案例分析(i18n)

    所谓国际化就是支持多种语言,web应用在不同的浏览环境中可以显示出不同的语言,比如说汉语.英语等.下面我将以具体的实例来举例说明: (1)新建动态Javaweb项目,并导入几个SpringMVC必需的 ...

  10. code manager tools git的使用;

    git的使用 一.下载及安装: 1.下载:https://github.com 2.安装: 二.常用命令: 查看.添加.提交.删除.找回,重置修改文件 git help< command> ...