编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"
输出: "holle"

示例 2:

输入: "leetcode"
输出: "leotcede"

说明:

元音字母不包含字母"y"。


思路

设立2个指针,一个从索引0开始向右,一个从末尾向前,根据条件进行处理即可

代码

class Solution:
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
yuan = ['a','e','i','o','u','A','E','I','O','U']
s = list(s) l = 0
r = len(s)-1
while l < r:
if s[l] in yuan and s[r] in yuan:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1
elif s[l] in yuan and s[r] not in yuan:
r -= 1
elif s[l] not in yuan and s[r] in yuan:
l += 1
else:
l += 1
r -= 1
return ''.join(s)

Leetcode 345. 反转字符串中的元音字母 By Python的更多相关文章

  1. Java实现 LeetCode 345 反转字符串中的元音字母

    345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...

  2. LeetCode:反转字符串中的元音字母【345】

    LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...

  3. 【leetcode 简单】 第八十三题 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  4. 345 Reverse Vowels of a String 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...

  5. [Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...

  6. leetCode题解之反转字符串中的元音字母

    1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...

  7. Python实现 "反转字符串中的元音字母" 的方法

    #coding=utf- def reverseVowels(s): """ :type s: str :rtype: str """ sS ...

  8. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

  9. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

随机推荐

  1. Django之用户上传文件的参数配置

    Django之用户上传文件的参数配置 models.py文件 class Xxoo(models.Model): title = models.CharField(max_length=128) # ...

  2. centos/ubuntu 双击运行 .sh(shell)文件

    centos 创建桌面双击启动程序(更改图标) - Feythin Lau - 博客园http://www.cnblogs.com/feiyuliu/archive/2012/11/29/279503 ...

  3. Drools 规则引擎

    Drools - Drools - Business Rules Management System (Java™, Open Source) http://drools.org/ [Drools]J ...

  4. Oracle 表空间的创建与管理

    Oracle数据库创建之后有一些默认的表空间随之被创建,查询数据字典 dba_data_files 可以得到数据库当前的所有表空间信息. select * from v$tablespace; sel ...

  5. element-ui 源码解析 二

    Carousel 走马灯源码解析 1. 基本原理:页面切换 页面切换使用的是 transform 2D 转换和 transition 过渡 可以看出是采用内联样式来实现的 举个栗子 <div : ...

  6. DNS_PROBE_FINISHED_NXDOMAIN & MacOS

    DNS_PROBE_FINISHED_NXDOMAIN 内网 DNS bug 8.8.8.8 8.8.4.4 # new inner Wi-Fi 10.1.3.10 10.1.3.13 Windows ...

  7. SpringBoot之文件下载

    package org.springboot.controller; import org.springboot.constant.Constant; import org.springframewo ...

  8. 一、zipkin

    zipkin是Twitter基于google的分布式监控系统Dapper(论文)的开发源实现,zipkin用于跟踪分布式服务之间的应用数据链路(具体就是收集微服务之间的调用情况,然后处理调用之间数据延 ...

  9. jqGrid选中行、格式化、自定义按钮、隐藏

    获取选择一行的id: var id=$('#jqGrid').jqGrid('getGridParam','selrow'); 获取选择多行的id: var ids=$('#jqGrid').jqGr ...

  10. Apache ab 单测 分布式

    使用synchronized 处理并发 缺点:无法做到细粒度控制 只适合单点的情况 使用Redis作为分布式锁 setnx命令 设计模式 :使用 !setnx 加锁 getset命令