/**
* Source : https://oj.leetcode.com/problems/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.
*
* 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.
*
*
*/
public class ValidPalindrome { /**
* 判断一个字符串是否是回文字符串,只判断字符串里面的字母和数字
*
* @param str
* @return
*/
public boolean valid (String str) {
int left = 0;
int right = str.length()-1;
while (left <= right) {
// 过滤左边非数字字母
while (left < str.length() && !isAlphanumeric(str.charAt(left))) {
left++;
}
// 过滤右边非数字字母
while (0 < right && !isAlphanumeric(str.charAt(right))) {
right--;
}
if (left >= right) {
return true;
}
if (Character.toLowerCase(str.charAt(left++)) != Character.toLowerCase(str.charAt(right--))) {
return false;
}
}
return true;
} public boolean isAlphanumeric (char c) {
if ((c >= 48 && c <= 57) || ((c >= 65 && c <= 90) || (c >= 97 && c<= 122))) {
return true;
}
return false;
} public static void main(String[] args) {
ValidPalindrome validPalindrome = new ValidPalindrome();
System.out.println(validPalindrome.valid("A man, a plan, a canal: Panama") + "-----true");
System.out.println(validPalindrome.valid("race a car") + "-----false");
} }

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

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

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

  4. [leetcode]Valid Palindrome @ Python

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

  5. LeetCode Valid Palindrome II

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

  6. Leetcode Valid Palindrome

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

  7. LeetCode: Valid Palindrome [125]

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

  8. LeetCode: Valid Palindrome 解题报告

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

  9. [Leetcode] valid palindrome 验证回文

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

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

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

随机推荐

  1. django 常见错误汇总

    File "D:\python\django\mysite\mysite\view.py", line 7 SyntaxError: (unicode error) 'utf-8' ...

  2. MyBatis3系列__05查询补充&resultMap与resultType区别

    1.查询补充 当你查询一条记录并且是简单查询时,情况相对简单,可以参考以下的例子: public Employee getEmpById(Integer id); 对应的xml文件中: <sel ...

  3. vs2010 sp1 安装Silverlight5 语言版本不匹配的问题

    好久之前用silverlight写了个程序,今天心血来潮想给朋友看一下,朋友更新了sl5,但是运行不起来. 所以有点郁闷,于是打算更新项目到silverlight5. 装sp1后,下载silverli ...

  4. centos为docker配置加速器

    国内拉去docker镜像慢得可怜,为了解决这个问题,可为docker配置加速器. 1.修改daemon配置文件 sudo mkdir -p /etc/dockervim /etc/docker/dae ...

  5. Hive 本地调试方法

    关键词:hive, debug 本地调试(local debug) Hive 可分为 exec (hive-exec,主要对应源码里的ql目录) 和 metastore 两部分,其中exec对外有两种 ...

  6. [LeetCode] Score After Flipping Matrix 翻转矩阵后的分数

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

  7. PHP环境在7以上的项目报错A non-numeric value encountered

    报错如下图: 解决办法: 在相对应的报错控制器层加入一行代码,需加载控制器上方,代码如下:   ini_set("error_reporting","E_ALL & ...

  8. 32 ArcToolBox学习系列之数据管理工具箱——属性域(Domains)的两种创建及使用方式

    属性域分为两类,一种是范围域,一种是编码的值,下面将两个一起介绍,其中涉及到的编码,名称,只是试验,并非真实情况. 一.首先新建一个文件型地理数据库,将数据导入或者是新建要素类都可以 二.打开ArcT ...

  9. MVVM简介与运用

    在介绍MVVM框架之前,先给大家简单介绍一下MVC.MVP框架(由于本博文主要讲解MVVM,所以MVC和MVP将简化介绍,如果需要我将在以后的博文中补充进来). MVC框架: M-Model : 业务 ...

  10. boa调试

    Cannot access memory at address 0x0 0x400fc7e0 in ?? () 0 0x4014f0dc in wcscasecmp_l () from /lib/li ...