leetcode 125
125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
判断字符串是否为回文字符串,只需考虑字符串中的数字和字母。
思路:字符串首尾各一个指针,进行比较;
指针移动规则:若相等则首指针向前移动一位,尾指针向后移动以为;若出现非法字符则首尾指针相应的移动一位。
代码如下:
class Solution {
public:
bool isPalindrome(string s) {
int n = s.size();
int st = ;
if(n == )
{
return true;
}
n--;
while(st < n)
{
if(isValid(s[st]) && isValid(s[n]))
{
if(s[st] <= && s[st] >= )
{
if(s[n] > || s[n] < )
{
return false;
}
else if(s[st] != s[n])
{
return false;
}
else
{
st++;
n--;
continue;
}
}
if(s[st] == s[n] || s[st]-s[n] == || s[n]-s[st] == )
{
st++;
n--;
continue;
}
else
{
return false;
}
}
else if(isValid(s[st]))
{
n--;
}
else if(isValid(s[n]))
{
st++;
}
else
{
n--;
st++;
}
}
return true;
}
bool isValid(char s)
{
if(s < || s > )
{
return false;
}
else if(s > && s < )
{
return false;
}
else if(s > && s < )
{
return false;
}
return true;
}
};
leetcode 125的更多相关文章
- 前端与算法 leetcode 125. 验证回文串
目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端 ...
- Leetcode 125 Valid Palindrome 字符串处理
题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...
- LeetCode(125)题解--Valid Palindrome
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Java实现 LeetCode 125 验证回文串
125. 验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- Valid Palindrome ---- LeetCode 125
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode 125. Valid Palindrome ----- java
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
随机推荐
- javascript笔记7-事件
主要讲事件流.事件捕获.事件冒泡.事件处理程序.事件属性.事件类型.内存和优化等. 由于本文已经在微信订阅号上发布,为了防止原创性冲突检测,因此本文在此处已经删除. 详细请扫描订阅号二维码,查看历史信 ...
- 如何在Kali Linux中搭建钓鱼热点
文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用! 0×00 实验环境 操作系统:Kali 1.0 (VM) FackAP: easy-creds 硬件:NETGEAR wg ...
- .Net MVC框架 + WCF 搭建 集群开发
http://www.cnblogs.com/zhijianliutang/archive/2012/01/28/2258844.html
- 【转】LokiJS:纯JavaScript实现的轻量级数据库
原文转自:http://www.html5cn.org/article-7091-1.html LokiJS一个轻量级的面向文档的数据库,由JavaScript实现,性能高于一切.目的是把JavaSc ...
- False Discovery Rate, a intuitive explanation
[转载请注明出处]http://www.cnblogs.com/mashiqi Today let's talk about a intuitive explanation of Benjamini- ...
- JavaScript 图片的上传前预览(兼容所有浏览器)
功能描述 通过 JavaScript 实现图片的本地预览(无需上传至服务器),兼容所有浏览器(IE6&IE6+.Chrome.Firefox). 实现要点 ● 对于 Chrome.Fire ...
- 排序算法总结(四)快速排序【QUICK SORT】
感觉自己这几篇都是主要参考的Wikipedia上的,快排就更加是了....wiki上的快排挺清晰并且容易理解的,需要注意的地方我也添加上了注释,大家可以直接看代码.需要注意的是,wikipedia上快 ...
- MapReduce简介
MapReduce简介 参考自[http://www.cnblogs.com/swanspouse/p/5130136.html] MapReduce定义: MapReduce是一种可用于数据处理的编 ...
- 02 Linux 下安装JDK并测试开发“Hello World!”
测试环境 主机系统:Win7 64位 虚拟机:VMware® Workstation 11.1.0 虚拟机系统:CentOS 6.5 64位 Kernel 2.6.32-431.e16.x86_6 ...
- bzoj3087: Coci2009 misolovke
Description [misolovke]给定一个 N*N 的网格.每个格子里至少会有一个捕鼠器, 并且已知每个格子里的捕鼠器个数.现在需要在 每一行 中选取恰好 K 个连续的格子, 把里面的捕鼠 ...