Skip to content

Commit 9cde1f1

Browse files
committed
🐱(string): 125. 验证回文串
复盘
1 parent 8aa6a46 commit 9cde1f1

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Diff for: docs/data-structure/array/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -1323,9 +1323,7 @@ func maxProfit(prices []int) int {
13231323

13241324
[原题链接](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/comments/)
13251325

1326-
### 思路
1327-
1328-
双指针
1326+
### 解一:双指针
13291327

13301328
- 指针 i 指向最左端,从左到右依次循环
13311329
- 指针 j 指向最右端,从右到左依次循环

Diff for: docs/data-structure/string/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,38 @@ class Solution(object):
924924
return True
925925
```
926926

927+
复盘:
928+
929+
```python
930+
class Solution:
931+
def isPalindrome(self, s: str) -> bool:
932+
length = len(s)
933+
i = 0
934+
j = length - 1
935+
936+
while i < j:
937+
if self.isValid(s[i]) and self.isValid(s[j]):
938+
if s[i].lower() != s[j].lower():
939+
return False
940+
i += 1
941+
j -= 1
942+
943+
if not self.isValid(s[i]):
944+
i += 1
945+
946+
if not self.isValid(s[j]):
947+
j -= 1
948+
return True
949+
950+
def isValid(self, c):
951+
if c.isdigit() or c.isalpha():
952+
return True
953+
return False
954+
```
955+
956+
- 时间复杂度:`O(n)`
957+
- 空间复杂度:`O(1)`
958+
927959

928960
## 139. 单词拆分
929961

0 commit comments

Comments
 (0)