LeetCode Valid Palindrome 有效回文(字符串)
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 有效回文(字符串)的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 125 Valid Palindrome 验证回文字符串
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- leetcode.双指针.680验证回文字符串-Java
1. 具体题目 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- QueryString
- const指针与指向const的指针
当使用带有const的指针时其实有两种意思.一种指的是你不能修改指针本身的内容,另一种指的是你不能修改指针指向的内容.听起来有点混淆一会放个例子上来就明白了. 先说指向const的指针,它 ...
- Unity3D -- shader语法内置函数
该篇是Unity Shader中HLSL的内置函数,主要是一些数学方面的计算函数.在写Shader的时候可以直接使用. abs //计算输入值的绝对值. acos //返回输入值反余弦值. all / ...
- Hadoop 2.0完全分布式集群搭建方法(CentOS7+Hadoop 2.7.7)
本文详细介绍搭建4个节点的完全分布式Hadoop集群的方法,Linux系统版本是CentOS 7,Hadoop版本是2.7.7,JDK版本是1.8. 一.准备环境 1. 在VMware worksta ...
- Java按模板导出Excel———基于Aspose实现
目录 开发环境 先看效果 引入jar包 校验许可证 导出方法 测试结果 占位符 开发环境 jdk 1.8 Maven 3.6 SpringBoot 2.1.4.RELEASE aspose-cells ...
- Java基础--正则表达式的规则
注意:正则表达式只关注格式是否正确,不关注内容是否有效. 一.字符集合, []表示一个字符. 1.[abc] :指a,b,c中的任意一个字符. 2.[^abc]:指除了a,b,c外的任意字符. 3.[ ...
- ajax异步请求问题
今天在使用异步请求删除图片时,想在页面测试是不是有效果,使用halt完全没反应,我以为是AJAX请求地址有问题,没有请求到这个方法中,但是在控制台中network的请求地址是正常的,后来反应过来了,异 ...
- Unity 坐标
一. transform.position 世界坐标 transform.localposition 相对于父亲的坐标 二. 三.应用 1.物体跟随鼠标 Vector3 screenPos = Cam ...
- 判断是pc端登录还是移动端登录
java判断 https://blog.csdn.net/qq_32657581/article/details/71405838 https://zhidao.baidu.com/question/ ...
- 物理机和虚拟机互相可以ping通,还是无法连接
关闭防火墙服务 CentOS # systemctl stop firewalld.service Debian # iptables -F Ubuntu # ufw disable 安装SSH服务 ...