LeetCode: Valid Palindrome [125]
【题目】
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.
【题意】
给定一个字符串,仅仅关注英文字母和数字,忽略其它字符。问英文字母和数字按序排列后是否构成是回文
【思路】
利用回文串推断的老办法就可以,两个指针p1, p2分别从头和尾中间扫描,推断对称位置的字符是否同样,仅仅只是须要跳过除英文字母和数字之外的其它字符。
另外还须要注意处理2中特殊情况:(1)字符串为空串,(2)字符串不是空串,但里面没有英文字母或者数字。 这两种情况都判定为true
【代码】
class Solution {
public:
bool isAlphanumeric(char c){
if(isdigit(c))return true;
if(c>='A'&&c<='Z'||c>='a'&&c<='z')return true;
return false;
}
bool isEqual(char c, char b){
if(isdigit(c))return c==b;
if(c>='A'&&c<='Z')c='a'+(c-'A');
if(b>='A'&&b<='Z')b='a'+(b-'A');
return c==b;
}
bool isPalindrome(string s) {
int len=s.length();
if(len==0)return true;
int front=0;
int back=len-1;
while(front<back){
while(front<=back && !isAlphanumeric(s[front]))front++;
while(front<=back && !isAlphanumeric(s[back]))back--;
if(front<=back){
if(!isEqual(s[front], s[back]))return false;
front++;
back--;
}
}
return true;
}
};
LeetCode: Valid Palindrome [125]的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome II 验证回文字符串之二
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [leetcode]Valid Palindrome @ Python
原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...
- LeetCode Valid Palindrome II
原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/ 题目: Given a non-empty string ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode Valid Palindrome C++&python 题解
题目描写叙述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
随机推荐
- C++ - Operator Precedence
The following table lists the precedence and associativity of C++ operators. Operators are listed to ...
- javascript (九)注释
单行注释,采用双斜杠 // 多行注释,采用 /* */
- 控制台程序的参数解析类库 CommandLine
C#控制台程序的参数解析类库 CommandLine简单使用说明 前言 C#开发的控制台程序,默认接收string[] args参数.如果有多个参数需要输入时,可以按照顺序依次输入:但如果有些参数不是 ...
- 【Demo 0015】位置服务及地图
本章学习要点: 1. 掌握位置相关类(CLLocationManager,MKUserLocation) 基本用法; 2. 掌握地图视图(MKMapView)基本用法; ...
- STL之涉及到的算法
一.非变异算法 是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理.元素查找.子序列搜索.统计和匹配.非变异算法具有极为广泛的适用性,基本上可应用与各种容器. 1查找容器元素find 它用于查 ...
- WinRarHelper帮助类
WinRarHelper帮助类 关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩 ...
- 轻松学会多线程(四)——synchronized同步keyword知多少
每个对象都有一把独占锁. 独占锁仅仅限制线程对它的同步方法的訪问,对非同步方法,独占锁没有意义. synchronizedkeyword能够作为函数的修饰符,也能够作为函数内的语句,也就是平时说的同步 ...
- c++
使用全局变量的方法多个文件
启动错误的做法 在global.h声明和定义变量 int sharedData = 9; 编译出错 Building target: CTest Invoking: GCC C++ Linker g+ ...
- POJ 1159 - Palindrome 优化空间LCS
将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文.. 问题转化为求LCS..但是n最大到5000. ...
- Android开发之Sqlite的使用
在Android中存储数据可以用文件.数据库.网络,其中文件和数据库是最常用的,数据库我们常用的就是Sqlite,它是一种经量级的.嵌入式的关系型数据库:在android中当需要操作SQLite数据库 ...