[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求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...
随机推荐
- 003实现字符串反转reverse
这个简单就直接代码了 //写代码翻转一个C风格的字符串. (C风格的意思是"abcd"须要用5个字符来表示,包括末尾的 结束字符) #include <stdio.h> ...
- vivado设计三:一步一步生成自己的自定义IP核
开发环境:xp vivado2013.4 基于AXI-Lite的用户自定义IP核设计 这里以用户自定义led_ip为例: 1.建立工程 和设计一过程一样,见vivado设计一http://blog. ...
- 【JS设计模式】装饰者模式
装饰者模式:在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象 装饰模式的特点 (1) 装饰对象和真实对象有同样的接口.这样clien ...
- python之读取Excel 文件
# -*- coding: utf-8 -*- """ Created on Thu May 24 13:53:10 2018 @author: Frank " ...
- linux学习笔记24---命令grep
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是global search regular expression(RE) and ...
- spring-jmx.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Google Analytics Overview - Google Analytics 概述
该文档讨论了如何开始使用Google Analytics SDK for Android v3. Before you Begin - 在开始之前 在开始实现SDK之前,请确保有下面的东东: 1. ...
- java 获取用户真实ip
/** * 获取用户真实ip * @param request * @return */ public static String getIpAddr(HttpServletRequest reque ...
- visitor设计模式记录
数据类型通过枚举来区分是一种简单实用的做法. 缺点是使用的时候需要通过if .switch 去判断什么类型执行什么分支操作,说是缺点其实也要看具体场景.不过如果if代码多会导致代码很长是肯定的. 复杂 ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...