leetcode:Valid Palindrome

一、     题目

题目给出一个字符串,求出它是否为回文字符串,其中只有字母和数字是有效字符,其他的字符可以忽略。

例如:"Aman, a plan, a canal: Panama" 是回文字符串.

"race a car" is not a palindrome.不是回文字符串

二、     分析

看到这个题目我首先想到的是使用两个数组将有效字符串保存,其中一个正序一个逆序,然后做比较。但是考虑到效率和空间使用,可以使用“两指针法”,即设置一个左指针一个右指针,相向移动,判断他们的有效值是否相等,不相等则直接false,直到相遇。

class Solution {
public:
	//判断字符是不是字母或字符
	bool isAlphanumeric(char c)
     {
         if((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')||(c >= '0' && c <= '9'))
             return true;
         else return false;
     }
    bool isPalindrome(string s) {

        int len = s.size();
        //判断是否为空,为空则返回真
        if(len == 0) return true;
        int left=0;
        int right = len-1;
        while(left<right){
        	//注意判断left是否小于len,当时就卡在这了
        	if(!isAlphanumeric(s[left])&&left<=len-1){
        		left++;
        	}
        	else if(!isAlphanumeric(s[right])&&right>=0){
        		right--;
        	}
        	else {
        		//注意字母的大小写,这里只需要判断字母是否相等或差的绝对值是否为32
        		if(s[left] != s[right]&&fabs(s[left]-s[right])!=32)
        			return false;
        		left++;
        		right--;
			}
        }
        return true;
    }
};
时间: 2024-08-03 09:08:19

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 tha

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 Parentheses 题解

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

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】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] 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