首页 > 其他 > 详细

Reverse Vowels of a String

时间:2016-05-08 22:26:49      阅读:346      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!