[ 问题: ]

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.

[ 解法1: ]

先把有效的字母、数字准备好,然后遍历目标字符串,有效的字符放入buffer。

再比較buffer中的字符串和反转后的字符串,假设同样那就是回文。否则不是回文。

点评:这个解法能够被系统accept,但调用了太多api,性能一般。

public class Solution {

	public boolean isPalindrome(String s) {
StringBuilder buffer = new StringBuilder();
String tempStr = "abcdefghijklmnopqrstuvwxyz0123456789";
char[] cArr = s.toCharArray();
for (int i = 0; i < cArr.length; i++) {
if (tempStr.contains(String.valueOf(cArr[i]).toLowerCase())) {
buffer.append(String.valueOf(cArr[i]).toLowerCase());
}
}
String currentStr = buffer.toString();
String reverseStr = buffer.reverse().toString();
if (currentStr.equals(reverseStr)) {
return true;
}
return false;
} }

[ 解法2: ]

採用二分法,一个指针从左边遍历,一个从右边遍历,跳过非字母和非数字,当遍历到中点依旧同样那就是回文

点评:代码清晰。性能较好。

public class Solution {

	/**
* 推断是否是回文
*
* @param String str
* @return boolean true(is palindrome)/false(is not palindrome)
*/
public boolean isPalindrome(String s) {
if (s == null) {
return false;
} int i = 0;
int j = s.length() - 1;
while (i < j) {
if (!isAlphanumeric(s.charAt(i))) {
i++;
continue;
}
if (!isAlphanumeric(s.charAt(j))) {
j--;
continue;
}
if(Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(s.charAt(j))){
i++;
j--;
continue;
}
return false;
}
return true;
} /**
* 推断是否是字母或数字
*
* @param char character
* @return boolean true(is alphanumeric) / false(is not alphanumeric)
*/
public boolean isAlphanumeric(char c) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
return true;
}
return 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 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  3. [Leetcode] valid palindrome 验证回文

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

  4. [LeetCode] 125. Valid Palindrome 验证回文字符串

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

  5. [leetcode]125. Valid Palindrome判断回文串

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

  6. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

  7. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  9. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  10. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

随机推荐

  1. 黑马程序猿_ 利用oc的协议实现代理模式

    先说下代理模式是什么吧 定义: 为其它对象提供一种代理以控制对这个对象的訪问.在某些情况下,一个对象不适合或者不能直接引用还有一个对象 而代理对象能够在client和目标对象之间起到中介的作用. 在看 ...

  2. HDU 3639 Hawk-and-Chicken(良好的沟通)

    HDU 3639 Hawk-and-Chicken 题目链接 题意:就是在一个有向图上,满足传递关系,比方a->b, b->c,那么c能够得到2的支持,问得到支持最大的是谁,而且输出这些人 ...

  3. python实用小代码

    栈的实现 #!/usr/bin/env python #coding=utf-8 #python version 2.7.4 class stack: def __init__(self,list=N ...

  4. hdu3555(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意:求区间[a,b]内包含有'49'的数的总个数. 分析:dp[pos][0]表示到第pos位 ...

  5. 重温delphi之控制台程序:Hello World!

    原文:重温delphi之控制台程序:Hello World! 这二天用c#开发ActiveX时,发现不管怎么弄,c#就是没办法生成ocx的纯正activeX控件,而且还要强迫用户安装巨大的.net f ...

  6. 六:Java之集合

    集合包括的内容非常多,我发现一个非常好的博客,感觉自己都没有写的必要了! 链接献上  Java集合

  7. Jetty开发指导:Jetty Websocket API

    Jetty WebSocket API使用 Jetty提供了功能更强的WebSocket API,使用一个公共的核心API供WebSockets的服务端和client使用. 他是一个基于WebSock ...

  8. 什么是IT

    这个是同事总结的,我补充了若干项,算不上原创,但这个没有在其他地方看到,在这儿权且当原创了.后面再配个软件架构图吧.看到缺的同学能够补充 什么是IT:Information-信息Technology- ...

  9. 项目架构mvc+webapi

    mvc+webapi 项目架构 首先项目是mvc5+webapi2.0+orm-dapper+ef codefirst. 1.项目框架层次结构: 这个mvc项目根据不同的业务和功能进行不同的区域划分, ...

  10. spring.net中间IoC、DI和MVC

    轮廓 spring.net它是开源的业务层框架,功能很强大,它归结到什么都有3能:面向切面编程:IoC和DI:提供综合型的框架支持,本片博客主要说一下IoC和DI.和其提供的对MVC框架的支持. Io ...