Valid Palindrome
leetcode:https://oj.leetcode.com/problems/
今天A了一个Easy类型的,主要是判断一个字符串是否是回文。东平西凑的还是给弄好了,具体可看下面的要求,或者直接去网站上看也行
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.
public class Solution {
public boolean isPalindrome(String str){
char array[] = str.toCharArray();
int i = 0;
int j = array.length - 1;
boolean isPal = true;
if(str.length() == 0)
return true;
else if(str.length() == 1)
return true;
while(i <= j){
while(!isNumOrAlp(array[i++]) && i < array.length);
while(!isNumOrAlp(array[j--]) && j >= 0);
i--;
j++;
if(!isEqual(array[i], array[j]))
{
isPal = false;
break;
}
i++;
j--;
}
if(isPal)
return true; else
return false; }
public boolean isNumOrAlp(char ch){
if(ch >= '0' && ch <= '9')
return true;
else if(ch >= 'a' && ch <= 'z')
return true;
else if(ch >= 'A' && ch <= 'Z')
return true;
else
return false;
}
public boolean isEqual(char ch1,char ch2){
if(0 == ch1 - ch2)
return true;
else if(32 == Math.abs(ch2 - ch1))
return true;
else if(!isNumOrAlp(ch1) && !isNumOrAlp(ch2)){
return true;
}
else
return false;
}
}
这个还是比较简单的,所以就没注释了。很多细节的地方需要注意,oj系统给出错误信息都能很好定位问题在哪儿
Valid Palindrome的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- 25. Valid Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Valid Palindrome [LeetCode]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- 实现MySQL的Replication
实现MySQL的Replication 实现MySQL的Replication在MySQL 3.23.15版本之后,MySQL提供了数据库复制的功能,可以实现两个数据库实时同步,增强了MySQ ...
- CSS3 Filter
Filters主要是运用在图片上,以实现一些特效.(尽管他们也能运用于video上),不过我们在些只来讨论图片上的运用. 语法: elm { filter: none | <filter-fun ...
- 调试时屏蔽JavaScript库代码 –Chrome DevTools Blackbox功能介绍
代码难免会有Bug,每次我们在Chrome调试代码时,总是会进入各种各样的库代码(比如jQuery.Zepto),但实际上很多时候我们并不希望这样,要是能把这些库代码“拉黑”多好啊. 广大码农喜闻乐见 ...
- Python之路【第六篇】:socket
Python之路[第六篇]:socket Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字&quo ...
- ASP.NET验证控件二
RequiredFieldValidator 验证控件 页面布局: <div> <h1>RequiredFieldValidator 验证控件</h1> 用户名 ...
- 第一章:1-06、 试将TCP/IP和OSI的体系结构进行比较。讨论其异同之处?
<计算机网络>谢希仁著第四版课后习题答案答:(1)OSI和TCP/IP的相同点是二者均采用层次结构,而且都是按功能分层.(2)OSI和TCP/IP的不同点:①OSI分七层,自下而上分为物理 ...
- PHP中的Array
PHP中的数组是一个有序映射(1对1的关系 key->value).Array是一个综合体:可表示数组.字典.集合等. key可以是int或string.value可以是任意类型. key如下情 ...
- CSS3 column-rule-style 属性
CSS column-rule-style属性用于在多列布局中指定列与列之间通过column rule属性设置的分隔线的样式.column-rule是列与列之间的一条垂直分隔线,你可以使用column ...
- eMarketer:DMP帮广告主搞定大数据处理问题
DMP(数据管理平台)帮助广告主获得可行动的洞察 在数字广告领域,大数据和数据管理平台(DPMs)仍大有可为.DMPs让广告主可以使用他们的大数据来做出更灵活更有效的营销决策. 数据管理和分析是业界挑 ...
- thinkphp 字段静态验证$_validate中错误提醒多语言化写成{%LANGUATE}的原因
class UserModel extends Model{ protected $_validate = array( array('account', 'require', '{%LANGUAG ...