Skip to content

Commit 8197f0a

Browse files
ybian19azl397985856
authored andcommitted
feat: 125.valid-palindrome add Python3 implementation (azl397985856#83)
1 parent 6df2438 commit 8197f0a

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

problems/125.valid-palindrome.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Output: false
4848

4949
## 代码
5050

51-
* 语言支持:JS,C++
51+
* 语言支持:JS,C++,Python
5252

5353
JavaScript Code:
5454

@@ -117,3 +117,31 @@ public:
117117
}
118118
};
119119
```
120+
121+
Python Code:
122+
123+
```python
124+
class Solution:
125+
def isPalindrome(self, s: str) -> bool:
126+
left, right = 0, len(s) - 1
127+
while left < right:
128+
if not s[left].isalnum():
129+
left += 1
130+
continue
131+
if not s[right].isalnum():
132+
right -= 1
133+
continue
134+
if s[left].lower() == s[right].lower():
135+
left += 1
136+
right -= 1
137+
else:
138+
break
139+
return right <= left
140+
141+
def isPalindrome2(self, s: str) -> bool:
142+
"""
143+
使用语言特性进行求解
144+
"""
145+
s = ''.join(i for i in s if i.isalnum()).lower()
146+
return s == s[::-1]
147+
```

0 commit comments

Comments
 (0)