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. HTML中的ID不能以数字开头

    最近在学习网页制作,发现ID在w3c规范里是不能以一个数字开头的,chrome浏览器是可以,firefox就不能使用数字开头了,其它浏览器未测试. 记录一下! W3C规范链接:http://www.w ...

  2. 清北刷题冲刺 10-28 p.m

    水题(贪心) (water) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每 ...

  3. 洛谷P2534 [AHOI2012]铁盘整理

    P2534 [AHOI2012]铁盘整理 题目描述 输入输出格式 输入格式: 共两行.第一行为铁盘个数N(1<=N<=50),第二行为N个不同的正整数,分别为从上到下的铁盘的半径R.(1& ...

  4. 洛谷P3966 [TJOI2013]单词(后缀自动机)

    传送门 统计单词出现次数……为啥大家都是写AC自动机的嘞……明明后缀自动机也能做的说…… 统计出现次数这个就直接按长度排序然后做个dp就好,这是SAM的板子的要求啊,不提了 然后考虑怎么让所有串之间隔 ...

  5. Mac 安装flutter 踩坑记

    完整版请看链接: http://b36d5043.wiz03.com/share/s/2Prl132RpQ3x2XpA4I2oTa2204K0FF0vB4J42tWIEQ04UrAg 首先下载flut ...

  6. 前后分离调用API跨域

    前后分离调用API接口跨域问题 什么是跨域?  跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,这里跨域是广义的. 广义的跨域: 资源跳转:A链接.重定向.表单提交. 资源嵌入: <li ...

  7. @Transactional之Spring事务深入理解

         Spring支持两种事务方式: 编程式事务:使用的是TransactionTemplate(或者org.springframework.transaction.PlatformTransac ...

  8. EOS 智能合约编写(一)

    本文编写了一个简单的EOS智能合约,实现用户管理和资产管理,包括存钱,取钱,转帐的功能,旨在学习如何编写自己的EOS合约功能. 系统:Ubuntu      EOS版本:v1.1.1 一.智能合约代码 ...

  9. k8s 部署应用程序

    k8s相关工具介绍: Kubeadm Kubeadm解决了处理TLS加密配置.部署核心Kubernetes组件和确保其他节点可以轻松地加入集群的问题.生成的集群通过RBAC等机制得到保护. 有关Kub ...

  10. win下rabbitmq的安装

    安装erlang 10.4 和 rabbitmq 3.7.5 然后关闭rabbitmq服务 然后设置 erlang和rabbitmq的环境变量 ERLANG_HOME=erlang安装目录 RABBI ...