[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 ...
随机推荐
- java高级---->Thread之单例模式的使用
这里我们介绍一下在多线程中如何安全正确的编写单例模式的代码.不知为何,恰如其分的话总是姗姗来迟,错过最恰当的时机. 多线程中的单例模式 这里面通过代码来体会一下在多线程中如何正确的编写单例模式的代码. ...
- MFC 三种消息
在MFC应用程序中传输的消息有三种类型:窗口消息.命令消息和控件通知. (1)窗口消息:WM_XXX 窗口消息(Window Message)一般与窗口的内部运作有关,如:创建窗口.绘制窗口和销毁窗口 ...
- C#串口介绍以及简单串口通信程序设计实现
C#串口介绍以及简单串口通信程序设计实现 周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题 串口介绍 串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口) ...
- mysql查询语句集
1. mysql 查询出某字段的值不为空的语句 1.不为空 select * from table where id <> ""; select * from tabl ...
- HDU 2199 Can you solve this equation(二分答案)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 游戏AI-行为树
参考: 游戏AI—行为树研究及实现 GAD腾讯游戏开发者平台:游戏中的人工智能AI 腾讯开源项目behaviac 占坑,待编辑
- 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT
[CF896D]Nephren Runs a Cinema 题意:一个序列中有n格数,每个数可能是0,1,-1,如果一个序列的所有前缀和都>=0且总和$\in [L,R]$,那么我们称这个序列是 ...
- 开发工具-网络封包分析工具Charles
extends:http://blog.devtang.com/blog/2013/12/11/network-tool-charles-intr/ 简介 本文为InfoQ中文站特供稿件,首发地址为: ...
- HTML display:inline-block
元素转换 display:block; 行内转块 Display:inline; 块转行内 Display:inline-block; 块或行内转行内块 链接伪类 a:link{属性:值;} ...
- oracle如何删除表空间
drop tablespace 表空间名 including contents and datafiles cascade constraint; ............. 以system用户登录, ...