码迷,mamicode.com
首页 > 其他好文 > 详细

Reverse Vowels of a String

时间:2016-05-08 22:28:17      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

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

注意: 1.大小写 2. vowels 指的是元音字母 ‘a‘ ,‘e‘, ‘i‘,‘o‘,‘u‘ 3.题目第意思是two point switch.

 1 public class Solution {
 2     public String reverseVowels(String s) {
 3         if (s == null) {
 4             return null;
 5         }
 6         char[] ch = s.toCharArray();
 7         int left = 0;
 8         int right = ch.length - 1;
 9         while (left < right) {
10             while (left < right && !isVowel(ch[left])) {
11                 ++left;
12             }
13             while (left < right && !isVowel(ch[right])) {
14                 --right;
15             }
16             if (left < right) {
17                 char temp = ch[left];
18                 ch[left] = ch[right];
19                 ch[right] = temp;
20                 ++left;
21                 --right;
22             }
23         }
24         return new String(ch);
25     }
26     
27     private boolean isVowel(char c) {
28         c = Character.toLowerCase(c);
29         if (c == ‘a‘ || c == ‘e‘ || c == ‘i‘ || c == ‘o‘ || c == ‘u‘) {
30             return true;
31         }
32         return false;
33     }
34 }

 

Reverse Vowels of a String

标签:

原文地址:http://www.cnblogs.com/FLAGyuri/p/5471816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!