class Solution {
public:
bool isPalindrome(string s) {
if(s=="") return true;
if(s.length()==) return true; //单个字符,对称
char *p,*q;
p=&s[]; //p指向开头
q=&s[s.length()-]; //q指向末尾
while(p!=q){
//测试字符串里是否有字母或数字,若没有,则立刻返回
while( (*p<'' || (*p>''&&*p<'A') || (*p>'Z'&&*p<'a') || *p>'z')&&p!=q){
p++;
}
while( (*q<'' || (*q>''&&*q<'A') || (*q>'Z'&&*q<'a') || *q>'z')&&p!=q) //非字母和数字,跳过
q--;
if(*q>='A'&&*q<='Z') //若大写,转为小写
*q=*q+;
if(*p>='A'&&*p<='Z') //若大写,转为小写
*p=*p+;
if(p==q)
break;
if(*p==*q){
p++;
if(p==q)
break;
q--;
}
else
return false;
}
return true;
}
};

题意:

"A man, a plan, a canal: Panama" is a palindrome.是回文
"race a car" is not a palindrome.非回文

回文:即将字符串倒过来之后和原来仍一样。如:did=did

但是,此题要求过滤掉非数字和字母的其他字符,而且不区分大小写,A和a是一样的。

思路:用两个指针,分别指向字符串的头和尾,每次判断要过滤掉无效的字符。

注意:要考虑空串(即没有字母和数字,可能只有空格,标点什么的),只有1个字符的字符串。还得考虑两个指针指向同一个地址时即已经是回文了。

LeetCode Valid Palindrome 有效回文(字符串)的更多相关文章

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

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

  2. [Leetcode] valid palindrome 验证回文

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

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

    给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...

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

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

  5. [LintCode] Valid Palindrome 验证回文字符串

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

  6. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

  7. leetcode.双指针.680验证回文字符串-Java

    1. 具体题目 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...

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

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

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

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

随机推荐

  1. 基于FPGA具有容错能理的异步串口程序设计

    首先,问题源于一个项目.本来是一个很简单的多个串口收发FIFO存取数据的小程序,通过电脑验证也可用,而下位机板子之间通信就出现了丢数问题. 经过分析原因如下: 我的串口收模块是基于特权同学的开发板程序 ...

  2. Web调试利器fiddler介绍

    转载:http://blog.chinaunix.net/uid-27105712-id-3738821.html 最近在使用fiddler,发现这个真是非常最犀利的web调试工具,笔者这里强烈推荐给 ...

  3. Codeforces#514E(贪心,并查集)

    #include<bits/stdc++.h>using namespace std;long long w[100007],sum[100007];int fa[100007],degr ...

  4. 使用poi导出Excel表格,jar包冲突

    HTTP Status 500 – Internal Server Error Type Exception Report Message Handler processing failed; nes ...

  5. 检测工具lynis

    wget https://gitee.com/zzhlinux911218/software/raw/master/linux-inspect2.sh;bash linux-inspect2.sh检测 ...

  6. mathjax;latex

    \lfloor $\lfloor$ \rfloor $\rfloor$ \sum_{i=1}^{n} $\sum_{i=1}^{n}$ \mu $\mu$ \mid $\mid$ \Leftright ...

  7. hdu3949(线性基,求第k小的异或和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 XOR Time Limit: 2000/1000 MS (Java/Others)    Me ...

  8. (转)CentOS最大文件描述符限制更改

    CentOS最大文件描述符限制更改 原文:https://www.cnblogs.com/TonyXiaoClub/p/4747736.html 系统级的限制:/proc/sys/fs/file-ma ...

  9. 无需控件直接导出xls(csv)

    /// <summary> /// 执行导出 /// </summary> /// <param name="ds">要导出的DataSet&l ...

  10. win10 asp+access

    今天是灰色的一天. 大清早来到单位,告知:单位主页访问不了! 我远程看了下.所有的文件后缀都变成了.crab 赶紧上网查下,哎呀我的妈呀,这是中了勒索病毒啊. 还用查?打开服务器,有个打开的文本文件写 ...