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. 键盘enter按钮出发登陆事件

    $("#nameInput").focus();$(".txtUserName").keydown(function (event) { if (event.k ...

  2. vue结合Promise及async实现高效开发。

    在vue中平时的开发中我们应该都会遇到promise函数,比如我们常用的axios,resource这都是用来做http请求的插件. 在平时的开发里,关于axios我们可能是这样写的 import a ...

  3. 【原创】说说JSON和JSONP,也许你会豁然开朗,含jQuery用例

    说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...

  4. 【HTML5 WebSocket】WebSocket对象特性和方法

    <HTML5 WebSocket权威指南>学习笔记&3 WebSocket方法的对象特性 1. WebSocket方法 a. send方法 send方法用于在WebSocket连接 ...

  5. delete与delete [] 真正差别

    我们通常从教科书上看到这种说明: delete 释放new分配的单个对象指针指向的内存 delete[] 释放new分配的对象数组指针指向的内存 那么,依照教科书的理解,我们看下以下的代码: int ...

  6. 4~20mA模拟输出(电流环)应用笔记(转)

    https://zm12.sm-tc.cn/?src=http%3A%2F%2Fwww.aichengxu.com%2Fview%2F5397788&uid=d2f68cd7fd230c162 ...

  7. (转)No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=arm64, VA 解决办法

    c3dEngine在iphone6模拟器下运行报错No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=arm64, V ...

  8. Redis_发布订阅(Spring Boot)

    目录 前言 生产者和消费者 发布和订阅 Java实现 注意 转至 http://www.tianmaying.com/tutorial/springboot-redis-message 前言 利用Sp ...

  9. 【ZT】NUMA架构的CPU -- 你真的用好了么?

    本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...

  10. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...