ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-09-11 20:36:59  阅读:14  来源: 互联网

标签:end String start char 字符串 LeeCode arrs 元音



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;
		
	}
}



标签:end,String,start,char,字符串,LeeCode,arrs,元音

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有