题目

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) {
std::transform(s.begin(), s.end(), s.begin(),::tolower);
std::string::iterator begin = s.begin();
std::string::iterator end = s.end();
while ( begin < end )
{
if ( !::isalnum(*begin) ){
++begin;
}
else if ( !::isalnum(*end) ){
--end;
}
else if ( *begin != *end ){
return false;
}
else{
++begin;
--end;
}
}
return true;
}
};

Tips:

1. isalnum transform函数省去了不少篇幅

2. 双指针技巧,从两头往中间逼近,不用判断奇数偶数,代码很简洁。

============================================

第二次过回文判断的题,大体思路还在,iswalnum和transform能想起来有这么个东西,具体用法记不住了。代码改了一次以后AC了。

class Solution {
public:
bool isPalindrome(string s) {
if (s.size()==) return true;
std::transform(s.begin(), s.end(), s.begin(),::tolower);
int p1 = ;
int p2 = s.size()-;
while ( p1<p2 )
{
if ( !::iswalnum(s[p1]) ) { p1++; continue; }
if ( !::iswalnum(s[p2]) ) { p2--; continue; }
if ( s[p1++]!=s[p2--] ) return false;
}
if (p1>p2) return true;
return s[p1]==s[p2];
}
};

【Valid Palindrome】cpp的更多相关文章

  1. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  2. 【Valid Parentheses】cpp

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  3. 【Valid Number】cpp

    题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...

  4. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  5. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  6. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  7. 【Sudoku Solver】cpp

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  8. 【Gray Code】cpp

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  9. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

随机推荐

  1. WP8 调用特定API权限不足

    1.在解决方案中依次打开  Properties -> WMAppManifest.xml 2.点击功能 3.在左侧功能列表中勾选想要的功能权限.完毕

  2. mysql数据库去重语句和不同表之间列的复制语句

    1.去重语句:DELETE FROM `v_klg_item` WHERE id NOT IN (SELECT * FROM (SELECT MAX(id) FROM `v_klg_item` GRO ...

  3. PHP用Array模拟枚举

    C#中枚举Enum的写法: /// <summary> /// 公开类型 2-好友可见 1-公开 0-不公开 /// </summary> public enum OpenSt ...

  4. 快速清理Visual Studio起始页最近打开项目

    清除vs2008起始页最近打开项目 第一种:最简单的方式: 把以下内容保存为.bat批处理文件 @echo off@REG Delete HKCU\Software\Microsoft\VisualS ...

  5. DTD 知识归纳总结

    一:在xml文件中引用一个dtd规则. <!DOCTYPE 根元素 [元素声明]> 二: xml文档中中包含的内容模块 元素:元素是 XML 以及 HTML 文档的主要构建模块. 属性:属 ...

  6. yii2解析非x-www-form-urlencoded类型的请求数据(json,xml)

    组件配置添加: 'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', 'applicat ...

  7. php codebase生成随机数

    php使用codebase生成随机数.  有25幅作品拿去投票,一次投票需要选16幅,单个作品一次投票只能选择一次.前面有个程序员捅了漏子,忘了把投票入库,有200个用户产生的投票序列为空.那么你会如 ...

  8. js JSON对象属性

    json对象是是一种用于原生json分析的对象,无法对其进行构造函数调用,用java术语 来说,它相当于能够直接使用类方法的工具类JSON对象的属性parse(text[,reviver]);对参数t ...

  9. Ubuntu 14.04 安装flash插件

    分别tar.gz和apt-get方法 第一种: adboe官网下载tar.gz,进入terminal 1.解压缩包,输入命令“tar -zxvf 软件包名字” 2.拷贝插件到mozilla目录,输入命 ...

  10. PHP中英文字符串截取函数无乱码(mb_substr)和获取中英文字符串字数函数(mb_strlen)

    mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $length,要截取的字数 $encod ...