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

Commit 47caff4

Browse files
authored
344 reverse string resolved. (#16)
1 parent ebf00cd commit 47caff4

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ continually updating 😃.
3434
* [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`*
3535
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go)    *`sliding window`*
3636
* [125. Valid Palindrome](./src/0125_valid_palindrome/valid_palindrome.go)   *`string;`*  *`double index`*
37+
* [344. Reverse String](0344_reverse_string/reverse_string.go)   *`string;`*  *`double index`*
3738
* [438. Find All Anagrams in a String](./src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)   *`sliding window`*
3839

3940
### Linked List
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
344. Reverse String
3+
https://leetcode.com/problems/reverse-string/
4+
5+
Write a function that takes a string as input and returns the string reversed.
6+
*/
7+
// time: 2018年12月26日
8+
9+
package reversestring
10+
11+
// double index
12+
// time complexity: O(n)
13+
// space complexity: O(1)
14+
func reverseString(s string) string {
15+
var (
16+
l int
17+
r = len(s) - 1
18+
)
19+
20+
for r >= l {
21+
charL := s[l]
22+
charR := s[r]
23+
s = s[:r] + string(charL) + s[r+1:]
24+
s = s[:l] + string(charR) + s[l+1:]
25+
r--
26+
l++
27+
}
28+
return s
29+
}
30+
31+
// double index
32+
// time complexity: O(n)
33+
// space complexity: O(n)
34+
func reverseString1(s string) string {
35+
sLen := len(s)
36+
runes := []rune(s)
37+
for i := 0; i < sLen/2; i++ {
38+
runes[i], runes[sLen-i-1] = runes[sLen-i-1], runes[i]
39+
}
40+
return string(runes)
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package reversestring
2+
3+
import "testing"
4+
5+
func TestReverseString(t *testing.T) {
6+
testCases := []string{
7+
"hello",
8+
"A man, a plan, a canal: Panama",
9+
"",
10+
"ab",
11+
}
12+
expected := []string{
13+
"olleh",
14+
"amanaP :lanac a ,nalp a ,nam A",
15+
"",
16+
"ba",
17+
}
18+
19+
for index, data := range testCases {
20+
if res := reverseString(data); res != expected[index] {
21+
t.Errorf("expected %s, got %s", expected[index], res)
22+
}
23+
}
24+
}
25+
26+
func TestReverseString1(t *testing.T) {
27+
testCases := []string{
28+
"hello",
29+
"A man, a plan, a canal: Panama",
30+
"",
31+
"ab",
32+
}
33+
expected := []string{
34+
"olleh",
35+
"amanaP :lanac a ,nalp a ,nam A",
36+
"",
37+
"ba",
38+
}
39+
40+
for index, data := range testCases {
41+
if res := reverseString1(data); res != expected[index] {
42+
t.Errorf("expected %s, got %s", expected[index], res)
43+
}
44+
}
45+
}

‎src/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
4141
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
4242
|0343|[Integer Break](./0343_integer_break/integer_break.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
43+
|0344|[344. Reverse String](0344_reverse_string/reverse_string.go)|Easy|*`double index`*|
4344
|0349|[Intersection of Two Arrays](./0349_intersection_of_2_arrays/intersection_of_two_arrays.go)|Easy|*`set`*|
4445
|0350| [Intersection of Two Arrays II](./0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)|Easy|*`map`*|
4546
|0376|[Wiggle Subsequence](./0376_wiggle_subsequence/wiggle_subsequence.go)|Medium|*`dp`*|

0 commit comments

Comments
(0)

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