zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    hash介绍
    序列化
    面向对象编程
    计算机系统基础知识05
    19、Python之队列
    18、Python之多线程
    17、Python之paramikomo
    16、Python之socket网络编程
    15、Python之异常处理
    14、Python之反射
  • 原文地址:https://www.cnblogs.com/guoguolan/p/5450694.html
Copyright © 2011-2022 走看看