Code Monkey home page Code Monkey logo

Comments (2)

bazinga-web avatar bazinga-web commented on August 13, 2024

解题思路:

  1. 正则表达式去除非数字和字母
  2. 长度小于2时return true
  3. 长度大于等于2时,指定left right前后两个指针,当left<right时,
    循环,如果在任意地点s[left] !== s[right], return false。否则
    left++, right--,继续执行循环
  4. 当循环结束还没有return false ,则return true
/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function (s) {
  const formatStr = s.toLowerCase().replace(/[\W_]/g, "");
  if (s.length < 2) {
    return true;
  }
  let left = 0;
  let right = formatStr.length - 1;

  while (left <= right) {
    if (formatStr[left] !== formatStr[right]) {
      return false;
    }
    left++;
    right--;
  }

  return true;
};

from like-algorithms.

Ray-56 avatar Ray-56 commented on August 13, 2024

不知道是不是我的理解有误,该题目只考虑字母和数字字符,但是验证时会有下划线,所以在正则替换上加入下换线替换

解法一

思路:

  • 将输入的字符串中的其它字符替换掉
  • 如果长度比2个小直接返回 true
  • 字符逆序入栈
  • 对比输入字符格式化后的与栈转字符
var isPalindrome = function (s) {
    const str = s.replace(/[\W_]/g, '').toLocaleLowerCase();
    
    const stack = [];
    for (let i = str.length - 1; i >= 0; i--) {
        stack.push(str[i]);
    }

    return stack.join('') === str;
}

解法二

双指针

思路

  • 将输入的字符串中的其它字符替换掉
  • 如果长度比2个小直接返回 true
  • 创建左右指针,遍历对比左右字符是否一致,不一致则返回 false
  • 当 left > right 说明遍历结束,左右字符都相等,返回 true
var isPalindrome = function (s) {
    const str = s.replace(/[\W_]/g, '');
    
    if (str.length < 2) return true;
    let left = 0;
    let right = str.length - 1;

    while (left <= right) {
        if (str[left].toLocaleLowerCase() !== str[right].toLocaleLowerCase()) return false;
        left++;
        right--;
    }
    return true;
}

from like-algorithms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.