Skip to content

Commit 3190ec8

Browse files
committed
feat: 0125. Valid Palindrome
1 parent e178253 commit 3190ec8

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

Leetcode/0125.Valid-Palindrome/README.md

+68-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: "Valid Palindrome"
1010
license: ""
1111
images: []
1212

13-
tags: [LeetCode, Go, Easy/Medium/Hard, Valid Palindrome]
13+
tags: [LeetCode, Go, Easy, Valid Palindrome]
1414
categories: [LeetCode]
1515

1616
featuredImage: ""
@@ -61,12 +61,42 @@ seo:
6161
# [0125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)
6262

6363
## 題目
64+
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
6465

65-
## 題目大意
66+
Given a string s, return true if it is a palindrome, or false otherwise.
67+
68+
69+
70+
Example 1:
71+
72+
Input: s = "A man, a plan, a canal: Panama"
73+
Output: true
74+
Explanation: "amanaplanacanalpanama" is a palindrome.
75+
Example 2:
76+
77+
Input: s = "race a car"
78+
Output: false
79+
Explanation: "raceacar" is not a palindrome.
80+
Example 3:
81+
82+
Input: s = " "
83+
Output: true
84+
Explanation: s is an empty string "" after removing non-alphanumeric characters.
85+
Since an empty string reads the same forward and backward, it is a palindrome.
86+
87+
88+
Constraints:
6689

90+
1 <= s.length <= 2 * 105
91+
s consists only of printable ASCII characters.
92+
93+
## 題目大意
6794

6895
## 解題思路
6996

97+
左右指針
98+
99+
70100
## Big O
71101
時間複雜 : `O(n)`
72102
空間複雜 : `O(1)`
@@ -86,7 +116,7 @@ import (
86116
"unicode"
87117
)
88118

89-
// 時間複雜 O(n), 空間複雜 O(1)
119+
// 時間複雜 O(), 空間複雜 O()
90120
func IsPalindrome(s string) bool {
91121
// 將字符轉成小寫,
92122
s = strings.ToLower(s)
@@ -122,10 +152,44 @@ func isCharAZ09(c byte) bool {
122152
}
123153
return false
124154
}
155+
156+
func IsPalindromeStrBuilder(s string) bool {
157+
s = strings.ToLower(s)
158+
strLen := len(s)
159+
// 將字符轉成小寫, 並濾掉空格和標點符號等字符
160+
var sb strings.Builder
161+
for i := 0; i < strLen; i++ {
162+
c := rune(s[i])
163+
if unicode.IsLetter(c) || unicode.IsDigit(c) {
164+
sb.WriteRune(c)
165+
}
166+
}
167+
168+
sbStr := sb.String()
169+
170+
// 使用雙指針, 一左一右相向而行, 判斷回文
171+
left, right := 0, len(sbStr)-1
172+
for left < right {
173+
if sbStr[left] != sbStr[right] {
174+
return false
175+
}
176+
left++
177+
right--
178+
}
179+
return true
180+
}
181+
125182
```
126183

127184
## Benchmark
128185

129186
```sh
130-
187+
goos: darwin
188+
goarch: amd64
189+
pkg: LeetcodeGolang/Leetcode/0125.Valid-Palindrome
190+
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
191+
BenchmarkIsPalindrome-8 3840048 552.2 ns/op 32 B/op 1 allocs/op
192+
BenchmarkIsPalindromeStrBuilder-8 3164848 439.0 ns/op 88 B/op 4 allocs/op
193+
PASS
194+
ok LeetcodeGolang/Leetcode/0125.Valid-Palindrome 5.242s
131195
```

Leetcode/0125.Valid-Palindrome/main_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,21 @@ func BenchmarkIsPalindrome(b *testing.B) {
2828
}
2929
}
3030

31-
/*
32-
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.0125.Valid-Palindrome -bench=.
31+
func BenchmarkIsPalindromeStrBuilder(b *testing.B) {
32+
b.ResetTimer()
33+
for i := 0; i < b.N; i++ {
34+
IsPalindromeStrBuilder(tests[0].arg1)
35+
}
36+
}
3337

38+
/*
39+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0125.Valid-Palindrome -bench=.
40+
goos: darwin
41+
goarch: amd64
42+
pkg: LeetcodeGolang/Leetcode/0125.Valid-Palindrome
43+
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
44+
BenchmarkIsPalindrome-8 3840048 552.2 ns/op 32 B/op 1 allocs/op
45+
BenchmarkIsPalindromeStrBuilder-8 3164848 439.0 ns/op 88 B/op 4 allocs/op
46+
PASS
47+
ok LeetcodeGolang/Leetcode/0125.Valid-Palindrome 5.242s
3448
*/

0 commit comments

Comments
 (0)