/**
* 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. 3. Linux系统磁盘分区介绍

    1. 磁盘分区基本知识 1)磁盘在使用前一般要先分区(相当于建房子要分房间一样). 2)磁盘分区一般有主分区.扩展分区和逻辑分区之分.一块磁盘最多可以有4个主分区,其中一个主分区的位置可以用一个扩展分 ...

  2. jQuery各类选择器

  3. 如何实现文件上传 - JavaWeb

    直接上代码 ( idea 开发,SpringBoot 框架 ): 首先是Controller的写法: package com.xxx.Controller; import com.xxx.Tools. ...

  4. mac本webstrom破解

    之前忙着加班一直没搞,有时间解决一下 首先编辑hosts文件 https://jingyan.baidu.com/article/f3ad7d0f55154309c3345bdd.html Mac系统 ...

  5. 使用poi读取Excel文件数据

    package com.haiyisoft.iecp.util; import java.io.File;import java.io.FileInputStream;import java.io.F ...

  6. FCC(ES6写法) Make a Person

    用下面给定的方法构造一个对象. 方法有 getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(l ...

  7. Mesos源码分析(1): Mesos的启动过程总论

  8. [转]Understanding OpenStack Authentication: Keystone PKI

    The latest stable release of OpenStack, codenamed Grizzly, revolutionizes the way user authenticatio ...

  9. SSL/TLS握手过程

    ----------------------------------专栏导航----------------------------------HTTPS协议详解(一):HTTPS基础知识 HTTPS ...

  10. [Swift]LeetCode150. 逆波兰表达式求值 | Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...