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. Python3+Selenium3自动化测试-(五)

    这里来说一说selenium中的等待方式,其实在webdriver只有两种类型等待方式,显式等待和隐式等待,之前是在程序运行过程中使用time模块中的sleep进行代码的休眠进行强制等待,是显式等待中 ...

  2. 阿里云配置mysql远程连接

    默认是不能用客户端远程连接的,阿里云提供的help.docx里面做了设置说明,mysql密码默认存放在/alidata/account.log 首先登录: mysql -u root -h local ...

  3. while小用

    1.使用while打印1 2 3 4 5 6  8 9 10 #!/usr/bin/env python #encoding: utf-8 num = 1 while num < 11: if ...

  4. Java并发(6):concurrent包中的Copy-On-Write容器

    一. concurrent包介绍 在JDK1.5之前,Java中要进行业务并发时,通常需要有程序员独立完成代码实现,而当针对高质量Java多线程并发程序设计时,为防止死蹦等现象的出现,比如使用java ...

  5. maven的相关命令

    maven的相关命令 mvn archetype:create :创建 Maven 项目 mvn compile :编译源代码(编译到target文件夹中) mvn test-compile :编译测 ...

  6. CSS小知识---回到顶部

    所需js文件 <script type="text/javascript" src="js/jquery-1.11.3.js"></scrip ...

  7. js自执行函数&扩展方法

    我们通常将JS代码写在一个单独的JS文件中,然后在页面中引入该文件.但是,有时候引入后会碰到变量名或函数名与其它JS代码冲突的问题.那么如何解决这个问题呢?作用域隔离.在JS中,作用域是通过函数来划分 ...

  8. Spring_管理 Bean 的生命周期

    beans-cycle.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&quo ...

  9. fabric安装使用

    可以使用pip安装fabric,注意使用pip 安装fabric时,一定要指定版本,不要安装2.0版本的,无论怎样都会提示没有api这样模块,所以指定安装 pip install fabric==1. ...

  10. Graph_Master(连通分量_B_Trajan+完全图)

    hdu_4635 题目大意:给出一张DAG(n个点,m条边),求出能加的最大边数,使得该图无重边,无自环,非强连通. 题解:这题题面很好理解,也没有什么很难的点,主要是如何求出最大边数需要动点脑筋.首 ...