【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 ...
随机推荐
- 创建带sshd服务的docker image
参考:https://docs.docker.com/examples/running_ssh_service/ 1. 创建一个空目录用于存放Dockerfile mkdir -p /home/thm ...
- hadoop-2.7.1基于QMJ高可用安装配置
1.修改主机名及hosts文件 10.205.22.185 nn1 (主)作用namenode,resourcemanager,datanode,JournalNode,zk,zkfc(hive,sq ...
- 导出数据库数据制成Excel和txt
引用ICSharpCode.SharpZipLib.dll 1.编写压缩和解压代码 using System; using System.Collections.Generic; using Syst ...
- 服务器下自动备份MySQL
Linux下 service crond restart */ * * * * /home/mysqlbackup.sh >/home/runssh.log backup.sh #bin/bas ...
- 前端javascript发送ajax请求、后台书写function小案例
HTML端页面: <td> <input class="pp_text" type="text" name="" valu ...
- 从PC跳转至wap
<script language="JavaScript">function mobile_device_detect(url){var thisOS=navigato ...
- ASP.NET MVC5学习笔记之Controller执行ControllerDescriptor和ActionDescriptor
一. ControllerDescriptor说明 ControllerDescriptor是一个抽象类,它定义的接口代码如下: public abstract class ControllerDes ...
- Delphi 7 里没有加载的控件
在原来版本如D5.D6中使用的控件如Quickrep,FastNet等,在D7中仍然是保留的.只是Delphi没有将他们默认的安装到组件面版中来.这些控件包全部保存在Delphi目录的bin下,文件扩 ...
- DevExpress 使用 XtraTabbedMdiManager 控件以 Tab样式加载 Mdi窗体并合并 RibbonControl 解决方案
最近刚接触到 DevExpress 13.1 这个皮肤组件, 觉得相当好用 于是开始准备搭建 个小应用的主体框架. 找了好久的就是没找到对应的文章来讲解这一块.. 翻了他们主网站上人家问的,以及API ...
- Python之Flask Web开发
下载python包管理工具Pip: 访问网址:https://pip.pypa.io/en/stable/installing/ 下载文件get-pip.py到本地计算机 定位到get-pip. ...