We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
解题思路:125. 验证回文串
解题思路:
/** * @param {string} s * @return {boolean} */ var isPalindrome = function(s) { // 使用两个指针,分别从首尾向中间推进 let i = 0; let j = s.length - 1; // 不断对比每个合法字符,直到两个指针相遇 while (i < j) { // 如果两个指针遇到非法字符,则跳过 // 为避免指针移动出现i >= j的情况,增加判断 while (!check(s[i]) && i < j) { i++; } while (!check(s[j]) && i < j) { j--; } // 如果发现两个字符不相等,则s不是回文串,返回false即可 // 无论判断结果如何,都将指针向中间移动一位 if (s[i++].toLowerCase() !== s[j--].toLowerCase()) { return false; } } // 能够退出循环,表示没有发现不相等的字符,s是回文串 return true; }; // 判断字符是否合法 const check = (char) => { if ( (char >= '0' && char <= '9') || (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') ) { return true; } return false; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
解题思路:125. 验证回文串
解题思路:
The text was updated successfully, but these errors were encountered: