首页 > 代码库 > [LeetCode] Valid Palindrome
[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"
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.
Easy题不easy啊,我提交了好几次都没过,各种case都没有考虑清楚就开始做了。两个pointers这个我倒是一上来就确定了。
1.没有考虑清楚边界,其实两个指针往分两头往中间扫,当他们彼此超越的时候就应该结束循环。因为两个指针相互交错的时候如果还没有检测到不匹配的字符那肯定是回文串无误。
2.没读懂题目,不造alphanumberic是”数字和字母组成“的意思。。。
bool isAlphanumeric(char c) { return (c >= ‘A‘ && c <= ‘Z‘) || (c >= ‘a‘ && c <= ‘z‘) || (c >= ‘0‘ && c <= ‘9‘);}bool isPalindrome(string s) { int i = 0 ,j = (int)s.length()-1; while (i <= j) { if (!isAlphanumeric(s[i])) { i++; continue; } if (!isAlphanumeric(s[j])) { j--; continue; } if (tolower(s[i]) != tolower(s[j])) { return false; } i++; j--; } return true;}
[LeetCode] Valid Palindrome
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。