码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode_345. Reverse Vowels of a String

时间:2019-11-11 09:47:42      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:leo   ack   leetcode   ++   final   i++   har   eve   example   

 

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"

Note:
The vowels does not include the letter "y".

 

package leetcode.easy;

public class ReverseVowelsOfAString {
	private static final boolean[] isVowel;

	static {
		isVowel = new boolean[123]; // ‘z‘ is 122
		isVowel[‘a‘] = true;
		isVowel[‘e‘] = true;
		isVowel[‘i‘] = true;
		isVowel[‘o‘] = true;
		isVowel[‘u‘] = true;

		isVowel[‘A‘] = true;
		isVowel[‘E‘] = true;
		isVowel[‘I‘] = true;
		isVowel[‘O‘] = true;
		isVowel[‘U‘] = true;
	}

	public String reverseVowels(String s) {
		char[] ch = s.toCharArray();
		int i = 0;
		int j = s.length() - 1;
		while (i < j) {
			if (!isVowel[ch[i]]) {
				i++;
			} else if (!isVowel[ch[j]]) {
				j--;
			} else {
				char c = ch[i];
				ch[i] = ch[j];
				ch[j] = c;
				i++;
				j--;
			}
		}
		return new String(ch);
	}

	@org.junit.Test
	public void test() {
		System.out.println(reverseVowels("hello"));
		System.out.println(reverseVowels("leetcode"));
	}
}

 

LeetCode_345. Reverse Vowels of a String

标签:leo   ack   leetcode   ++   final   i++   har   eve   example   

原文地址:https://www.cnblogs.com/denggelin/p/11832588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!