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. React 从入门到进阶之路(三)

    之前的文章我们介绍了 React 创建组件.JSX 语法.绑定数据和绑定对象.接下来我们将介绍 React 绑定属性( 绑定class  绑定style).引入图片  循环数组渲染数据. 上一篇中我们 ...

  2. restfull知识点

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......).因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致API ...

  3. 解决Navicat 连接服务器数据库报10060问题

    1.登录mysql,授予远程登录权限(确保mysql表里的登录user对应的host为 % 即可:若不是 % ,使用mysql的update更新对应host) mysql> use mysql; ...

  4. Angular输入框内按下回车会触发其它button的点击事件的解决方法

    方法:给button按钮添加type=“botton”属性

  5. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  6. ldap第一天 编译安装LDAP + ldapadmin

    此文整理学习此大神的博客:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26168435&id=5746284 一.环 ...

  7. Datatable 转换 Dictionary

    DataTable dt = new DataTable(); dt.Columns.Add("姓名"); dt.Columns.Add("学号"); dt.R ...

  8. UVa 10652(旋转、凸包、多边形面积)

    要点 凸包显然 长方形旋转较好的处理方式就是用中点的Vector加上旋转的Vector,然后每个点都扔到凸包里 多边形面积板子求凸包面积即可 #include <cstdio> #incl ...

  9. c#spinLock使用

        版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011915028/article/details/53011811 一下解释摘自msdn  ...

  10. 01.Spring Ioc 容器

    基本概念 Spring 的 Ioc 容器,通常也称应用上下文.它包含了两个概念 Ioc 和 容器: 容器:顾名思义就是用来装东西的,在 Spring 中容器里盛放的就是各种各样的 Bean.既然装了东 ...