zoukankan      html  css  js  c++  java
  • 345. 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

    示例 1:

    输入: "hello"
    输出: "holle"
    

    示例 2:

    输入: "leetcode"
    输出: "leotcede"

    说明:
    元音字母不包含字母"y"。

    package p345;
    
    public class Main {
    
    	public static void main(String[] args) {
    		String s = "aA";
    		System.out.println(reverseVowels(s));
    	}
    	
    	//翻转元音字母
    	public static String reverseVowels(String s) {
            //定义元音字母
            char[]c = {'a','e','i','o','u','A','E','I','O','U'};
            char[] arr = s.toCharArray();
            int i=0;
            int j = arr.length-1;
            //循环遍历遇见元音字母停止交换
            
            while (i<j) {
            	char yuan_start = arr[i];
            	if (!isYuan(yuan_start, c)) {
            		i++;
            		continue;
            	}
            	char yuna_end = arr[j];
            	if (!isYuan(yuna_end, c)) {
            		j--;
            		continue;
            	}
            	if (isYuan(yuan_start, c) && isYuan(yuna_end, c)) {
            		change(i, j, arr);
            		i++;
            		j--;
            	}
            	
            	
            }
            return new String(arr);
        }
    	//判断字符是不是原因字母
    	public static boolean isYuan(char yuan,char[]c){
    		boolean result = false;
    		for (int i=0;i<c.length;i++) {
    			char temp = c[i];
    			if (yuan == temp) {
    				result = true;
    			}
    		}
    		return result;
    	}
    	
    	
    	
    	//交换函数
    	public static void change(int i,int j,char c[]){
    		char temp = c[i];
    		c[i] = c[j];
    		c[j] = temp;
    	}
    	
    	
    	
    	
    }
    

      

  • 相关阅读:
    Best Time to Buy and Sell Stock II
    Subsets II
    Subsets I
    Combinations
    Permutation Sequence
    Next Permutation
    Anagrams
    Combination-Sum II
    Combination-Sum I
    Permutations II
  • 原文地址:https://www.cnblogs.com/airycode/p/10454784.html
Copyright © 2011-2022 走看看