【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 ...
随机推荐
- php7对redis的扩展及redis主从搭建
这两天在学习一下php7下面的安装及redis相关配置认识.并将笔记记下来.以备后用.主要涉及到redis的扩展php-redis 及redis主从的配置. 一:redis安装 1:下载并安装 ...
- .NET Web开发总结(五)
7 常用服务器控件 7.1 服务器控件概述 · 服务器控件是指在服务器上执行程序的代码的组件 通常这些服务器控件会提供 给用户一定的界面, 以便用户与服务器之间快速的交互 7.2 HTML 服 ...
- PHPCMS建站经验分享
在这里不对模型.模板设置.category,list,show等静态页面引入.配置文件(caches\configs\database.php 和 caches\configs\system.php) ...
- MYSQL 管理笔记(一)
一.查看MySQL 版本 1.在终端下:mysql -V 2.在mysql中:mysql> status; 3:在help里面查找 mysql --help | grep Distr ...
- 共享内存shared pool (6):追踪sql语句
构建实验数据 --使用NC50用户查询(会话1) SQL> conn NC50/NC50 Connected. SQL> create table emp as select * from ...
- MVC的Filters(拦截过滤)的Error页面,支持Ajax报错
报错拦截过滤到error页面 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, A ...
- 浅析python的string.Template
摘自:python参考手册. string模块定义了一种新字符串类型Template,简化了特定的字符串置换操作, Template定义一个类 1.template(s), #s是字符串 s='he ...
- python encode decode unicode区别及用法
decode 解码 encode 转码 unicode是一种编码,具体可以百度搜 # coding: UTF-8 u = u'汉' print repr(u) # u'\u6c49' s = u.en ...
- oracle-审计导数
1.因审计需求,需要将MySQL.Oracle数据库中需要的表数据导入到SqlSERVER进行审计. 2.之前的方法: A. oracle组将表dump下来,进行压缩,传送到oracle导数服务器 ...
- RMAN 报:ORA-19504 ORA-27038
在itpub中看到下面的问题: oracle 10g备份脚本如下run{allocate channel d1 device type disk MAXPIECESIZE=100M;crosschec ...