题目:

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)注意两点:a、判断满足条件的字符  b、大写变小写(本题认为不区分大小写)

2)字符串为空则true

实现:

 class Solution {
public:
bool isPalindrome(string s) {
if(s.empty()) return true;
int len=strlen(s.c_str());
int i=;
int j=;
for (;i<len;i++)
{
if (isChar(s[i]))//去其他符合,若有大写则大写变小写
{
s[i] = tolower(s[i]);//库函数
s[j++]=s[i];
}
}
s[j]='\0';
if (s[]=='\0') return true;
int len2=strlen(s.c_str());
i=;
while(i<=len2--i) //判断是否回文
{
if(s[i]!=s[len2--i]) return false;
i++;
}
return true;
}
private:
bool isChar(char t)//判断是否满足条件字符
{
if (('a' <= t&&t<='z')||('A' <= t&&t<='Z')||('' <= t&&t<=''))
{
return true;
}
else
return false;
}
};

leetcode题解:Valid Palindrome(判断回文)的更多相关文章

  1. [leetcode]125. Valid Palindrome判断回文串

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

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

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

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

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

  4. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  5. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  6. [Leetcode] valid palindrome 验证回文

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

  7. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  8. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  10. LeetCode 125. Valid Palindorme (验证回文字符串)

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

随机推荐

  1. P2029 跳舞

    题目描述 小明今天得到一个跳舞毯游戏程序Dance.游戏每次连续出N个移动的“箭头”,箭头依次标号为1到N,并且的相应的分数S[1..N].如果你能“踏中”第i号箭头,你将获得相应的分数S[i]:否则 ...

  2. POJ 1375 Intervals | 解析几何

    参考了这个博客 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath&g ...

  3. BZOJ1176 [Balkan2007]Mokia 【CDQ分治】

    题目 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. 输入格式 ...

  4. 洛谷 P2155 [SDOI2008]沙拉公主的困惑 解题报告

    P2155 [SDOI2008]沙拉公主的困惑 题目描述 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为\(1\)到\(N\)的阶乘,但是,政府只发行编号与\(M!\ ...

  5. Tomcat学习笔记(十三)

    服务器组件和服务组件 服务器组件 org.apache.catalina.Server接口的实例表示Catalina的整个servlet引擎,包含了所有的组件.使用一种优雅的方法来启动/关闭整个系统, ...

  6. HDU 1054树形DP入门

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. Python设置函数调用超时

    http://blog.sina.com.cn/s/blog_63041bb80102uy5o.html 背景:        最近写的Python代码不知为何,总是执行到一半卡住不动,为了使程序能够 ...

  8. Linux Mint---fcitx中文,日语输入法

    安装中文输入法,我这边选了小企鹅,先前用过scim,稍微比较了下,效果还是这个好 安装可一步即可搞定 apt-get install fcitx fcitx-table-wubi-large fcit ...

  9. Sqlite 修改字段的名称。

    Sqlite 不支持直接修改字段的名称. 我们可以使用别的方法来实现修改字段名. 1.修改原表的名称 ALTER TABLE table RENAME TO tableOld; 2.新建修改字段后的表 ...

  10. EntityFramework之一对多关系(三)

    上篇介绍了一对一关系,下面介绍下一对多关系代码编写. 1.新建model实体,Product是产品类,Order是订单,一个产品对应多个订单 public class Product { public ...