转载请注明出处: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 解题报告相关推荐

  1. 【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 ...

  2. 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 ...

  3. 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 ...

  4. 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', ...

  5. Leetcode 345 Reverse Vowels of a String 字符串处理

    题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. 1 class Solution { 2 public: 3 bool i ...

  6. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  7. 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 ...

  8. 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', ' ...

  9. 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 ...

最新文章

  1. postDelayed总结
  2. swift. 扩展类添加属性_Swift中用到extension的一些基本的扩展功能讲解
  3. 七天开发安卓软件(五)
  4. Oracle之事务和锁
  5. gRPC in ASP.NET Core 3.x -- Protocol Buffer(3)更新消息类型
  6. java格式化到毫秒_java时间格式化到毫秒
  7. 【读书笔记】JavaScript高级编程(一)
  8. RocketMQ之消费者顺序消费源码解析
  9. Python学习记录(小甲鱼C的课程)
  10. signature=e7411d00c74ac359f20083a015dc6ea9,MS12-020:遠端桌面的弱點可能會允許遠端程式碼執行:2012 年 3 月 13 日...
  11. 推荐个可以和《魔戒》争雄的美剧:《权力的游戏》。
  12. windows10 安装php-cgi.exe为服务
  13. Mysql 面向企业级开发(一)
  14. Android8.0 NotificationChannel修改铃声和振动的坑
  15. wordcloud词云可视化
  16. Var,Let和Const –有什么区别?
  17. 使用 ESP32 + Python 实现在线人员入侵检测
  18. 43-为什么要训练数据集与测试数据集
  19. python做的RPG小游戏(面向对象思想)
  20. 京津冀工业机器人及其自动化系统应用与发展调研报告

热门文章

  1. h5怎么区分在ios、安卓、微信环境下?怎么调用原生函数
  2. JavaScript整合版——后端程序员学js,只读一文直接零基础入门,大学生期末考试不挂科宝典。
  3. python爬虫抓图_Python 爬虫网页抓图保存
  4. Large-scale cluster management at Google with Borg
  5. Win10系统图片打开方式恢复默认照片查看器
  6. 第03章 Tableau基础操作
  7. 在vue中使用Echarts的3D柱状图
  8. 为什么梦里常有视觉和听觉,却没有痛觉和嗅觉?
  9. 木材材积表快速计算器_木材材积表计算器
  10. Styled-Components