345. Reverse Vowels of a String

Posted 烁宝宝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 345. Reverse Vowels of a String

LeetCode #345 Reverse Vowels of a String

[LeetCode]345 Reverse Vowels of a String

345. Reverse Vowels of a Stringeasy

LeetCode 345. Reverse Vowels of a String

345. Reverse Vowels of a String