345. Reverse Vowels of a String Java Solutions

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

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public String reverseVowels(String s) {
 3         if(null == s) return s;
 4         char[] tmp = s.toCharArray();
 5         Stack<Character> stack = new Stack<Character>();
 6         for(int i = 0; i<tmp.length; i++){
 7             if(isVowels(tmp[i])){
 8                      stack.push(tmp[i]);
 9                 }
10         }
11         for(int i = 0; i<tmp.length; i++){
12             if(isVowels(tmp[i])){
13                     tmp[i] = stack.pop();
14                 }
15         }
16         String res = new String(tmp);
17         return res;
18     }
19
20     public boolean isVowels(char c){
21         if(c == ‘a‘ || c == ‘e‘ || c == ‘i‘ || c == ‘o‘ || c == ‘u‘ || c == ‘A‘ || c == ‘E‘ || c == ‘I‘ || c == ‘O‘ || c == ‘U‘ ){
22                     return true;
23         }
24         return false;
25     }
时间: 2024-08-04 03:40:08

345. Reverse Vowels of a String Java Solutions的相关文章

345. Reverse Vowels of a String(C++)

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"

345. Reverse Vowels of a String【easy】

345. Reverse Vowels of a String[easy] 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&q

LeetCode 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". Note:The vowels does not include

#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". 题意:将一个字符串中a,e,i,o,u收尾互换位置,返回. cl

【LeetCode】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". 应该算不上有难度. class Solution { p

&lt;LeetCode OJ&gt; 345. Reverse Vowels of a String

Total Accepted: 537 Total Submissions: 1488 Difficulty: Easy 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"

[LC] 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: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" class Solution { public String reverse

LeetCode_345. Reverse Vowels of a String

345. Reverse Vowels of a String Easy Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" N

Reverse Words in a String (JAVA)

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 1 public class Solution { 2 public String reverseWords(String s) { 3 if(s.equals(""))return s; 4 String