345. Reverse Vowels of a String

Leetcode 問題: 345. Reverse Vowels of a String

題目

Given a string s, reverse only all the vowels in the string and return it.

The vowels are ‘a’, ’e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases.

前後交換母音字

答案

Golang

// 反轉母音字
func reverseVowels(checkText string) string {
	// 將檢查的文字轉換成 byte (uint8)
	checkTextByte := []byte(checkText)

	// 設定檢查索引
	frontLetterIndex := 0
	backLetterIndex := len(checkTextByte) - 1

	for frontLetterIndex < backLetterIndex {
		// 若前後索引還沒交換,表示還沒檢查完
		if !isVowelLetter(checkTextByte[frontLetterIndex]) {
			// 如果前面的字不是母音字,往後移動到下一個字
			frontLetterIndex++
			continue
		}
		if !isVowelLetter(checkTextByte[backLetterIndex]) {
			// 如果後面的字不是母音字,往後錢動到下一個字
			backLetterIndex--
			continue
		}

		// 找到了兩個母音字,交換彼此字位置
		checkTextByte[frontLetterIndex], checkTextByte[backLetterIndex] = checkTextByte[backLetterIndex], checkTextByte[frontLetterIndex]

		// 繼續移動字的索引
		frontLetterIndex++
		backLetterIndex--
	}

	// 轉換 byte (uint8) 為 string
	finalCheckText := string(checkTextByte)

	return finalCheckText
}

// 檢查是否為母音字
func isVowelLetter(checkLetter byte) bool {
	return checkLetter == 'a' || checkLetter == 'e' || checkLetter == 'i' || checkLetter == 'o' || checkLetter == 'u' || checkLetter == 'A' ||
		checkLetter == 'E' || checkLetter == 'I' || checkLetter == 'O' || checkLetter == 'U'
}

完整程式碼

package main

import (
	"fmt"
)

type Question struct {
	// 參數
	Parameter
	// 答案
	Answer
}

// 參數
type Parameter struct {
	checkText string
}

// 答案
type Answer struct {
	result string
}

func main() {

	QuestionList := []Question{
		{
			Parameter{"hello"},
			Answer{"holle"},
		},

		{
			Parameter{"leetcode"},
			Answer{"leotcede"},
		},

		{
			Parameter{"aA"},
			Answer{"Aa"},
		},
	}

	fmt.Printf("------------------------Leetcode Problem 345------------------------\n")
	for _, question := range QuestionList {
		param := question.Parameter
		fmt.Printf("【input】:%v       【output】:%v\n", param, reverseVowels(param.checkText))
	}
}

// 反轉母音字
func reverseVowels(checkText string) string {
	// 將檢查的文字轉換成 byte (uint8)
	checkTextByte := []byte(checkText)

	// 設定檢查索引
	frontLetterIndex := 0
	backLetterIndex := len(checkTextByte) - 1

	for frontLetterIndex < backLetterIndex {
		// 若前後索引還沒交換,表示還沒檢查完
		if !isVowelLetter(checkTextByte[frontLetterIndex]) {
			// 如果前面的字不是母音字,往後移動到下一個字
			frontLetterIndex++
			continue
		}
		if !isVowelLetter(checkTextByte[backLetterIndex]) {
			// 如果後面的字不是母音字,往後錢動到下一個字
			backLetterIndex--
			continue
		}

		// 找到了兩個母音字,交換彼此字位置
		checkTextByte[frontLetterIndex], checkTextByte[backLetterIndex] = checkTextByte[backLetterIndex], checkTextByte[frontLetterIndex]

		// 繼續移動字的索引
		frontLetterIndex++
		backLetterIndex--
	}

	// 轉換 byte (uint8) 為 string
	finalCheckText := string(checkTextByte)

	return finalCheckText
}

// 檢查是否為母音字
func isVowelLetter(checkLetter byte) bool {
	return checkLetter == 'a' || checkLetter == 'e' || checkLetter == 'i' || checkLetter == 'o' || checkLetter == 'u' || checkLetter == 'A' ||
		checkLetter == 'E' || checkLetter == 'I' || checkLetter == 'O' || checkLetter == 'U'
}

參考資料