【LeetCode】345. Reverse Vowels of a String 解题报告
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51429823
Subject
出处: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”.
Explain
该题目的意思是反转一个字符串中的元音字母。第一个和最后一个元音字母交换,第二个和倒数第二个交换……
My Solution
首先定义一个方法去判断一个char字符是否是元音字母。然后通过首尾两个”指针”,当前后两个字符都是元音字符的时候才进行交换,否则将不是元音字符的指针向前或者向后移动。
跳出条件就是start < end。
/*** * @param c* @return*/public static boolean checkVowel(char c) {if ('a' == c || 'e' == c || 'i' == c || 'o' == c || 'u' == c|| 'A' == c || 'E' == c || 'I' == c || 'O' == c || 'U' == c) {return true;}return false;}/*** * @param s* @return*/public static String reverseVowels(String s) {char[] ch = s.toCharArray();int start = 0;int end = ch.length - 1;char temp;while (start < end) {if (checkVowel(ch[start]) && checkVowel(ch[end])) {temp = ch[end];ch[end] = ch[start];ch[start] = temp;start++;end--;} else {if (!checkVowel(ch[start])) {start++;}if (!checkVowel(ch[end])) {end--;}}}return new String(ch);}
so easy~
【LeetCode】345. Reverse Vowels of a String 解题报告相关推荐
- 【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 1 ...
- 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
题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...
- Leetcode 345: Reverse Vowels of a String
问题描述: Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', ...
- Leetcode 345 Reverse Vowels of a String 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. 1 class Solution { 2 public: 3 bool i ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- LeetCode:345. Reverse Vowels of a String
051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...
- 345. Reverse Vowels of a String
题目: Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', ' ...
- 345. Reverse Vowels of a String(python+cpp)
题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...
最新文章
- postDelayed总结
- swift. 扩展类添加属性_Swift中用到extension的一些基本的扩展功能讲解
- 七天开发安卓软件(五)
- Oracle之事务和锁
- gRPC in ASP.NET Core 3.x -- Protocol Buffer(3)更新消息类型
- java格式化到毫秒_java时间格式化到毫秒
- 【读书笔记】JavaScript高级编程(一)
- RocketMQ之消费者顺序消费源码解析
- Python学习记录(小甲鱼C的课程)
- signature=e7411d00c74ac359f20083a015dc6ea9,MS12-020:遠端桌面的弱點可能會允許遠端程式碼執行:2012 年 3 月 13 日...
- 推荐个可以和《魔戒》争雄的美剧:《权力的游戏》。
- windows10 安装php-cgi.exe为服务
- Mysql 面向企业级开发(一)
- Android8.0 NotificationChannel修改铃声和振动的坑
- wordcloud词云可视化
- Var,Let和Const –有什么区别?
- 使用 ESP32 + Python 实现在线人员入侵检测
- 43-为什么要训练数据集与测试数据集
- python做的RPG小游戏(面向对象思想)
- 京津冀工业机器人及其自动化系统应用与发展调研报告