leetcode笔记:Reverse Vowels of a String
来源:互联网 发布:php带采集小说系统 编辑:程序博客网 时间:2024/04/28 20:47
一. 题目描述
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello"
, return "holle"
.
Example 2:
Given s = "leetcode"
, return "leotcede"
.
二. 题目分析
题目大意是,编写函数输入一个字符串,将其中的元音字母反转,其他字母位置不变。
这里只实现了一种方法,比较无脑。使用left
和right
记录遍历字符串的左右下标,如果不是原因字母,直接跳过。当且仅当left < right
且当且left
和right
下标所指元素均为元音时才翻转顺序。
判断当前字符是否为原因时,使用了一个模式字符串temp
,如果在模式字符串中没找到,则返回:string::npos
。
三. 示例代码
class Solution {public: string reverseVowels(string s) { int left = 0, right = s.size() - 1; string temp = "aeiouAEIOU"; while (left < right) { while (left < right && temp.find(s[left]) == string::npos) ++left; while (left < right && temp.find(s[right]) == string::npos) --right; if(left < right) swap(s[left++], s[right--]); } return s; }};
四. 小结
有更好的方法,欢迎交流。
1 0
- leetcode笔记:Reverse Vowels of a String
- LeetCode笔记:345. Reverse Vowels of a String
- Leetcode Reverse Vowels of a String 345
- LeetCode 345. Reverse Vowels of a String
- LeetCode - Reverse Vowels of a String
- leetcode-345. Reverse Vowels of a String
- 【LeetCode-345】Reverse Vowels of a String
- #leetcode#345. Reverse Vowels of a String
- leetcode 345Reverse vowels of a string
- [leetcode] 345. Reverse Vowels of a String
- Leetcode:345. Reverse Vowels of a String
- LeetCode *** 345. Reverse Vowels of a String
- leetcode 345:Reverse Vowels of a String
- LeetCode 345 Reverse Vowels of a String
- LeetCode:Reverse Vowels of a String
- LeetCode.345. Reverse Vowels of a String
- LeetCode 345 Reverse Vowels of a String
- Leetcode - Reverse Vowels of a String
- 研究 Z-Stack 中ZigBee 设备的 IEEE 地址
- 观察者设计模式全解
- Scala--样例类(case)详解
- LeetCode 322. Coin Change
- vim中寄存器(剪切板)使用和vim标记
- leetcode笔记:Reverse Vowels of a String
- [linux]结束进程
- (1)PX4开发指南——引言及入门-linux下px4开发环境搭建
- mmap
- 使用ZwQueryVirtualMemory枚举进程模块支持x64
- 内核枚举进程总结
- geekband android #5 第九周分享(多进程)
- R3 x64枚举进程句柄
- LeetCode 345 Reverse Vowels of a String