【问题描写叙述】

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;
}
};

【屌丝AC源代码】

#include<iostream>
#include<string> using namespace std; class Solution {
public:
bool isPalindrome(string s)
{
int m(0);
if(s.size()==0)
return true;
char *a = new char[s.size()-1];
for(int k = 0;k<s.length();k++)
{
if((s[k]<='z'&&s[k]>='a')||(s[k]>='A'&&s[k]<='Z')||(s[k]>='0'&&s[k]<='9'))
a[m++] = s[k];
// if(m>240)
// cout<<m<<endl;
}
// cout<<a<<endl;
a[m] = '\0';
// cout<<a<<endl;
int i(0),j(m-1);
while(j>i)
{
if(((a[i]<='z'&&a[i]>='a')||(a[i]>='A'&&a[i]<='Z'))&&((a[j]<='z'&&a[j]>='a')||(a[j]>='A'&&a[j]<='Z')))
if((a[i]!=a[j])&&((a[i]-a[j]!=32)&&(a[i]-a[j]!=-32)))
return false;
else
{
i++;j--;continue;
}
if(a[i]!=a[j])
return false;
i++;
j--; }
return true;
}
}; int main()
{
/*
cout<<sizeof(int)/sizeof(char)<<endl;
cout<<sizeof(unsigned int)/sizeof(char)<<endl;
cout<<sizeof(long int)/sizeof(char)<<endl;
cout<<sizeof(double)/sizeof(char)<<endl;
*/
// string mystr = "tyQZrD2 7UL91z,i`O2ef:6e'2\"yP !:,U.:pX90PU3CXo'i!;3 `j 0?\"'hK8 ? BAjM2\"DBw?7!4R3?U2E8F2y!? 3 R2!fw 6e!:0 ErCi98KM`,8`8648,mi3P0`,!5 E.?00J3A 52\"x8,tHy!'2!DLBbK'j!tt1C' 7`JPulW\"\"uRTbr\"',\",U`ZOW5'\"LMDQDMJ\"'5WOZ`U,\",'\"rbTRu\"\"WluPJ`7 'C1tt!j'KbBJD!2'!yHt,8x\"25 A3J00?.E 5!,`0P3im,8468`8,`MK89iCrE 0:!e6 wf!2R 3?!y2F8E2U? 3R4!7?wBD\"2MjAB ? 8Kh'\"?0 j` 3;!i'oXC3UP09Xp:.U,:! Py\"2'e6:fe2O`i,z19LU7 2DrZQyt";
string mystr = "ab2A";
Solution my;
cout<<my.isPalindrome(mystr)<<endl;
/*
cout<<mystr.size()<<endl;
cout<<mystr[0]<<endl;
cout<<mystr[mystr.size()-1]<<endl;
*/ return 0;
}

3.【AC源代码】

class Solution
{
public:
bool isPalindrome(string s)
{
transform(s.begin(), s.end(), s.begin(), ::tolower);// 字符串所有改为小写输出到 s 之中
auto left = s.begin();
auto right = prev(s.end());
while (left < right)
{
if (!::isalnum(*left))
{ ++left;
continue;}
if (!::isalnum(*right))
{ --right;
continue;}
if (*left != *right)
return false;
else{ left++, right--; }
}
return true;
}
};




4.【复盘】

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

2.transform 使用方法详见文章 简单的程序诠释C++
STL算法系列之十八:transform

详址:http://blog.csdn.net/jerryjbiao/article/details/7523110

3.autokeyword:auto对象和变量被存储在栈中,它的生命周期仅存在于它的声明所在的块(block)中。

在块中定义的块假设不加其他的修饰符则都是auto类型的。

autokeyword能够省去。auto对象和变量对外部模块都是不可见的。

详见:C/C++中涉及存储方式的keyword: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

5.【算法核心思想】

【屌丝AC源代码】

1)剔除字符串中非字母、数字字符;

2)非大写和小写关系的不相等以及普通的不等构成判据;

3)前、后向迭代比对,得到结果。

【AC源代码】

1)tolower方法,字符串转小写;

2)islnum方法。滤除标点字符得到推断;

3)前、后向迭代比对,得到结果。

LeetCode 之 Valid Palindrome(字符串)的更多相关文章

  1. Leetcode 125 Valid Palindrome 字符串处理

    题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...

  2. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  3. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. leetcode 125. Valid Palindrome ----- java

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. leetcode:Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. Java [Leetcode 125]Valid Palindrome

    题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  9. 【LeetCode】- Valid Palindrome(右回文)

    [ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...

随机推荐

  1. 一些汇编中的 trick

    1. PC 总是指向下一条将要被执行的指令,而不是指向正在被执行的指令,这是有道理的,因为执行指令不是一个 atom 过程,而是分成了好多步骤,在执行指令的过程中 cpu 完全有可能将下一条将要执行的 ...

  2. PNG图片透明 IE6 解决方法

    原文发布时间为:2009-11-18 -- 来源于本人的百度文章 [由搬家工具导入] png透明解决办法 第1 种方法:定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了。 ...

  3. 【C/C++】知识点

    1.C++中的参数传递机制:值传递.指针传递.引用传递 2.C++的内部类和外部类: 一个讲得不错的博客,不过不让转载:C++内部类 3.static 可以修饰局部变量.全局变量和函数. 不可修饰类! ...

  4. memcached的内存管理与删除机制

    memcached的内存管理与删除机制 简介 注意:Memcache最大的value也只能是1M的空间,超过1M的数据无法保存(修改memcache源代码).   注意:内存碎片化永远都存在,只是哪一 ...

  5. [Machine Learning with Python] Data Preparation by Pandas and Scikit-Learn

    In this article, we dicuss some main steps in data preparation. Drop Labels Firstly, we drop labels ...

  6. 初探Java类型擦除

    本篇博客主要介绍了Java类型擦除的定义,详细的介绍了类型擦除在Java中所出现的场景. 1. 什么是类型擦除 为了让你们快速的对类型擦除有一个印象,首先举一个很简单也很经典的例子. // 指定泛型为 ...

  7. Linux笔记:vim

    文件搜索后显示高亮,即使退出编辑高亮依然存在.使用以下几个方法: 1)指令模式下运行:nohlsearch 2)运行set nohlsearc,可永久关闭搜索高亮 3)搜索任意不存在的字符串

  8. 洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense

    https://www.luogu.org/problemnew/show/P2884 题目描述 Farmer John is an astounding accounting wizard and ...

  9. 深入浅出 Cocoa 之 Core Data(1)- 框架详解

    深入浅出 Cocoa 之 Core Data(1)- 框架详解 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 Core data 是 Cocoa 中处 ...

  10. 解决unknown import path "golang.org/x/sys/unix": unrecognized import path "golang.org/x/sys"

    问题描述 当我们使用 go get.go install.go mod 等命令时,会自动下载相应的包或依赖包.但由于众所周知的原因,类似于 golang.org/x/... 的包会出现下载失败的情况. ...