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.

原题链接:https://oj.leetcode.com/problems/valid-palindrome/

题目:给定一个字符串,检測其是否是回文串,仅仅考虑字母数字字符。

思路:过滤源字符串中的非字母数字,组成一个新串,对新串进行推断。

	public static boolean isPalindrome(String s) {
if(s.isEmpty())
return true;
//过滤字母数字之外的字符
StringBuffer buf = new StringBuffer();
for(int i=0;i<s.length();i++){
if(Character.isLetterOrDigit(s.charAt(i)))
buf.append(s.charAt(i));
}
String tmp = buf.toString().toLowerCase();
for(int i=0;i<tmp.length();i++){
if(tmp.charAt(i) != tmp.charAt(tmp.length() - i - 1))
return false;
}
return true;
}

LeetCode——Valid Palindrome的更多相关文章

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

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

  2. [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 ...

  3. [leetcode]Valid Palindrome @ Python

    原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...

  4. LeetCode Valid Palindrome II

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/ 题目: Given a non-empty string  ...

  5. Leetcode Valid Palindrome

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

  6. LeetCode: Valid Palindrome [125]

    [题目] Given a string, determine if it is a palindrome, considering only alphanumeric characters and i ...

  7. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  8. [Leetcode] valid palindrome 验证回文

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

  9. leetcode Valid Palindrome C++&amp;python 题解

    题目描写叙述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

随机推荐

  1. 【转载】ASP.NET线程安全与静态变量的生命周期浅谈

    ASP.NET线程安全所涉及的是什么呢?让我们先来看看静态变量的生命周期问题,下面是我理解的静态变量的生命周期: void Application_Start开始 void Application_E ...

  2. Android Marquee

    android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:e ...

  3. webservice调用接口,接口返回数组类型

    1. 其中sendSyncMsg1接口是方法名,Vector实现了List接口,xml是sendSyncMsg1的方法形参 Service service = new Service(); Call ...

  4. IIS7、IIS6 web.config注册HttpModule

    IIS6注册HttpModule <system.web>     <httpModules>       <add name="..." type= ...

  5. 使用Eclipse创建Hibernate工程

    创建一个java project项目,加入hibernate的jar包和数据库驱动包,并引入到项目.

  6. 第一个shell编程,输出hello world!

    在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe.它接收用户命令,然后调用相应的应用程序.--- ...

  7. 小波 mallat 算法

    算法要求:输入序列是大于滤波器长度的偶数列 确实可以通过编程的手段使算法适合所有的情况,但本文章的目的是展示mallat算法的过程,所以就一切从简了 // Mallat.cpp : Defines t ...

  8. poj3620

    #include<iostream>#include<string>#include<stack>#include<vector>#include< ...

  9. Linux 下 安装 Phalcon

    先安装GIT 然后从 git://github.com/phalcon/cphalcon.git 这里下载安装文件 编译完成就可以安装了! 编译chmod -R 777 cphalcon1. 创建从C ...

  10. HTML&CSS基础学习笔记1.30-颜色的表达

    颜色的表述 在网页中的颜色设置是非常重要,CSS的属性有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令颜色 ...