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的更多相关文章

  1. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  3. Leetcode Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  6. 25. Valid Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  7. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. Valid Palindrome [LeetCode]

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

  10. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. Windows server 2008 R2远程桌面终端连接数的破解

    Windows server 2008 R2远程桌面终端连接数的破解 日常工作中,经常需要远程连接到服务器上,然而在公司里,老总.同事都需要连接到服务器上,而默认的服务器系统同时连接的最大连接数只有2 ...

  2. 022 ARM寄存器详解

    R13:堆栈指针寄存器 SP R14:链接寄存器 LR R15:程序计数器 PC指针 CPSR:当前程序状态寄存器 SPSR:备份程序状态寄存器

  3. Android IOS WebRTC 音视频开发总结(十一)-- stun&turn部署

    本篇文章主要介绍webrtc里面的stun,turn服务的安装与配置(转载请说明出处: http://www.cnblogs.com/lingyunhu, RTC.Blacker) 说到STUN,TU ...

  4. c# winfrom应用程序关闭任务管理器中的进程

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Configuring Report Manager

     Steps to configure and get Reports in Report manager. 1. Enable getting Reports in Report Manager. ...

  6. 关于Ajax跨域

    本人因工作需求,编写了一个测试页面,在页面填写完信息之后去向一个站点请求数据,然后返回结果!一开始是直接用Ajax在脚本中去访问,没有大碍(因为目标地址是本机上的一个网站),但是当站点去外部的网站时, ...

  7. 别不拿里程碑当石头---------IT项目管理之项目计划(转)

    如果说做项目不需要计划,恐怕没人会认同.是否每个项目计划都起到了作用呢?却不尽然.知道要做计划,但不知道为什么做计划,如何做计划的还是大有人在.所以很多计划沦为依样画葫芦,成了摆设. IT项目计划的用 ...

  8. Lambda Grinding Miller From Zenith

    data = """ The Basic Things About Grinding Mill A grinding mill is a unit operation d ...

  9. WebForm与MVC混用

    步骤一:添加引用 -> 程序集 -> 扩展 -> System.Web.Mvc ; System.Web.Razor; System.Web.WebPages; System.Web ...

  10. xtrabackup之Innobackupex全备数据库

    一.Xtrabackup是什么: Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品. ...