LeetCode_345. Reverse Vowels of a String

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

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

时间: 2024-10-11 05:44:51

LeetCode_345. Reverse Vowels of a String的相关文章

345. Reverse Vowels of a String(C++)

345. Reverse Vowels of a String 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"

[LeetCode][JavaScript][Python]Reverse Vowels of a String

Reverse Vowels of a String 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". 将字符串中

345. Reverse Vowels of a String【easy】

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:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede&q

LeetCode 345. Reverse Vowels of a String

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". Note:The vowels does not include

Reverse Vowels of a String

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". 注意: 1.大小写 2. vowels 指的是元音字母 'a'

#345 Reverse Vowels of a String

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". 题意:将一个字符串中a,e,i,o,u收尾互换位置,返回. cl

【LeetCode】345. Reverse Vowels of a String 解题小结

题目: 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". 应该算不上有难度. class Solution { p

345. 反转字符串中元音字母的位置 Reverse Vowels of a String

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" 题意:反转字符串中元音字母的位置 方法1:用栈保存元音字符串,时间

Leetcode题目:Reverse Vowels of a String

题目: 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". 题目解答: 要求将字符串中所有的元音字母逆转,辅音字