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 ...
随机推荐
- 从 .NET Framework到 .NET Core
参考资料: https://docs.microsoft.com/zh-cn/dotnet/core/porting/ https://docs.microsoft.com/zh-cn/aspnet/ ...
- Tensorflow函数——tf.placeholder()函数
tf.placeholder()函数 Tensorflow中的palceholder,中文翻译为占位符,什么意思呢? 在Tensoflow2.0以前,还是静态图的设计思想,整个设计理念是计算流图,在编 ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...
- 洛谷P3803 【模板】多项式乘法(FFT)
P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: ...
- Ruby中如何识别13位的时间戳
由于13位的时间戳在Ruby中是比较另类的,以为Ruby中默认的时间戳都是10位的.而Time和Date是Ruby中常用的处理时间的模块. 由于最初遇到问题的时候网上搜了好久都没找到合适的,因此就自己 ...
- 我的省选 Day -5
Day -5 时间载着我们,一天又一天,呼啸而过. 已经记不清今天是Day 负几了,总之还有不到一个星期就要去参加选拔赛了. 写一下今晚做NOI2009的心路历程. T1题意有点绕,但很快看出是个二分 ...
- JavaScript for impatient programmers
参考 作者发布的在线HTML版本(包含大部分主要章节,只缺少四个额外章节)——https://exploringjs.com/impatient-js/toc.html 作者的博客——http://2 ...
- kvm磁盘io优化以及性能测试以及与物理机对比
ubuntu下kvm的磁盘io性能优化步骤 1.virsh shutdown wcltest2 2.virsh edit wcltest2 <driver name='qemu' type='q ...
- Asp.net Core 添加 EF 工具并执行初始迁移错误解决方法(Add-Migration Initial---Build failed.)
1.问题: 首次在ASP.NET Core项目中使用Code First模式的Entity Framework框架,在添加EF工具并做初始化迁移(perform initial migration), ...
- gogs迁移
windows->linux 之前gogs放在windows server2016中,需要迁移至linux docker中. 首先拉取gogs镜像 docker pull gogs/gogs 然 ...