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. 000 初步使用Kotlin开发Android应用

    Kotlin是Jetbrians公司开发的一款编程语言,基于jvm兼容Java. 要求 IDE:IDEA或者Android Studio(简称studio)对Kotlin语言有所了解,官方文档:htt ...

  2. SqlAlchemy基本

    安装SQLAlchemy: $ easy_install sqlalchemy 数据库表是一个二维表,包含多行多列 [ ('1', 'Michael'), ('2', 'Bob'), ('3', 'A ...

  3. python基础(数字、字符串、布尔值、字典数据类型简介)

    一 执行第一个python程序 1.下载安装python2.7和python3.6的版本及pycharm,我们可以再解释器中输入这样一行代码: 则相应的就打出了一句话.这里的print是打印的意思.你 ...

  4. Matplot相关(一)

    ——————————缩写定义—————————— import matplotlib.pyplot as plt import matplotlib as mpl ——————————函数解析———— ...

  5. class_options

    一.class_option :stylesheet, :type => :boolean, :default => "true", :description => ...

  6. HDU - 6406 Taotao Picks Apples (RMQ+dp+二分)

    题意:N个高度为hi的果子,摘果子的个数是从位置1开始从左到右的严格递增子序列的个数.有M次操作,每次操作对初始序列修改位置p的果子高度为q.每次操作后输出修改后能摘到得数目. 分析:将序列分为左.右 ...

  7. js踩过的一些坑

    参考我的博客:http://www.isedwardtang.com/2017/08/29/js-bug/

  8. shell文件安全与权限 笔记

    主要学习: 文件盒目录的权限 Setuid Chown和chgrp Umask 连接符号 一个文件已经创建,就具有三种访问方式 读,可以显示该文件的内容 写,可以编辑或删除它 执行,如果该文件时一个s ...

  9. CAS单点登录的配置

    先说单点登录是个啥? 单点登录主要用于多系统集成,即在多个系统中,用户只需要到一个中央服务器登录一次即可访问这些系统中的任何一个,无须多次登录. 配置的步骤如下: 1.生成安全证书 Cas serve ...

  10. 【Flask】Flask常用信号

    使用信号分为3步,第一是定义一个信号,第二是监听一个信号,第三是发送一个信号. 1. 定义信号:定义信号需要使用到blinker这个包的Namespace类来创建一个命名空间.比如定义一个在访问了某个 ...