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

344 reverse string resolved. #16

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 1 commit into master from reverse-string
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
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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`*
* [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
41 changes: 41 additions & 0 deletions src/0344_reverse_string/reverse_string.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
344. Reverse String
https://leetcode.com/problems/reverse-string/

Write a function that takes a string as input and returns the string reversed.
*/
// time: 2018年12月26日

package reversestring

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

for r >= l {
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
}

// double index
// time complexity: O(n)
// space complexity: O(n)
func reverseString1(s string) string {
sLen := len(s)
runes := []rune(s)
for i := 0; i < sLen/2; i++ {
runes[i], runes[sLen-i-1] = runes[sLen-i-1], runes[i]
}
return string(runes)
}
45 changes: 45 additions & 0 deletions src/0344_reverse_string/reverse_string_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package reversestring

import "testing"

func TestReverseString(t *testing.T) {
testCases := []string{
"hello",
"A man, a plan, a canal: Panama",
"",
"ab",
}
expected := []string{
"olleh",
"amanaP :lanac a ,nalp a ,nam A",
"",
"ba",
}

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

func TestReverseString1(t *testing.T) {
testCases := []string{
"hello",
"A man, a plan, a canal: Panama",
"",
"ab",
}
expected := []string{
"olleh",
"amanaP :lanac a ,nalp a ,nam A",
"",
"ba",
}

for index, data := range testCases {
if res := reverseString1(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 @@ -40,6 +40,7 @@
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
|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`*|
|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 によって変換されたページ (->オリジナル) /