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. Js里面IF(var)表示什么意思?js中if的写法、含义

    Q:if(一个变量),没有> < !=这些半短逻辑, 什么时候为真 什么时候为假? A:是判断逻辑,javascript是弱变量类型语言,通俗地就是没有数据类型.任意值都有逻辑值(真或假) ...

  2. Android权限详解

    在Android的设计中,资源的访问或者网络连接,要得到这些服务都需要声明其访问权限,否则将无法正常工作.在Android中这样的权限有很多种,这里ATAAW.COM将各类访问权限一一罗列出来,供大家 ...

  3. YY老总李学凌给记者们的几句话

    从记者到总编,从狗狗.多玩到如今的 YY.100 教育,似乎李学凌在这么多年来一直没有放缓过脚步.作为记者转型的成功案例,李学凌总结记者生涯有几方面令其获益匪浅: 1.平常心.对待再高层次的人,也用一 ...

  4. OPENSSL编程起步

    原文链接: http://blog.csdn.net/itmes/article/details/7711076 WINDOWS平台下OPENSSL的编译和安装使用 OPENSSL是开放源代码的,可以 ...

  5. 学习笔记之 curl 命令用法详解

    [前言]     本文翻译和整理自 Linux-2.6.32 中和 curl 相关的 Manual Page 描述文档. 文档目的仅在提醒读者所遗忘的知识点,故在整理时削弱了阅读流畅性,适用于对 cu ...

  6. SIMULINK的模块库介绍

    SIMILINK模块库按功能进行分为以下8类子库:Continuous(连续模块)Discrete(离散模块)Function&Tables(函数和平台模块)Math(数学模块)Nonline ...

  7. Java Date and Calendar examples

    Java Date and Calendar examples This tutorial shows you how to work with java.util.Date and java.uti ...

  8. 翻译Java虚拟机的结构

    英文原版:  https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html 直接谷歌翻译: Java SE规范 > Java虚拟机 ...

  9. Windows 平台下Myeclipse 10 中SVN 插件使用教程(TortoiseSVN)

    1.  TortoiseSVN 简介 版本控制是管理信息修改的艺术,它一直是程序员最重要的工具,程序员经常会花时间作出小的修改,然后又在某一天取消了这些修改,想象一下一个开发者并行工作的团队 - 或许 ...

  10. Android呼叫管理服务之会话发起协议(SIP)API

    原文:http://android.eoe.cn/topic/android_sdk Android提供了一个支持会话发起协议(SIP)的API,这可以让你添加基于SIP的网络电话功能到你的应用程序. ...