题目

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. 解决mysql占用IO过高

    1.日志产生的性能影响:由于日志的记录带来的直接性能损耗就是数据库系统中最为昂贵的IO资源.MySQL的日志包括错误日志(ErrorLog),更新日志(UpdateLog),二进制日志(Binlog) ...

  2. IOS学习感想

    1.一开始学习的时候将会感到非常的难,即使自己曾经学过C/JAVA/HTML/CSS/JS/PHP等,但是对于学过C++的人来说,我就不知道了.因为它的语法不同于任何一门语言,所以说入门难是正常的.但 ...

  3. C# 多线程运用

    没有用过多线程,所以没有过多的了解操作原理以及怎么编写多线程 后来才只知道将一个传入的集合分别拆开为N个集合来进行使用 //分线程执行 public static void OperateThread ...

  4. html判断IE版本

    html判断IE版本 . <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> . <!--[if IE]> ...

  5. js-布尔值

    1.任何JavaScript的值都可以转换为布尔值 下面这些将会转换为false(假值): undefined null 0 -0 NaN "" //空字符串 所有其他值,包括所有 ...

  6. 关于PYTHON_EGG_CACHE无权限的问题

    Perhaps your account does not have write access to this directory? You can change the cache director ...

  7. Android WebView代理设置方法(API10~21适用)

    最近碰到个需求需要在APP中加入代理,HttpClient的代理好解决,但是WebView碰到些问题,然后找到个API10~API21都通用的类,需要用的同学自己看吧,使用方法,直接调用类方法setP ...

  8. C,C++容易被忽略的问题

    1.字符串数组,字符串指针可以直接输出 ]="I am a student"; cout<<s2<<endl; char *p="I am a s ...

  9. chrome下的js调试

    console输出对象信息:console.log(object[,object,.....])

  10. [原创]pg_shard使用场景及功能测试

    pg_shard是一个PostgreSQL的sharding extension.可以用于Shards.Replicates tables和高可用.它可以在不修改Applications的情况下无缝分 ...