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 ...
随机推荐
- libcurl 静态库编译
转载:http://www.cnblogs.com/jkcx/p/6406706.html 1.下载最新版的libcurl(官网:http://curl.haxx.se/download.html), ...
- Django创建超级用户出现错误
如果运行python manage.py createsuperuser出现一大堆错误代码 解决方案: 1.检查settings.py中的DATABASE配置确定正确性 2.执行python mana ...
- SpringBoot 利用过滤器Filter修改请求url地址
要求: 代码中配置的url路径为http://127.0.0.1/api/associates/queryAssociatesInfo 现在要求http://127.0.0.1/associates/ ...
- MySQL 安装步骤
今天用了一下MySQL,刚好看到之前电保存脑的笔记,于是整理了一下,还是记在博客上方便查询. 1.官网下载https://dev.mysql.com/downloads/mysql/之前安装的是mys ...
- 题解——loj6278 数列分块入门2 (分块)
查询小于k的值 注意lower_bound一定要减去查找的起始位置得到正确的位置 调了快两天 淦 #include <cstdio> #include <algorithm> ...
- B树,B+树比较
首先注意:B树就是B-树,"-"是个连字符号,不是减号.也就是B-树其实就是B树 B-树是一种平衡的多路查找(又称排序)树,在文件系统中有所应用.主要用作文件的索引.其中的B就表示 ...
- PTA 根据后序和中序遍历输出先序遍历(25 分)
7-1 根据后序和中序遍历输出先序遍历(25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...
- python学习打卡 day12 生成器
本节主要内容 : 生成器 生成器函数 各种推导式 生成器表达式 一.生成器 什么是生成器.生成器的本质就是迭代器. 在python中有三种方式来获取生成器: 1.通过生成器函数 2.通过各种推导式来实 ...
- 快排+java实现
import java.util.Arrays; public class QuickSort { //三数取中法.取出不大不小的那个位置 public static int getPivotPos( ...
- SQL Server DATEADD() 函数及实际项目应用注意事项
1. DATEADD() 函数的解释和语法分析 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法: DATEADD(datepart,number,date) date 参数是合法的日 ...