class Solution {
public:
/**
* @param s A string
* @return Whether the string is a valid palindrome
*/
bool isPalindrome(string& s) {
// Write your code here
int left = , right = s.length() - ;
while (left < right) {
while (left < right && !isdigit(s[left]) && !isLetter(s[left]))
left++;
if (left == right) break;
while (right > left && !isdigit(s[right]) && !isLetter(s[right]))
right--;
if (right == left) break;
if (!match(s[left++], s[right--])) return false;
}
return true;
}
private:
bool isLetter(char s) {
return (s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z');
}
bool match(char s, char t) {
if (isLetter(s) && isLetter(t))
return (s == t) || (s - t == ) || (t - s == );
return s == t;
}
};

[LintCode] 有效回文串的更多相关文章

  1. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  2. lintcode:Palindrome Partitioning 分割回文串

    题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  3. lintcode-108-分割回文串 II

    108-分割回文串 II 给定一个字符串s,将s分割成一些子串,使每个子串都是回文. 返回s符合要求的的最少分割次数. 样例 比如,给出字符串s = "aab", 返回 1, 因为 ...

  4. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. bzoj 3676 回文串 manachar+hash

    考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...

随机推荐

  1. Java编程介绍

    原文地址:http://happyshome.cn/blog/java/introduction.html 本文介绍的编程基础知识很Java适合刚開始学习的人. 要学习编程,你须要了解编程语言的语法和 ...

  2. 使用NoSQL Manager for MongoDBclient连接mongodb

    1.安装NoSQL Manager for MongoDB 下载地址:http://www.mongodbmanager.com/download 2.打开client,选server-new mon ...

  3. Mac OSX下Go语言开发环境的搭建与配置--使用InteliJ IDEA 13

    折腾了一上午终于把go语言的ide配置好了. 其实GO语言的语法和特性早在去年的时候就学习了一遍.结果后来一直没机会进行开发,结果还是个GO小白.感叹一下,要学好一门编程语言唯一的途径就是多写代码.. ...

  4. NSArray、NSMutableArray和NSMutableDictionary的用法

    转自:http://www.cnblogs.com/wangpei/admin/EditPosts.aspx?opt=1 NSArray是静态的数组,就是它所指向的内容是不可改变的,它指向一段内存区域 ...

  5. oracle TABLE ACCESS BY INDEX ROWID 你不知道的索引回表-开发系列(三)

    1 引言 近期系统常常提示一个sql查询时间过长的问题,看了一下就是一个每天依照时间戳统计前一天量的sql. 表总的数据量为53483065. 语句例如以下: select count(x.seria ...

  6. MySQL 记录中包含换行符

    换行符: \r\n \r CHAR(10) 处理方法: REPLACE(str,from_str,to_str); 如:REPLACE('aaa\nbbb','\n','');

  7. uGUI Anchor

    Anchor定位:inspector面板的Rect Transform组件中PosX左边的方框图标就是设置锚点的,做界面自适应时可定位控件在视图中的位置,与NGUI类似.Anchor+Canvas的C ...

  8. Java实现ZIP解压功能

    1.引入依赖 <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...

  9. swt生成、jar可执行包生成.exe可执行文件(giter)

    http://tomfish88.iteye.com/blog/1074786 —————————————————————————————————————————————————————————— 最 ...

  10. Windows的静态库使用步骤

    windows库程序: 1.静态库程序 - 运行时不独立存在,会被链接到可执行文件或者动态库中,目标程序的归档. 文件扩展名:LIB 2.动态库程序 - 运行时独立存在,不会被链接到可执行文件或其他动 ...