[ 问题: ]

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. 2-13. 平均两个有序序列(25)(ZJU_PAT 名单 | 排列 )

    主题链接:http://pat.zju.edu.cn/contests/ds/2-13 已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A0, A1-AN-1的中位 ...

  2. &quot;ScrollView can host only one direct child&quot;问题解决了

    1. 问题叙述性说明: (请注意以下几点大胆). ScrollView作为顶层view时报错,直接导致apk崩溃.具体错误信息例如以下: 12-21 09:12:15.150: D/AndroidRu ...

  3. gem5 设定checkpiont以及从checkpoint开始运行

    同spec2006中间bzip2一个例子,如何设置checkpoint .以及从checkpoint继续以启动运行.这样做的目的是为了,采纳automic运行N指令,然后detailed运行M指令. ...

  4. 调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法

    1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置- ...

  5. Tkinter隐藏窗口再让他显示出来的例子

    隐藏主要是 : withdraw()函数. 重新显示出来主要是: update()和deiconify()函数. 来源:http://www.blog.pythonlibrary.org/2012/0 ...

  6. hdu1828(线段树——矩形周长并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 分析:与面积不同的地方是还要记录竖的边有几个(num记录),并且当边界重合的时候需要合并(用lb ...

  7. hadoop学习;大数据集在HDFS中存为单个文件;安装linux下eclipse出错解决;查看.class文件插件

    sudo apt-get install eclipse 安装后打开eclipse,提示出错 An error has occurred. See the log file /home/pengeor ...

  8. [Leetcode]-ReverseLinkedList

    题目:单链表取反 #include <stdlib.h> #include <stdio.h> typedef struct node *list; typedef struc ...

  9. STL 源代码剖析 算法 stl_numeric.h -- copy

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie copy //唯一对外接口 /*------------------------------ ...

  10. 推测的手机型号和cpu模型

    <span style="font-size:18px;">推断手机型号:</span> <span style="font-size:18 ...