LeeCode_反转字符串中的元音字符

Given s = “leetcode”, return “leotcede”.

  • 思路:使用双指针指向待反转的两个元音字符,一个指针从头向尾遍历,一个指针从尾到头遍历。
/**
 * 3. 反转字符串中的元音字符
 * aeiou
 * Given s = "leetcode", return "leotcede".
 * @author 林博弈
 *
 */
public class Three_元音字符 {
	public static void main(String[] args) {
		String string = "leetcode";
		char[] arr = reverseVowels(string);
		for(char c:arr)System.out.print(c+",");
	}
	public static char[] reverseVowels(String s){
		char[] vowels = {'a','e','i','o','u','A','E','I','O','U'};
		char[] arrs = s.toCharArray();
		int start = 0;
		int end = arrs.length-1;
		
		while(start<end){
			while(start<arrs.length-1){
				for(char c:vowels){
					if(arrs[start]==c){
						break;
					}
					start++;
				}
			}
			while(end>0){
				for(char c:vowels){
					if(arrs[end]==c){
						break;
					}
					end--;
				}
			}
			char temp = arrs[start];
			arrs[start] = arrs[end];
			arrs[end] = temp;
			start++;
			end--;
		}
		
		return arrs;
		
	}
}

上一篇:Leecode 35 ----- 搜索插入位置


下一篇:你需要的LeeCode题No.08——“盛最多水的容器”_一点课堂(多岸学院)