【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 ...
随机推荐
- 组件-------(一)redis系列--安装部署redis+实现redis分布式缓存 java+Spring+redis
目的:解决单机session不能共享问题,插入查询数据库时间效率问题,实现分布式缓存. 准备材料:Redis 下载链接 http://pan.baidu.com/s/1dEGTxvV 相关jar包如果 ...
- Working with forms
翻译 Django文档 Version 1.5 https://docs.djangoproject.com/en/1.5/topics/forms Working with forms 关于此文章: ...
- thinkphp框架的相关总结
参考链接地址:http://gongwen.sinaapp.com/article-205.html 1. 模板中不能使用的标签 {$content} {$i} 2. If标签 如: <if c ...
- Swift的属性,方法,下标脚本以及继承
从这篇章节起,Swift编程语言指南大部分的重要内容在于概念,代码并非太多.理解Swift的面向对象理念,语法以及类结构,构造析构过程对于非常好的应用Swift语言将会有比較大的帮助. 属性 存储属性 ...
- js获取设备信息
var su = navigator.userAgent.toLowerCase(), mb = ['ipad', 'iphone os', 'midp', 'rv:1.2.3.4', 'ucweb' ...
- adt-bundle-windows-x86_32-20140702
adt-bundle-windows-x86_32-20140702 Windows > Preference,在Android里没有NDK选项 法一:help->install new ...
- linux环境下的线程的创建问题
pthread_create函数用于创建一个线程 函数原型 #include<pthread.h> int pthread_create(pthread_t *restrict tidp, ...
- Java开发环境的基本设置
作为Java的刚開始学习的人,不知道其它的刚開始学习的人有没有和我一样的感受:用Java开发须要配置这么复杂 的环境.太难了.第一次配置时,一团混乱.Oracle监听服务打不开了,PLSql连接不上O ...
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_1
以下是一些设计略显繁琐,有必要清除思维. 下一个主要的成就,当我们点击Gobutton后,得到一个随机数骰子,是走了几步,它是基于以下步骤行走路径的数目,然后移动位置的基于角色的路径. 流程如图普遍认 ...
- 【ALearning】第四章 Android Layout组件布局(一)
在本章中,我们将Android学习组件布局.在前面的章节,我们也开始使用LinearLayout布局.然后我们在布局文件更加具体的学习和理解,会. Android的界面是有布局和组件协同完毕的,布局好 ...