LeetCode 125. 验证回文串

Posted by cody1991 on August 9, 2020

简单思路:循环查看左右两边,看是否相等就好了

125. 验证回文串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function (s) {
  s = s.toLocaleLowerCase().replace(/[^a-zA-Z0-9]/g, "");
  let result = true;

  for (let i = 0; i < s.length / 2; i++) {
    const left = s[i];
    const right = s[s.length - i - 1];
    if (left !== right) {
      result = false;
      break;
    }
  }
  return result;
};

console.log(isPalindrome("A man, a plan, a canal: Panama"));