OJ练习30——T125 Valid Palindrome

判断字符串是否是回文。

字母、数字都算在内;空串也是回文。

【思路】

经典回文,两个指针,一个从前向后遍历,一个从后向前,遇到不是要求字符的就跳过。

前后指针位置交叉(i>j),则遍历结束。

特殊的地方在于,包含字母和数字,如果一一排除,代码很繁琐。

【my code】

bool isPalindrome(string s) {
        if(s=="")
            return true;
        int length=s.size();
        int i=0,j=length-1;
        while(i<=j){
            //while(!((s[i]>=‘a‘&&s[i]<=‘z‘)||(s[i]>=‘A‘&&s[i]<=‘Z‘))&&i<j)
            while((s[i]==‘ ‘||ispunct(s[i]))&&i<j)
                i++;
            //while(!((s[j]>=‘a‘&&s[j]<=‘z‘)||(s[j]>=‘A‘&&s[j]<=‘Z‘))&&i<j)
            while((s[j]==‘ ‘||ispunct(s[j]))&&i<j)
                j--;
            if(i<=j&&((s[i]==s[j])||abs(s[i]-s[j])==32)){
                i++;
                j--;
            }
            else return false;
        }
        return true;
    }

【评价】

这里用了一个ctype的函数,ispunct(char),当字符是非空格或数字或字母时,返回0,但题目不包括空格,所以要加上。

另外,i和j的比较要在每一个循环中,否则不满足条件也会继续执行。

用时16ms。

时间: 2024-08-16 08:46:27

OJ练习30——T125 Valid Palindrome的相关文章

leetcode Valid Palindrome C++&amp;amp;python 题解

题目描写叙述 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 consid

Valid Palindrome 解答

Question 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 conside

LeetCode: 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

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 tha

LeetCode(125)题解--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 car"

680. Valid Palindrome II【easy】

680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You co

leetcode Valid Palindrome C++&amp;python 题解

题目描述 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

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 co

[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."rac