【LeetCode】- 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.
[ 解法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(右回文)的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
随机推荐
- HBase经常使用操作之namespace
1.介绍 在HBase中,namespace命名空间指对一组表的逻辑分组,类似RDBMS中的database,方便对表在业务上划分.Apache HBase从0.98.0, 0.95.2两个版本号開始 ...
- 西南民大oj(两园交求面积)
西南民大oj:http://www.swunacm.com/acmhome/welcome.do?method=index 我的几何不可能那么可爱 时间限制(普通/Java) : 1000 MS/ 3 ...
- HTML5之画布的拖拽/拖放
<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> func ...
- Kafka - SQL 引擎
Kafka - SQL 引擎分享 1.概述 大多数情况下,我们使用 Kafka 只是作为消息处理.在有些情况下,我们需要多次读取 Kafka 集群中的数据.当然,我们可以通过调用 Kafka 的 AP ...
- Nlog配置文件
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nl ...
- 通过 Jersey Http请求头,Http响应头,客户端 API 调用 REST 风格的 Web 服务
原地址:http://blog.csdn.net/li575098618/article/details/47853263 Jersey 1.0 是一个开源的.可以用于生产环境的 JAX-RS(RES ...
- hdu1754(splay)
给n个数,有两种操作 Q a b 询问区间[a,b]的最大值, U a b 将第a个数的值改成b splay树的中序遍历是我们所维护的序列.如果要询问区间[a,b]的最大值,那么只要将第a ...
- python学习之print输出不换行
print的即时打印会导致换行,要使得print的输出不换行,可以在字符串或者变量后面加个逗号(“,”),如下: s = "A bird in the hand..." for c ...
- 王立平--Program Files (x86)
window7根据系统.program files(x86) 它是应用程序目录,在64下位系统.为了更好的相容性32位程序,在一些安装32位程序(请注意,有些节目自己是32位),将默认被安装progr ...
- Mac maven环境变量配置
近期一直在学习使用Macbook,在这里记录一下全部遇到的问题 问题起源: 1.Macbook 安装了Eclipse,Eclipse装入插件maven & git , 可是在git中clone ...