问题描述:

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

示例 1:

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

示例 2:

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

说明:
元音字母不包含字母"y"。

方法:   用一个list纪录元音字母的索引 index = [],对里面的value进行swap.

 class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
pattern = "aeiouAEIOU"
index = []
strToList = list(s)
for i in range(len(s)):
if strToList[i] in pattern:
index.append(i)
j = len(index)
for i in range(j // 2):
strToList[index[i]],strToList[index[j - i - 1]] = strToList[index[j - i - 1]],strToList[index[i]]#交换
s = ""
for i in strToList:
s += i
return s

官方:

 class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
s = list(s)
l = len(s) yun = set(('a', 'e', 'i', 'o', 'u','A', 'E', 'I', 'O', 'U')) i = 0
j = l-1 while i < j:
while i < j:
if s[i] in yun:
break
i += 1 while i < j:
if s[j] in yun:
break
j -= 1 s[i], s[j] = s[j], s[i]
i += 1
j -= 1 return "".join(s)

2018-09-26 14:46:24

LeetCode--345--反转字符串中的元音字母的更多相关文章

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

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

  2. Leetcode 345. 反转字符串中的元音字母 By Python

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. [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. CentOS6.8下安装Redis

    1.由于Redis是使用C语言开发的,安装时需要对Redis的源码进行编译,编译依赖gcc环境,如果没有gcc,需要先安装gcc: yum install gcc-c++ 2.安装完成后,进入Redi ...

  2. uniGUI试用笔记(六)

    uniGUI提供了一个文件上传控件TUniFileUpload,进行数据的导入就变得比较容易.首先将TUniFileUpload控件放置在窗体上,按下导入按钮后,执行TUniFileUpload的文件 ...

  3. Delphi程序的主题(Theme)设置

    本文参考了 http://superlevin.ifengyuan.tw/delphi-change-vcl-style/ 在项目的工程文件中勾选主题,设置缺省主题为Windows 部分代码如下: u ...

  4. linux内核中的IPIs是什么?

    答: 处理器间中断(Interprocessor Interrupts)

  5. 再谈 tp的 实例化 类 的自动加载

    表示一个域名下的所有/任何主机 使用 的格式是: [*.] example.com 其中 , example.com叫着 裸域名. (这个example.com/net/org不能被注册, 被保留) ...

  6. Module controller in JMeter

    https://qualibrate.com/blog/quality-assurance/jmeter-module-controller/ 通过组合Test Fragments 和Module C ...

  7. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)

    题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...

  8. (转载)WinCC 卸载后 Simatic Shell 的删除

    现象:WinCC卸载后,在计算机(我的电脑)中仍有Simatic Shell文件夹,双击无反应 解决:1.按Win+X,运行“regedit”,打开注册表2.在注册表中,选中HKEY_LOCAL_MA ...

  9. 使用MapperScannerConfigurer简化MyBatis配置

    MyBatis的一大亮点就是可以不用DAO的实现类.如果没有实现类,Spring如何为Service注入DAO的实例呢?MyBatis-Spring提供了一个MapperFactoryBean,可以将 ...

  10. Latex: 保持参考文献大小写

    参考: BibTeX loses capitals when creating .bbl file Latex: 保持参考文献大小写 在排版时,BibTeX会根据参考文献的格式将除了title中的第一 ...