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 ...
随机推荐
- javaSE第十六天
第十六天 140 1:List的子类(掌握) 140 (1)List的子类特点 140 (2)ArrayList 141 A:没有特有功能需要学习 141 B:案例 ...
- [转]10分钟入门python
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为Rocrocket Wu. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体 ...
- 软件工程 speedsnail 冲刺8
完成任务:蜗牛移动的界面调整,速度调整: 遇到问题: 问题1 进入关卡的开始蜗牛会跳一段距离 解决1 线程延迟3S: 明日任务: 划线实现挡板功能
- Clearing Search Values
Use the SearchClear() method which is part of the Field class to clear search values on a particul ...
- 【转】详解JavaScript中的this
ref:http://blog.jobbole.com/39305/ 来源:foocoder 详解JavaScript中的this JavaScript中的this总是让人迷惑,应该是js众所周知的坑 ...
- JS组件系列——KnockoutJS用法
前言:出于某种原因,需要学习下Knockout.js,这个组件很早前听说过,但一直没尝试使用,这两天学习了下,觉得它真心不错,双向绑定的机制简直太爽了.今天打算结合bootstrapTable和Kno ...
- 第六章Linux的文件权限与目录配置
一.Linux用户分类 1.Linux用户分为:(文件|目录)所有者(OWN),(同组内的)用户组,其他人; 2.一个天神:root;,几乎能完成任何事.... 二.目录权限的意义 目录的权限和文件的 ...
- jqueryMobile应用第一课《构建跨平台APP:jQuery Mobile移动应用实战》连载一(Hello World)
有人说每个程序员都曾经有过改变世界的梦想,笔者认为,这与程序员年轻时编写的第一个程序有着莫大的关系.简简单单的一句“hello world”让年轻的心开始相信梦想,用一种低调的壮志凌云向世界展示自己的 ...
- 《Prism 5.0源码走读》Service Locator Pattern
在Prism Bootstrapper里面取实例的时候使用 ServiceLocator模式,使用的是CommonServiceLocator库 (http://commonservicelocato ...
- springMVC+jpa配置之简单案例
搭建springMVC+jpa的亲身经历,看着网上的博客,自己摸索着搭建框架结果错误一大堆.现在把流程走一遍,方便以后查看. 其中我遇到这样的一个问题:直接启动tomcat运行保存实体能通过,但是通过 ...