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 ...
随机推荐
- 深入研究Clang(四) Clang编译器的简单分析
作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...
- KDB支持单步调试功能(ARM架构)
0 实践发现KDB不支持step调试功能 (本文针对的是arm CotexA9架构,各种架构的实现方式不一样, X86的好像已经支持,不过本人没有验证过) 1 首先看下要调试的代码段 ...
- IT忍者神龟之中的一个句sql语句——连接同一字段的全部值
Oracle能够用SYS_CONNECT_BY_PATH字符串聚合函数: SELECT LTRIM(MAX(SYS_CONNECT_BY_PATH(productname, ', ')), ', ') ...
- android平台中,EventBus研究学习
当一个Android应用功能越来越多的时候.app中各个部分之间通信.往往採用Observer的方式来进行,即注冊----通知----注销的方式运行 各类控件常常须要依据某个状态来更 ...
- CF 258B Little Elephant and Elections [dp+组合]
给出1,2,3...m 任取7个互不同样的数a1,a2,a3,a4,a5,a6,a7 一个数的幸运度是数位上4或7的个数 比方244.470幸运度是2. 44434,7276727.4747,7474 ...
- [精读]Spationtemporal Saliency Detection Using Textural Contrast and Its Applications
Spationtemporal Saliency Detection Using Textural Contrast and Its Applications Last Edit 2013/12/3 ...
- 浙江大学PAT上机题解析之2-06. 数列求和
给定某数字A(1<=A<=9)以及非负整数N(0<=N<=100000),求数列之和S = A + AA + AAA + … + AA…A(N个A).例如A=1, N=3时,S ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- MySQL如何修改root密码
MySQL修改用户密码 因为长期不登录MySQL数据库,登录时经常忘记root权限密码.本文提供一个在数据库服务器上修改root密码的方法,本文撰写基础是在xp操作系统下进行. 第一步 ...
- table显示边框问题,隐藏行线,列线
只显示上边框 <table frame=above> 只显示下边框 <table frame=below> 只显示左.右边框 <table frame=vsides> ...