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.

SOLUTION 1:

左右指针往中间判断。注意函数: toLowerCase

 /*
SOLUTION 1: Iterator.
*/
public boolean isPalindrome1(String s) {
if (s == null) {
return false;
} int len = s.length(); boolean ret = true; int left = 0;
int right = len - 1; String sNew = s.toLowerCase(); while (left < right) {
// bug 1: forget a )
while (left < right && !isNumChar(sNew.charAt(left))) {
left++;
} while (left < right && !isNumChar(sNew.charAt(right))) {
right--;
} if (sNew.charAt(left) != sNew.charAt(right)) {
return false;
} left++;
right--;
} return true;
} public boolean isNumChar(char c) {
if (c <= '9' && c >= '0' || c <= 'z' && c >= 'a' || c <= 'Z' && c >= 'A') {
return true;
} return false;
}

SOLUTION 2:

引自http://blog.csdn.net/fightforyourdream/article/details/12860445 的解答,会简单一点儿。不用判断边界。

左右指针往中间判断。新技能GET: isLetterOrDigit

 /*
SOLUTION 2: Iterator2.
*/
public boolean isPalindrome(String s) {
if (s == null) {
return false;
} int len = s.length(); boolean ret = true; int left = 0;
int right = len - 1; String sNew = s.toLowerCase(); while (left < right) {
// bug 1: forget a )
if (!Character.isLetterOrDigit(sNew.charAt(left))) {
left++;
// bug 2: Line 67: error: cannot find symbol: method isLetterOrDigital(char)
} else if (!Character.isLetterOrDigit(sNew.charAt(right))) {
right--;
} else if (sNew.charAt(left) != sNew.charAt(right)) {
return false;
} else {
left++;
right--;
}
} return true;
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IsPalindrome_2014_1229.java

LeetCode: Valid Palindrome 解题报告的更多相关文章

  1. 【LeetCode】125. Valid Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 列表生成式 正则表达式 双指针 日期 题目地址:https:/ ...

  2. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  3. LeetCode: Valid Sudoku 解题报告

    Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...

  4. LeetCode: Valid Number 解题报告

    Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  5. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  6. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. [leetcode]Valid Palindrome @ Python

    原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...

  8. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

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

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

随机推荐

  1. build high performance server 转载

    http://blog.ci123.com/wobushizhanghua/entry/246311 先后查看了haproxy,l7sw和lighttpd的 相关源码,无一例外,他们一致认为多路复用是 ...

  2. DHCP工作过程的六个主要步骤

    对于学习DHCP来说,很重要的一部分就是对于DHCP工作过程的理解. DHCP分为两个部分:一个是服务器端,另一个是客户端. 所有客户机的IP地址设定资料都由DHCP服务器集中管理,并负责处理客户端的 ...

  3. 利用hadoop来解决“共同好友”的问题

    假设A有好友B C D:B有好友A C D E:C有好友A B D E:D有好友A B C E;E有好友B C D. A -> B C D B -> A C D E C -> A B ...

  4. window 10 企业版激活

    一. 用管理员权限打开CMD.EXE 接着输入以下命令: slmgr /ipk NPPR9-FWDCX-D2C8J-H872K-2YT43 弹出窗口提示:“成功的安装了产品密钥”. 继续输入以下命令: ...

  5. block(五)用法

    最近又从网上找了点block用法的博客,供大家参考. Block简介: Block的实际行为和Function很像,最大的差别是在可以存取同一个Scope的变量值.Block实体形式如下: ^(传入参 ...

  6. Mac 下查看网络端口占用情况

    1.Mac 下查看网络端口占用情况 有的时候关闭了服务器,但是端口还是占用,解决的方法是 kill 掉占用该端口的进程. # 查看 8009 端口的占用情况 $ lsof -i:8009 可以看到,该 ...

  7. sql改写or 改成union不等价数据变多

    select count(*) from (SELECT A.* FROM (SELECT CD.*, nvl(CV.SUM_CI_BALANCE, 0) as SUM_CI_BALANCE, nvl ...

  8. com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 > 1024)

    ### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 > 1024). You ...

  9. FreeSWITCH呼叫参数之sip_cid_type

    这个参数定义了呼叫中主叫信息的头字段类型.支持两种类型: 1. rpidRemote-Party-ID头,这是默认的设置.{sip_cid_type=rpid}sofia/default/user@e ...

  10. Web用户控件开发--星型评分控件

    本文中分享一个实现简单,使用方便的星型评分控件. 一:贴几张测试图片先: 二.星型评分控件的实现: RatingBar.ascx: <%@ Control Language="C#&q ...