Skip to content

Commit

Permalink
feat: 125.valid-palindrome add Python3 implementation (azl397985856#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybian19 authored and azl397985856 committed Aug 6, 2019
1 parent 6df2438 commit 8197f0a
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion problems/125.valid-palindrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Output: false

## 代码

* 语言支持:JS,C++
* 语言支持:JS,C++,Python

JavaScript Code:

Expand Down Expand Up @@ -117,3 +117,31 @@ public:
}
};
```
Python Code:
```python
class Solution:
def isPalindrome(self, s: str) -> bool:
left, right = 0, len(s) - 1
while left < right:
if not s[left].isalnum():
left += 1
continue
if not s[right].isalnum():
right -= 1
continue
if s[left].lower() == s[right].lower():
left += 1
right -= 1
else:
break
return right <= left
def isPalindrome2(self, s: str) -> bool:
"""
使用语言特性进行求解
"""
s = ''.join(i for i in s if i.isalnum()).lower()
return s == s[::-1]
```

0 comments on commit 8197f0a

Please sign in to comment.