[LeetCode][JavaScript]Valid Palindrome

https://leetcode.com/problems/valid-palindrome/

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.



两个指针,一个指向头,一个指向尾。如果不是字母数字,就继续找,头++尾--,然后比较值就好啦。

没交的时候看到这道题有Solution,考虑了很久有没有其他机智的办法。等交了一看,其实并没有,一切都是错觉。

 1 /**
 2  * @param {string} s
 3  * @return {boolean}
 4  */
 5 var isPalindrome = function(s) {
 6     s = s.trim();
 7     if(s === ""){
 8         return true;
 9     }else{
10         var i = 0, j = s.length - 1;
11         while(i <= j){
12             while(!/[a-z0-9]/i.test(s[i]) && i < s.length && i < j){
13                 i++;
14             }
15             while(!/[a-z0-9]/i.test(s[j]) && j > 0 && i < j){
16                 j--;
17             }
18
19             if(s[i].toUpperCase() !== s[j].toUpperCase()){
20                 return false;
21             }
22
23             i++; j--;
24         }
25
26         i--; j++;
27         if(i !== j && s[i].toUpperCase() !== s[j].toUpperCase()){
28             return false;
29         }
30
31         return true;
32     }
33 };
时间: 2024-08-16 02:05:33

[LeetCode][JavaScript]Valid Palindrome的相关文章

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

【Leetcode】Valid Palindrome

题目链接:https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a ca

[leetcode] 1. Valid Palindrome

leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider that the string might be empty? This is a good question to ask during an interview. For

leetCode 125. Valid Palindrome 字符串

125. Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Hav

[Leetcode][JAVA] Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

[LeetCode] 125. Valid Palindrome 有效回文

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

LeetCode 1216. Valid Palindrome III

原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, find out if the given string is a K-Palindrome or not. A string is K-Palindrome if it can be transformed into a palindrome by removing at most k charac

Java [Leetcode 125]Valid Palindrome

题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider t

【LeetCode】- Valid Palindrome(正确的回文)

[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 直译:给你一个字符串, 判定它是否是回文(只统计字母.数字,其他字符请忽略). For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" i