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

注意: 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 }

 

转载于:https://www.cnblogs.com/FLAGyuri/p/5471816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值