LeetCode--345--反转字符串中的元音字母
问题描述:
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 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--反转字符串中的元音字母的更多相关文章
- Java实现 LeetCode 345 反转字符串中的元音字母
345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...
- Leetcode 345. 反转字符串中的元音字母 By Python
编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...
- LeetCode:反转字符串中的元音字母【345】
LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...
- 【leetcode 简单】 第八十三题 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- [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 ...
- leetCode题解之反转字符串中的元音字母
1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...
- Python实现 "反转字符串中的元音字母" 的方法
#coding=utf- def reverseVowels(s): """ :type s: str :rtype: str """ sS ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- [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 ...
随机推荐
- python---01.各类计算机语言,python历史,变量,常量,数据类型,if条件
一.认识计算机 1.硬件组成: CPU(大脑) + 内存(缓冲) + 主板(连接各部分) + 电源(心脏) + 显示器 + 键盘 +鼠标+ 显卡 + 硬盘 2.操作系统 ①windows ...
- topcoder srm 683 div1
problem1 link 肯定存在相邻两堆满足不会存在任何操作在这两堆之间进行.然后就成为一条链,那么只需要维护链的前缀和即可判断当前堆和前一堆之间需要多少次操作. problem2 link 对于 ...
- Spring Boot源码分析
1.核心: SpringApplication.run(SpringbootdemoApplication.class, args); 内部 2.初始化: new SpringApplication( ...
- 【问题解决:时区】连接MySQL时错误The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
问题描述: MySQL升级到8.0.11之后连接数据库报错: Your login attempt was not successful, try again. Reason: Could not g ...
- Nuget CsvHelper 的使用
CsvHelper:nuget地址 csv导出类||生成类 public class CSVHeader { public string head1 { get; set; } public stri ...
- 常用markdown语法入门
入门markdown常用基本语法,简单到让你怀疑人生~~ 不说废话,直接上图(如果图片显示不清晰,建议选中图片右键——在新标签页中打开图片,妥妥的呢!!) (左侧黑色背景为markdown语法,右侧为 ...
- git代码回滚的两种选择
回滚到指定commit,且保持该commit之前和之后的提交记录 使用git revert命令. git revert HEAD // 回滚到前一次 commit git revert HEAD^ / ...
- 动态LINQ(Lambda表达式)
1.准备数据实体 public class Data { public string AccountNO { get; set; } public int Count { get; set; } } ...
- 前端阶段_div以及css介绍
1.div div是html的一个标签,是块级元素,单独使用没有意义,必须结合css来使用,进行网页布局 2.span span是一个html标签,是一个内联元素,主要对括起来的内容进行修饰 3.&l ...
- Java代码优化,都有哪些常用方法?
Java代码优化是Java编程开发很重要的一个步骤,Java代码优化要注重细节优化,一个两个的细节的优化,产生的效果不大,但是如果处处都能注意代码优化,对代码减少体积.提高代码运行效率是有巨大帮助的, ...