[LintCode] 有效回文串
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] 有效回文串的更多相关文章
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- lintcode:Palindrome Partitioning 分割回文串
题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...
- lintcode-108-分割回文串 II
108-分割回文串 II 给定一个字符串s,将s分割成一些子串,使每个子串都是回文. 返回s符合要求的的最少分割次数. 样例 比如,给出字符串s = "aab", 返回 1, 因为 ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- bzoj 3676 回文串 manachar+hash
考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...
随机推荐
- django inspectdb
使用inspectdb --通过已有数据库表生成 model.pyinspectdb辅助工具检查你的settings文件指向的数据库,决定你表示你的表的Django模型并打印Python模型代码到标 ...
- 前端JS脚本将网页表格导出为Excel
话不多说,上代码! <!DOCTYPE> <html> <head> <title>Excel Test</title> </head ...
- 图片onerror事件,为图片加载指定默认图片
为图片指定加载失败时显示默认图片,js输出的img对象,onerror是事件,不是属性,所以这样写是不起作用的: var img = $(document.createElement("IM ...
- rownum浅析
对于 Oracle 的 rownum 问题,非常多资料都说不支持>.>=.=.between...and,仅仅能用以上符号(<.<=.!=),并不是说用>, >=, ...
- linux内存排查工具valgrind
官网:http://valgrind.org/info/about.html 百科介绍:http://baike.baidu.com/link?url=ZdXzff0omzoPpE_yZUlNW9lJ ...
- 个人的Linux实用命令
Linux下的命令有那么多,我不可能也不想去把每一个都记住,列举一些自己工作中很实用的命令,这些命令或许不是很常用,但是有时候却离不了. 1.网络方面 service iptables start/s ...
- 【待完善】make: command not found,以及libtool.m4 and ltmain.sh have a version mismatch问题的解决方案
之前为了使用一个库,都是去下载源码,然后根据开发者提供的README手动用GCC编译,一直不能使用Makefile感觉很蛋痛,比如最近使用的ZThread 还是怪自己以前过于依赖IDE 最近发现用Cy ...
- Could not resolve dependencies for project
最近项目上使用的是idea ide的多模块话,需要模块之间的依赖,比如说系统管理模块依赖授权模块进行认证和授权,而认证授权模块需要依赖系统管理模块进行,然后,我就开始相互依赖,然后出现这样的问题: “ ...
- 【转】移动App测试中的最佳做法
一说起软件测试,测试员想到肯定是去检查文件,功能,API,性能并确定软件是否安全,以及关于软件特定部分的其他事项.但是对于移动测试,测试员不得不基于用户移动使用模式考虑移动相关的功能. 本文是基于我的 ...
- 说明Oracle数据库逻辑备份和物理备份的方式。
说明Oracle数据库逻辑备份和物理备份的方式. 解答:Oracle备份包括逻辑备份和物理备份. 1).逻辑备份 数据库的逻辑备份包含读一个数据库记录集和将记录集写入文件. a.输出(Export)输 ...