Valid Palindrome

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.

验证回文与否,这个估计大家学c语言的时候就都写过:

 class Solution {
public:
bool isPalindrome(string s) {
int len = s.length();
for(int i = , j = len - ; i < j ;++i, --j){
while(i < j && (!isalpha(s[i]) && !isdigit(s[i])))
i++;
while(j > i && (!isalpha(s[j]) && !isdigit(s[j])))
j--;
if(isalpha(s[i]) && isalpha(s[j])){
if(toupper(s[i]) == toupper(s[j]))
continue;
else return false;
}else{
if(s[i] == s[j])
continue;
return false;
}
}
return true;
}
};

java版本的代码如下所示,算法没有变化:

public class Solution {
public boolean isPalindrome(String s) {
int sz = s.length();
int beg = 0, end = sz - 1;
while(beg < end){
while(!Character.isLetter(s.charAt(beg)) && !Character.isDigit(s.charAt(beg))){
if(beg < end) beg++;
else return true;
}
while(!Character.isLetter(s.charAt(end)) && !Character.isDigit(s.charAt(end))){
if(beg < end) end--;
else return true;
}
if(Character.toUpperCase(s.charAt(beg)) == Character.toUpperCase(s.charAt(end))){
beg++;
end--;
}else{
return false;
}
}
return true;
}
}

LeetCode OJ:Valid Palindrome(验证回文)的更多相关文章

  1. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. [Leetcode] valid palindrome 验证回文

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. 125 Valid Palindrome 验证回文字符串

    给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...

  7. LeetCode 125. Valid Palindorme (验证回文字符串)

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  9. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  10. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

随机推荐

  1. django重写用户模型

    重写一个UserProfile继承自带的AbstractUser # -*- coding: utf-8 -*- from __future__ import unicode_literals fro ...

  2. Read Large Files in Python

    I have a large file ( ~4G) to process in Python. I wonder whether it is OK to "read" such ...

  3. mybatis参数处理 $#

  4. hdu4749 kmp应用

    呃,从网上看的题解,然而其实有点地方还没搞懂,先放在这,以后再回来理解. 题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题目:2013 is ...

  5. cdoj1337郭大侠与阴阳家

    地址:http://acm.uestc.edu.cn/#/problem/show/1337 思路: 郭大侠与阴阳家 Time Limit: 3000/4000MS (Java/Others)     ...

  6. nodejs多核处理

    前言大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,有人开发了第三方的cluster,让node可以利用多核CPU实现并行. 随着nod ...

  7. git指令整理汇总

    Git 1.git init 创建版本库,初始化 2.git add  向git添加文件,把文件添加到版本库 3.git log   告诉我们历史记录 4.git commit -m ‘’ 提交修改 ...

  8. 在Ubuntu上安装Brackets的步骤(加源和移除源)

    学习编写接口开发和测试时需要用到编写HTML页面,在Windows下采用的是HBuilder,但是无Ubuntu版本.安装一个网上推荐的Brackets 安装步骤:参见http://www.xiton ...

  9. 如何评价一个pipeline的好坏

    生物信息NGS相关软件众多. 常用的比对软件:bwa,bowtie: 去pcr重复的软件\:samtools,picard: calling variant:samtools/bcftools,gat ...

  10. ubuntu linux 1604 编译安装tesseract-ocr 4.0

    主要参考官方的编译,梳理一下整个流程 Linux The build instructions for Linux also apply to other UNIX like operating sy ...