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.

1.【基础知识】

回文字符串是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。

2.【屌丝代码】

class Solution {
public:
    bool isPalindrome(string s)
	{
        int m(0);
		char a[100]={'0'};
		if(s.size()==0)
			return true;
		for(int k =0;k<s.length();k++)
		{
			if((s[k]<='z'&&s[k]>='a')||(s[k]>='A'&&s[k]<='Z'))
				a[m++] = s[k];
		}
		a[m] = '\0';
		int i(0),j(strlen(a)-1);
		while(j>i)
		{
			if((a[i]!=a[j])&&(a[i]-a[j]!=32)&&(a[i]-a[j]!=-32))
				return false;
				i++;
				j--;
		}
		return true;
    }
};

3.【AC源码】

class Solution {
public:
bool isPalindrome(string s) {
transform(s.begin(), s.end(), s.begin(), ::tolower);
auto left = s.begin(), right = prev(s.end());
while (left < right) {
if (!::isalnum(*left)) ++left;
else if (!::isalnum(*right)) --right;
else if (*left != *right) return false;
else{ left++, right--; }
}
return true;
}
};

4.【复盘】

1.considering only alphanumeric characters and ignoring cases 理解为回文的约束为字符且忽略其大小写,没考虑到数字,导致直接实现存在问题;

2.transform 用法详见文章 简单的程序诠释C++ STL算法系列之十八:transform http://blog.csdn.net/jerryjbiao/article/details/7523110

3.auto关键字:auto对象和变量被存储在栈中,它的生命周期仅存在于它的声明所在的块(block)中。在块中定义的块如果不加其它的修饰符则都是auto类型的。auto关键字可以省去。auto对象和变量对外部模块都是不可见的。

详见:C/C++中涉及存储方式的关键字:auto,static,register,extern

4.int isalnum ( int c );//检查字符是否是字母或者数字。

详见:isalnum
<ctype.h> <cctype>

详址:http://blog.csdn.net/snowdream86/article/details/6889276

5.定义函数:int tolower(int c);头文件:#include <stdlib.h>;函数说明:若参数
c 为大写字母则将该对应的小写字母返回。

详见 :tolower

详址:http://blog.csdn.net/Michaelwubo/article/details/41080495

6.思想小结

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 02:59:09

LeetCode 之 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][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

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