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 ...
随机推荐
- 2018年11月16日 我和SB交流有代沟-继续字符串4
test="abcdeffedcba" v=test.lstrip("bcabc")#寻找的是最多匹配然后移除指定字符串 print("1.lstri ...
- 对象new和不new的理解
1.现象 在一个线程类[QObject]中声明一个对象QTimer,[不new,直接声明],在槽函数中timer.start() 报警告:不能跨线程调用对象 2.分析 不使用new的方式,直接A a; ...
- FireMonkey 源码学习(2)
三.TControl FireMonkey重写了TControl的代码,实现了众多接口,如下图: 基类上实现了众多功能,这里不详细描述. 四.TEdit 编辑框是从TControl—TStyledCo ...
- Bootstrap3基础 btn-xs/sm... 按钮的四种大小
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- 修改button样式小例子
.toolbar button{ background: none; border:none; padding:0 3px;} <div class="toolbar toolbar- ...
- Linux驱动开发调试 -- 打开dev_dbg()【转】
本文转载自:https://blog.csdn.net/kunkliu/article/details/78048618 转载地址:http://blog.chinaunix.net/uid-2284 ...
- 如何评价 React 实现的前端 UI 库 material-ui?
作者:知乎用户链接:https://www.zhihu.com/question/51040975/answer/208582603来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 外观模式Facade pattern
http://www.runoob.com/design-pattern/facade-pattern.html 外观模式 外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一 ...
- Rime输入法的配色方案
致青春 so_young: name: "致青春/So Young" author: "五磅兔 zcunlin@foxmail.com" text_color: ...
- LOJ 534 花团(线段树+dfs栈)
题意 https://loj.ac/problem/534 思路 又是复杂度错误的一题,\(O(n^2\log n)\) 能过 \(15000\) . 虽然看起来强制在线,其实是一道假的在线题.首先按 ...