[leetcode] 345. Reverse Vowels of a String 解题报告
来源:互联网 发布:亚马逊广告优化 编辑:程序博客网 时间:2024/05/16 12:17
题目链接: https://leetcode.com/problems/reverse-vowels-of-a-string/
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".
思路: 用一个hash表存一下元音字符, 然后设置左右指针找到两个都是元音的位置交换一下就可以了. 注意字符是大小写都有的.
代码如下:
class Solution {public: string reverseVowels(string s) { if(s.length()==0) return ""; int left =0, right = s.length()-1; set<char> hash{'a','e','i','o','u', 'A', 'E', 'I', 'O', 'U'}; while(left < right) { if(hash.count(s[left]) == 0) left++; if(hash.count(s[right]) == 0) right--; if(hash.count(s[left])!=0 && hash.count(s[right])!=0) { swap(s[left], s[right]); left++, right--; } } 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】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
- Leetcode 345. Reverse Vowels of a String
- LeetCode 345. Reverse Vowels of a String
- 345.[LeetCode]Reverse Vowels of a String
- PAT-B 1024. 科学计数法
- java基础 -- 六哥
- 127.0.0.1和localhost和本机IP三者的区别!!!
- Qt实战经验
- 算法的时间复杂度的计算
- [leetcode] 345. Reverse Vowels of a String 解题报告
- Django 文档自翻自用中文版(2.1-2.1.3)
- Hibernate之deleted object would be re-saved by cascade 异常的解决
- [leetcode] 344. Reverse String 解题报告
- 三、SpringBoot JPA 配置Hibernate
- Intellij IdeaI快捷键
- Ruby入门之一(Ruby简介)
- Leetcode 刷题:Q58 Length of last word
- 关于 Android 异步消息处理机制 Handler