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

345. Reverse Vowels of a String

时间:2016-05-25 20:33:17      阅读:126      评论: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 public class Solution {
 2     public String reverseVowels(String s) {
 3         ArrayList<Character> list=new ArrayList<>();
 4         char[] ss=s.toCharArray();
 5         list.add(‘a‘);
 6         list.add(‘e‘);
 7         list.add(‘i‘);
 8         list.add(‘o‘);
 9         list.add(‘u‘);
10         list.add(‘A‘);
11         list.add(‘E‘);
12         list.add(‘I‘);
13         list.add(‘O‘);
14         list.add(‘U‘);
15         int i=0,j=s.length()-1;
16         while(i<j)
17         {
18             while(!list.contains(ss[i])&&i<j)
19             i++;
20             while(!list.contains(ss[j])&&i<j)
21             j--;
22             
23             char c=ss[i];
24             ss[i]=ss[j];
25             ss[j]=c;
26             
27             i++;j--;
28         }
29         s=String.valueOf(ss);
30         return s;
31     }
32 }

 

345. Reverse Vowels of a String

标签:

原文地址:http://www.cnblogs.com/ghuosaao/p/5528094.html

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