/**
* 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. [R]R包版本更迭【持续更新】

    由于R版本更迭,网上或旧的教材上的包可能没有在维护,或者被其他包替代. 做一个表记录碰到的一些替代方案.个人向,非完整指南. * mvpart 2014年之后不再更新,R 3.0版本后无法安装, 提示 ...

  2. C++入门笔记(一)零碎基础知识

    零碎基础知识 一.创建和运行程序 1.使用文本编辑器编写程序,保存为文件,该文件就叫源代码. 2.编译源代码:运行一个程序,将源代码翻译为主机使用的内部语言----机器语言.包含了 编译后程序的文件就 ...

  3. docker pull 镜像报错

    [root@localhost ~]# docker pull ningx Using default tag: latest Trying to pull repository docker.io/ ...

  4. Java课程之团队开发(团队介绍)

    一.介绍团队和团队成员 团队名称:凯域软创 团队成员介绍:张某某,崔某某,焦某某,陈某 二.关于团队作品 1.作品名称:课程表 2.你的创意解决了用户的什么需求:查看课程信息的需求 3.你有什么招数用 ...

  5. ARouter基础使用(一)

    一个用于帮助 Android App 进行组件化改造的框架 —— 支持模块间的路由.通信.解耦1.新建一个Android项目 "ARouterDemo"2.添加依赖和配置 andr ...

  6. 你不知道的JS之作用域和闭包(四)(声明)提升

    原文:你不知道的js系列 先有鸡还是先有蛋? 如下代码: a = 2; var a; console.log( a ); 很多开发者可能会认为结果会输出 undefined,因为 var a 在 a ...

  7. JS 多选文件或者选择文件夹

    <%--文件多选--%> <input type="file" name="file" id="file" multipl ...

  8. qt5.4解决输出中文乱码问题

    需要在字符串前添加 QString::fromUtf8 例: b2 = new QPushButton(this); b2->setText(QString::fromUtf8("变化 ...

  9. PostgreSQL+PostGIS 的使用

    一.PostGIS中的几何类型 PostGIS支持所有OGC规范的“Simple Features”类型,同时在此基础上扩展了对3DZ.3DM.4D坐标的支持. 1. OGC的WKB和WKT格式 OG ...

  10. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...