/**
* 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. windows下编译SDL1.2

    首先,官网下载开发库,我这里用的是tdm-gcc,因此下载mingw版的. 解压,写代码,编译…… 成功!...地出错了 这里提一下,编译命令是 g++ test.cpp -I include目录  ...

  2. git逻辑和基本命令

    提交和推送的区别 提交(commit):把您做的修改,保存到本地仓库中 推送(push):把您本地仓库的代码推送至服务器(一般是远程服务器及gitlab或github) 拉取和获取的区别 git  p ...

  3. ubuntu 14.04 rabbitmq集群部署

    1.准备机器,我这边准备的是三台ubuntu14.04 机器主机名不能相同,不然节点冲突 2.安装rabbitmq 3.修改hosts文件 root@abc-web-04:~# vim /etc/ho ...

  4. vue 源码学习二 实例初始化和挂载过程

    vue 入口 从vue的构建过程可以知道,web环境下,入口文件在 src/platforms/web/entry-runtime-with-compiler.js(以Runtime + Compil ...

  5. python中的单向链表实现

    引子 数据结构指的是是数据的组织的方式.从单个数据到一维结构(线性表),二维结构(树),三维结构(图),都是组织数据的不同方式. 为什么需要链表? 顺序表的构建需要预先知道数据大小来申请连续的存储空间 ...

  6. C. Neko does Maths

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Redis安装完整步骤

    安装: 1.获取redis资源 wget http://download.redis.io/releases/redis-4.0.8.tar.gz 2.解压 tar xzvf redis-4.0.8. ...

  8. windows运维如何批量远程桌面

    作用:windows下批量管理远程桌面, http://www.appmazing.com/ 官方站点  http://www.appmazing.com/files/RDO_Setup.exe wi ...

  9. [译文]Domain Driven Design Reference(七)—— 大型战略设计结构

    本书是Eric Evans对他自己写的<领域驱动设计-软件核心复杂性应对之道>的一本字典式的参考书,可用于快速查找<领域驱动设计>中的诸多概念及其简明解释. 上周末电脑硬盘文件 ...

  10. NeuChar 平台使用及开发教程(六):成为开发者

    在上一篇<NeuChar 平台使用及开发教程(五):使用 NeuChar 的关键字回复服务>中,我们已经学习了如何命中关键字来反馈特定格式内容的信息,这是由微信开发者/运营者自己来维护的信 ...