【Valid Palindrome】cpp
题目:
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的更多相关文章
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Valid Parentheses】cpp
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 【Valid Number】cpp
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Sudoku Solver】cpp
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- 【Gray Code】cpp
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
随机推荐
- 用Java实现一个堆排序
堆可以看成是一个完全二叉树,而且非终端节点的值均不大于(不小于)其左右孩子节点的值.堆排序只需要一个记录大小的辅助空间,输出堆顶的值之后需要对堆进行调整建立新堆,找到剩下节点的最大值(最小值),反复执 ...
- 为什么for in循环不适合用于数组
首先一点无关的,使用(var i in a) 而不是( i in a),除非你想创建全局变量. 第二点,for in 循环会忽略空的数组 var a = []; a[5] = 5; // Perfec ...
- mouseover,mouseout,mouseenter,mouseleave的区别
1.前言 今天下午参加一个面试,对方问我写不写博客,这时候才猛然意识到好久没写东西了.最近一直在外边实习,每天有很多经历和挑战,但是却没有及时地记录下来,这一点必须得批评自己,以后得经常把自己遇到的问 ...
- linux下怎么编译运行C语言程序?
linux下的C语言编译器是gcc,C++的编译器是g++. linux下编程可以使用编辑器vi或vim,建议使用vim,因为它有语法高亮显示.程序编写好后,假设你的程序名为test.c,可以使用gc ...
- Knockout.Js官网学习(简介)
前言 最近一段时间在网上经常看到关于Knockout.js文章,于是自己就到官网看了下,不过是英文的,自己果断搞不来,借用google翻译了一下.然后刚刚发现在建立asp.net mvc4.0的应用程 ...
- HTML5 对于手机页面长按会粘贴复制的禁用 (解决方案)
解决方案: 直接在CSS 文件中添加下面的代码,就可以实现了在手机端禁止粘贴复制的功能: *{ -webkit-touch-callout:none; /*系统默认菜单被禁用*/ -we ...
- js 将json字符串转换为json兑现
在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如:JSON字符串:var str1 = '{ &quo ...
- jqGrid根据ID获取行号
根据行号获取ID $('#grid').getCell(rownumber,'id') 根据ID获取行号 $('#' + rowid)[0].rowIndex
- ok6410的madplay配置
二.移植嵌入式播放器 madplay madplay 播放器程序主要依赖于如下库: zlib zlib-1.1.4.tar.gz 提供数据压缩用的函式库 libid3tag libid3tag- ...
- python zip函数介绍
首先用help(zip)来看一下帮助文档: