LeetCode 125. 验证回文串
原题链接
简单
作者:
caichuntao
,
2023-06-16 17:00:22
,
所有人可见
,
阅读 62
方法1:双指针遍历
时间复杂度:$O(n)$
空间复杂度:$O(1)$
解题思路
Java 代码
class Solution {
public boolean isPalindrome(String s) {
int l = 0, r = s.length() - 1;
while (l < r) {
while (l < r && !Character.isLetterOrDigit(s.charAt(l))) {
l ++;
}
while (l < r && !Character.isLetterOrDigit(s.charAt(r))) {
r --;
}
if (l < r) {
if (Character.toLowerCase(s.charAt(l))
!= Character.toLowerCase(s.charAt(r))) {
return false;
}
}
l ++;
r --;
}
return true;
}
}