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. go——数组(二)

    1.内部实现 在Go语言里,数组是一个长度固定的数据类型,用于存储一段具有相同的类型的元素的连续块. 数组存储的类型可以是内置类型,如整型或字符串,也可以是某种结构类型. 灰格子代表数组里面的元素,每 ...

  2. svn / git SourceTree

    开发使用SourceTree 忽略文件这块老弄错,这次专门博客一下,使用CocoaPods 开发项目, 忽略步骤如下:  忽略文件内容如下 *.xcworkspace xcuserdata *.loc ...

  3. Linux mount/unmount命令(6/16)

    linux是一个优秀的开放源码的操作系统,可以运行在大到巨型小到掌上型各类计算机系统上,随着 linux系统的日渐成熟和稳定以及它开放源代码特有的优越性,linux在全世界得到了越来越广泛的应用.现在 ...

  4. 32位JDK和64位JDK

    32位和64位系统在计算机领域中常常提及,但是仍然很多人不知道32位和64位的区别,所以本人在网上整理了一些资料,并希望可以与大家一起分享.对于32位和64位之分,本文将分别从处理器,操作系统,JVM ...

  5. JavaWeb请求中文乱码

    解决中文乱麻问题,页面端发出的数据作两次encodeURI var name="张三"; encodeURI(encodeURI(name)); 后台解码: URLDecoder. ...

  6. ssi include返回404页面

    项目中index.html中包含<!--#include virtual="/commonfrag/djdzkan/recomm_www_info.inc"  --> ...

  7. 构造函数与super

    1. 当不定义构造方法,系统会为类隐式的创建一个空的无参构造方法 2. 当类定义了有参的构造方法,系统就不会为类创建无参构造方法 3. 子类中,若不显式调用super(), super()会被隐式调用 ...

  8. 20145201 《Java程序设计》第四周学习总结

    20145201 <Java程序设计>第四周学习总结 教材学习内容总结 本周学习了课本第六.七章内容,即继承与多态.接口与多态. 第六章 继承与多态 6.1 何谓继承 6.1.1 继承共同 ...

  9. 20145109《Java程序设计》第二周学习总结

    20145109 <Java程序设计>第二周学习总结 教材学习内容总结 Variable : naming rule : Camel case no default value e.g : ...

  10. [Usaco2008 Open]Word Power 名字的能量

    1622: [Usaco2008 Open]Word Power 名字的能量 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 408  Solved: 19 ...