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 ebf00cd

Browse files
authored
125 valid palindrome solved. (#15)
1 parent 5a76144 commit ebf00cd

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ continually updating 😃.
3333
* [20. Valid Parentheses](./src/0020_valid_parentheses/valid_parentheses.go)   *`stack`*
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`*
36+
* [125. Valid Palindrome](./src/0125_valid_palindrome/valid_palindrome.go)   *`string;`*  *`double index`*
3637
* [438. Find All Anagrams in a String](./src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)   *`sliding window`*
3738

3839
### Linked List
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
125. Valid Palindrome
3+
https://leetcode.com/problems/valid-palindrome/
4+
5+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
6+
7+
Note: For the purpose of this problem, we define empty string as valid palindrome.
8+
*/
9+
10+
// time: 2018年12月26日
11+
12+
package validpalindrome
13+
14+
// double index
15+
// time complexity: O(n)
16+
// space complexity: O(n)
17+
func isPalindrome(s string) bool {
18+
chars := make([]uint8, 0)
19+
20+
for i := 0; i < len(s); i++ {
21+
if s[i] >= 65 && s[i] <= 90 {
22+
chars = append(chars, s[i])
23+
}
24+
if s[i] >= 97 && s[i] <= 122 {
25+
chars = append(chars, s[i]-32)
26+
}
27+
if s[i] >= 48 && s[i] <= 57 {
28+
chars = append(chars, s[i])
29+
}
30+
}
31+
32+
var (
33+
l int
34+
r = len(chars) - 1
35+
)
36+
37+
for r >= l {
38+
if chars[r] != chars[l] {
39+
return false
40+
}
41+
r--
42+
l++
43+
}
44+
return true
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package validpalindrome
2+
3+
import "testing"
4+
5+
func TestIsPalindrome(t *testing.T) {
6+
testCases := []string{
7+
"A man, a plan, a canal: Panama",
8+
"race a car",
9+
"0P",
10+
"",
11+
}
12+
13+
expected := []bool{true, false, false, true}
14+
15+
for index, data := range testCases {
16+
if res := isPalindrome(data); res != expected[index] {
17+
t.Errorf("expected %t, got %t", expected[index], res)
18+
}
19+
}
20+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
|0111|[Minimum Depth of Binary Tree](./0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)|Easy|*`binary tree`*|
3232
|0112|[Path Sum](./0112_path_sum/path_sum.go)|Easy|*`binary tree`*|
3333
|0120|[Triangle](./0120_triangle/triangle.go)|Medium|*`dynamic programming;`* *` dfs`*|
34+
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
3435
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
3536
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
3637
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|

0 commit comments

Comments
(0)

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