File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ Output: false
48
48
49
49
## 代码
50
50
51
- * 语言支持:JS,C++
51
+ * 语言支持:JS,C++,Python
52
52
53
53
JavaScript Code:
54
54
@@ -117,3 +117,31 @@ public:
117
117
}
118
118
};
119
119
```
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
+ ```
You can’t perform that action at this time.
0 commit comments