Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

345 reverse vowels resolved. #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 3 commits into master from reverse-vowels
Dec 26, 2018
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
3 changes: 2 additions & 1 deletion README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ continually updating 😃.
* [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`*
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go)    *`sliding window`*
* [125. Valid Palindrome](./src/0125_valid_palindrome/valid_palindrome.go)   *`string;`*  *`double index`*
* [344. Reverse String](0344_reverse_string/reverse_string.go)   *`string;`*  *`double index`*
* [344. Reverse String](./src/0344_reverse_string/reverse_string.go)   *`string;`*  *`double index`*
* [345. Reverse Vowels of a String](./src/0345_reverse_vowels_of_a_string/reverse_vowels.go)   *`string;`*  *`double index`*
* [438. Find All Anagrams in a String](./src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)   *`sliding window`*

### Linked List
Expand Down
8 changes: 4 additions & 4 deletions src/0344_reverse_string/reverse_string.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func reverseString(s string) string {
r = len(s) - 1
)

for r >= l {
for r > l {
charL := s[l]
charR := s[r]
s = s[:r] + string(charL) + s[r+1:]
Expand All @@ -33,9 +33,9 @@ func reverseString(s string) string {
// space complexity: O(n)
func reverseString1(s string) string {
sLen := len(s)
runes := []rune(s)
bytes := []byte(s)
for i := 0; i < sLen/2; i++ {
runes[i], runes[sLen-i-1] = runes[sLen-i-1], runes[i]
bytes[i], bytes[sLen-i-1] = bytes[sLen-i-1], bytes[i]
}
return string(runes)
return string(bytes)
}
77 changes: 77 additions & 0 deletions src/0345_reverse_vowels_of_a_string/reverse_vowels.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
345. Reverse Vowels of a String
https://leetcode.com/problems/reverse-vowels-of-a-string/

Write a function that takes a string as input and reverse only the vowels of a string.

*/
// time: 2018年12月26日

package reversevowels

// time complexity: O(n)
// space complexity: O(1)
func reverseVowels(s string) string {
var (
l int
r = len(s) - 1
)

for r > l {
for r >= 0 && !isVowel(s[r]) {
r--
}

for l < len(s) && !isVowel(s[l]) {
l++
}

if l >= r {
break
}
charL := s[l]
charR := s[r]
s = s[:r] + string(charL) + s[r+1:]
s = s[:l] + string(charR) + s[l+1:]
r--
l++
}
return s
}

// time complexity: O(n)
// space complexity: O(n)
func reverseVowels1(s string) string {
bytes := []byte(s)
var (
l int
r = len(bytes) - 1
)
for r > l {
for r >= 0 && !isVowel(s[r]) {
r--
}
for l < len(bytes) && !isVowel(s[l]) {
l++
}
if l >= r {
break
}

bytes[l], bytes[r] = bytes[r], bytes[l]
l++
r--
}
return string(bytes)
}

func isVowel(char byte) bool {
vowels := [...]byte{'a', 'o', 'e', 'i', 'u', 'A', 'O', 'E', 'I', 'U'}

for _, k := range vowels {
if char == k {
return true
}
}
return false
}
47 changes: 47 additions & 0 deletions src/0345_reverse_vowels_of_a_string/reverse_vowels_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package reversevowels

import "testing"

func TestReverseVowels(t *testing.T) {
testCases := []string{
"hello",
"leetcode",
"aA",
"a.b,.",
}

expected := []string{
"holle",
"leotcede",
"Aa",
"a.b,.",
}

for index, data := range testCases {
if res := reverseVowels(data); res != expected[index] {
t.Errorf("expected %s, got %s", expected[index], res)
}
}
}

func TestReverseVowels1(t *testing.T) {
testCases := []string{
"hello",
"leetcode",
"aA",
"a.b,.",
}

expected := []string{
"holle",
"leotcede",
"Aa",
"a.b,.",
}

for index, data := range testCases {
if res := reverseVowels1(data); res != expected[index] {
t.Errorf("expected %s, got %s", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
|0343|[Integer Break](./0343_integer_break/integer_break.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0344|[344. Reverse String](0344_reverse_string/reverse_string.go)|Easy|*`double index`*|
|0345|[345. Reverse Vowels of a String](0345_reverse_vowels_of_a_string/reverse_vowels.go)|Easy|*`double index`*|
|0349|[Intersection of Two Arrays](./0349_intersection_of_2_arrays/intersection_of_two_arrays.go)|Easy|*`set`*|
|0350| [Intersection of Two Arrays II](./0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)|Easy|*`map`*|
|0376|[Wiggle Subsequence](./0376_wiggle_subsequence/wiggle_subsequence.go)|Medium|*`dp`*|
Expand Down

AltStyle によって変換されたページ (->オリジナル) /