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. Apache 日志设置不记录指定文件类型的方法和日志轮

    Apache日志精准的记录了Web访问的记录,但对于访问量很大的站来说,日志文件过大对于分析和保存很不方便.可以在http.conf(或虚拟主机设置文件httpd-vhosts.conf)中进行设置, ...

  2. >/dev/null 2>&1的含义

    转帖:http://www.cnblogs.com/dkblog/archive/2009/07/31/1980722.html os.system("/etc/init.d/winbind ...

  3. 实体格式化转xml

    In the past, I've done the following to control datetime serialization: Ignore the DateTime property ...

  4. EF的表左连接方法Include和Join

    在EF中表连接常用的有Join()和Include(),两者都可以实现两张表的连接,但又有所不同. 例如有个唱片表Album(AlbumId,Name,CreateDate,GenreId),表中含外 ...

  5. 【MySQL】MySQL之MySQL5.7中文乱码

    自己的MySQL服务器不能添加中文,于是自己使用 show variables like 'character%'; 查看了当前的编码格式 我又通过以下方法将其设置为utf-8 SET charact ...

  6. 编码 GBK 的不可映射字符

    一般做项目公司都会统一要求文件编码类型,很多为了实现应用国际化和本地化和更高的性能,而选用UTF-8而非GBK. 但在开发过程中我们都用的是IDE,只要更改了配置就不用操心了,但有时我们也会用命令行来 ...

  7. Ubuntu 13.04 SSH其他机器连接慢的解决办法

    原来Windows上用Xshell,用这比较爽,命令open http,自动打开自己定义的http服务器SSH 远程连接,点一下文件传输,如果装了xsftp,自动启动xsftp,没有就来个提示,打开一 ...

  8. 转:jQuery插件之Wookmark:流布局插件遇到图片资源请求过慢导致最终计算图片绝对位置top不够准确发生图片重叠的解决方案

    谈起Wookmark我想做过前端的大侠都不会觉得陌生,它就是远近闻名的流布局jQuery插件,这个插件使用起来非常简单,需要引入两个js: 1.<script src="/js/jqu ...

  9. 使用 sqlyog 导入导出数据显示 lost connection to mysql server during query

    mysql中经常需要备份数据,在使用 sqlyog 进行备份数据库为转储文件,然后在其他数据库中导入发生 lost connection 经过查询大量资料是数据库配置的 max_allowed_pac ...

  10. jQuery学习笔记(简介,选择器)

    jQuery优势 1. 强大的选择器.jQuery允许开发者使用从CSS1到CSS3几乎所有的选择器,以及jQuery独创的高级而复杂的选择器. 2. 出色的DOM操作封装 3. 可靠的事件处理机制 ...