[LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Input: "hello"
Output: "holle"
Example 2:
Input: "leetcode"
Output: "leotcede"
Note:
The vowels does not include the letter "y".
思路用一个indexs来存是vowels的index, 然后分别对调index的l, r, 最后返回字符串. Note: array可以modify, 但是string不能, 所以要将string变为array, 最后再转换为string.
Code
class Solution:
def reverseVowels(self, s):
indexs, s, vowels, length = [], list(s), "euioaEUIOA", len(s) for i in range(length):
if s[i] in vowels:
indexs.append(i)
l2 = len(indexs)
for j in range(l2//2):
l, r = indexs[j], index[l2-j-1]
s[l], s[r] = s[r], s[l]
return "".join(s)
[LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers的更多相关文章
- LeetCode 345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels(元音字母) of a string. Example ...
- Python [Leetcode 345]Reverse Vowels of a String
题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- Leetcode 345 Reverse Vowels of a String 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...
- LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- Leetcode 345 Reverse Vowels in a String
两个for 第一个for将每一个元音依次存放进一个char数组 第二个for,每检测到元音,就从char数尾部开始,依次赋值 如何检测元音呢?当然写一个冗长的if(),不过我们有更好的选择 hashs ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- 345. Reverse Vowels of a String【easy】
345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...
随机推荐
- 【python3】 django2.0 加载css 、js 、img 等静态文件
python : 3.6.4 django : 2.0 1 文件目录 /var/www/mysite 2 nginx配置: server { listen ; #暴露给外部访问的端口 server_ ...
- css3整理--gradient
gradient语法: -moz-linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, & ...
- win7 64位系统下读写access数据库以及安装了office32位软件再安装64位odbc的方法
公司一款软件还在读写access数据库. 问题是我的电脑是win7 64位, 运行程序会报错, 出错信息很明显, 大意是ODBC数据源读写出错. 因此,我需要下载Access ODBC 64位数据源 ...
- EXCEL通俗易懂讲公式(一):sumif,sumifs,countif,countifs
最近公司招了一批新人,excel基本都是小白阶段,以前用过的也就是画个课程表,没做过什么数据统计和文本计算等工作.因此各种问题都来了,什么vlookup,offset,连条件求和的sumif也不会用, ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- qcow2、raw、vmdk等镜像格式的比较和基本转换
注:本文转自http://www.cnblogs.com/feisky/archive/2012/07/03/2575167.html 云计算用一个朋友的话来说:”做云计算最苦逼的就是得时时刻刻为 ...
- CmD空格转义的三种方法,总有一种会解决问题
CmD空格转义 在cmd中,如果路径中存在空格报错 可以有三种解决方法: 1.将存在空格的路径用双引号包起来,如:"D:/Program Files/xx"; 2.将存在空格的名称 ...
- C++/C, Java学习资料
C++11系列-什么是C++11 [Java]Final 与 C++ Const的区别 C++开发者都应该使用的10个C++11特性 史上最明白的 NULL.0.nullptr 区别分析 C语言堆栈入 ...
- C++和Java中枚举enum的用法
在C++和java中都有枚举enum这个关键字,但是它们之间又不太一样.对于C++来说,枚举是一系列命名了的整型常量,而且从枚举值转化为对应的整型值是在内部进行的.而对于Java来说,枚举更像一个类的 ...
- Centos7 安装hive
安装hive 配置hive 在hdfs中新建目录/user/hive/warehouse 首先启动hadoop任务 hdfs dfs -mkdir /tmp hdfs dfs -mkdir /user ...