关于算法:算法双指针思想

34次阅读

共计 546 个字符,预计需要花费 2 分钟才能阅读完成。

leetcode

125. 验证回文串

本人写的

    var isPalindrome = function(s){
      let i = 0;
      let j = s.length -1;

      while(i <j){if(!isValid(s[i])){
          continue
          i++
        }
        if(!isValid(s[j])){
          continue
          j--
        }
        if(s[i] != s[j]){return false}
        return true;
      }
    }
    
    var isValid = function (c) {return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
    }

正确

var isPalindrome = function(s) {var s = s.toUpperCase();
  let i =0;
  let j = s.length -1;
  while(i <j){if(!isValied(s[i])){
      i++
      continue
    }
    if(!isValied(s[j])){
      j--
      continue
    }
    if(s[i] != s[j]){return false;}
    i++
    j--
  }
  return true
};

var isValied = function(c){return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
}

正文完
 0