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

125 valid palindrome solved. #15

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 valid-palindrome
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 @@ -33,6 +33,7 @@ continually updating 😃.
* [20. Valid Parentheses](./src/0020_valid_parentheses/valid_parentheses.go)   *`stack`*
* [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`*
* [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
45 changes: 45 additions & 0 deletions src/0125_valid_palindrome/valid_palindrome.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
125. Valid Palindrome
https://leetcode.com/problems/valid-palindrome/

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.
*/

// time: 2018年12月26日

package validpalindrome

// double index
// time complexity: O(n)
// space complexity: O(n)
func isPalindrome(s string) bool {
chars := make([]uint8, 0)

for i := 0; i < len(s); i++ {
if s[i] >= 65 && s[i] <= 90 {
chars = append(chars, s[i])
}
if s[i] >= 97 && s[i] <= 122 {
chars = append(chars, s[i]-32)
}
if s[i] >= 48 && s[i] <= 57 {
chars = append(chars, s[i])
}
}

var (
l int
r = len(chars) - 1
)

for r >= l {
if chars[r] != chars[l] {
return false
}
r--
l++
}
return true
}
20 changes: 20 additions & 0 deletions src/0125_valid_palindrome/valid_palindrome_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package validpalindrome

import "testing"

func TestIsPalindrome(t *testing.T) {
testCases := []string{
"A man, a plan, a canal: Panama",
"race a car",
"0P",
"",
}

expected := []bool{true, false, false, true}

for index, data := range testCases {
if res := isPalindrome(data); res != expected[index] {
t.Errorf("expected %t, got %t", 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 @@ -31,6 +31,7 @@
|0111|[Minimum Depth of Binary Tree](./0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)|Easy|*`binary tree`*|
|0112|[Path Sum](./0112_path_sum/path_sum.go)|Easy|*`binary tree`*|
|0120|[Triangle](./0120_triangle/triangle.go)|Medium|*`dynamic programming;`* *` dfs`*|
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
Expand Down

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