Files
LeetCode-Go/website/content/ChapterFour/0345.Reverse-Vowels-of-a-String.md
2020-08-09 00:39:24 +08:00

1.1 KiB

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:


Input: "hello"
Output: "holle"

Example 2:


Input: "leetcode"
Output: "leotcede"

题目大意

题目要求我们反转字符串中的元音字母。需要注意字母大小写。

解题思路

这一题的解题思路是用 2 个指针,指针对撞的思路,来不断交换首尾元素,即可。这一题和第 344 题思路一样。

代码


package leetcode

func reverseVowels(s string) string {
	b := []byte(s)
	for i, j := 0, len(b)-1; i < j; {
		if isVowels(b[i]) && isVowels(b[j]) {
			b[i], b[j] = b[j], b[i]
			i++
			j--
		} else if isVowels(b[i]) && !isVowels(b[j]) {
			j--
		} else if !isVowels(b[i]) && isVowels(b[j]) {
			i++
		} else {
			i++
			j--
		}
	}
	return string(b)
}

func isVowels(s byte) bool {
	if s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u' || s == 'A' || s == 'E' || s == 'I' || s == 'O' || s == 'U' {
		return true
	}
	return false
}