leetcode:Valid Palindrome 【Java】
2016-03-10 23:22
513 查看
一、问题描述
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
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.
二、问题分析
用Character.isLetterOrDigit方法(确定指定字符是否为字母或数字)排除格式字符的干扰。
三、算法代码
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.
二、问题分析
用Character.isLetterOrDigit方法(确定指定字符是否为字母或数字)排除格式字符的干扰。
三、算法代码
public class Solution { public boolean isPalindrome(String s) { if (s == null){ return false; } //空字符串返回为true if (s.length() == 0) return true; int head = 0, tail = s.length() - 1; s = s.toLowerCase(); while (head <= tail) { while (head < tail && !Character.isLetterOrDigit(s.charAt(head))) head++; while (head < high && !Character.isLetterOrDigit(s.charAt(tail))) tail--; if (s.charAt(head) != s.charAt(tail)) return false; head++; tail--; } return true; } }
相关文章推荐
- struts-dojo的使用
- struts2发送邮件
- SpringMVC的几种返回方式
- Spring3 MVC请求参数获取的几种方法
- LeetCode : Search in Rotated Sorted Array [java]
- SpringMVC接收复杂集合参数
- ubuntu 配置 jdk 和 maven 环境变量 sudo vim /etc/profile 后面添加
- exe4j打包java应用程序
- 唯爱小粽子:Spring中ContextLoaderListener的作用
- Java String详解
- No_16_0307 Java基础学习第十四天
- 设计模式中的代理模式与Java中的动态代理
- JAVA SE——观察者模式
- C/C++内存管理与Java内存的各种分配
- leetcode:Rotate List 【Java】
- Android开发过程中eclipse不能打开虚拟机的情况
- 【Struts2进阶】Struts2 多方法调用的两种方式
- Java 数据类型转换
- Java的反射机制
- 重识java-EnumMap