[leetcode]: 345. Reverse Vowels of a String
来源:互联网 发布:python使用turtle 编辑:程序博客网 时间:2024/06/14 14:42
1.题目
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”.
给一个字符串,翻转其中的元音字母
2.分析
两个指针分别从头部和尾部开始往中间遍历。遇到元音字母则swap。
3.代码
class Solution {public: bool isVowel(char c) { return c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U' || c == 'u'; } string reverseVowels(string s) { int i = 0; int j = s.size() - 1; while (i < j) { if (isVowel(s[i]) && isVowel(s[j])) { swap(s[i], s[j]); ++i; --j; } else if (!isVowel(s[i])) ++i; else if (!isVowel(s[j])) --j; } return s; }};
阅读全文
0 0
- 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 *** 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
- 345.[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】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
- 非常可乐 HDU
- 并查集与欧拉回路
- On the way
- T-SQL 编程小结 (SQL Server)
- [TensorFlow 学习笔记-06]激活函数(Activation Function)
- [leetcode]: 345. Reverse Vowels of a String
- javaeeeclips快速开发 快捷键
- 虚拟机CentOS 7 网络连接显示"线缆被拔出"
- python--序列
- 对List中的 map 排序
- 贪心求解TSP问题
- 高内聚低耦合
- 如何构建一个反电信网络诈骗基础模型
- BZOJ 2216: [Poi2011]Lightning Conductor 决策单调性DP