Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ 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 {
if !isVowel(b[i]) {
i++
continue
}
if !isVowel(b[j]) {
j--
continue
}
b[i], b[j] = b[j], b[i]
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
func isVowel(s byte) bool {
return s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u' || s == 'A' ||
s == 'E' || s == 'I' || s == 'O' || s == 'U'
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,30 @@ Output: "leotcede"
## 代码

```go

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 {
if !isVowel(b[i]) {
i++
continue
}
if !isVowel(b[j]) {
j--
continue
}
b[i], b[j] = b[j], b[i]
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
func isVowel(s byte) bool {
return s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u' || s == 'A' ||
s == 'E' || s == 'I' || s == 'O' || s == 'U'
}

```


Expand Down