125. 验证回文串
思路
先做正则将不符合的字符去掉,再从两端对比
代码
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
s = s.replace(/[^a-zA-Z0-9]/g, '')
let left = 0, right = s.length - 1;
while(left < right) {
let slow = s[left].toLowerCase(), fast = s[right].toLowerCase()
if (slow !== fast) {
return false
}
left++;
right--;
}
return true
};
复杂度分析
时间复杂度 #card=math&code=O%28N%29)
空间复杂度 #card=math&code=O%281%29)