LeetCode: Valid Palindrome 解题报告
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 解题报告的更多相关文章
- 【LeetCode】125. Valid Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 列表生成式 正则表达式 双指针 日期 题目地址:https:/ ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- LeetCode: Valid Sudoku 解题报告
Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- [leetcode]Valid Palindrome @ Python
原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- ng-file-upload结合springMVC使用
引入angular和ng-file-upload. 前端代码 Upload.upload({ url: 'upload', fields: {'username': 'zouroto'}, // ad ...
- bat批处理,实现获取目录
@echo off echo 当前盘符:%~d0 echo 当前盘符和路径:%~dp0 echo 当前批处理全路径:%~f0 echo 当前盘符和路径的短文件名格式:%~sdp0 echo 当 ...
- Python学习笔记(九)—— 函数
一.函数调用: 1.python内部函数查询:http://docs.python.org/3/library/functions.html#abs 2.注意调用函数的参数个数和类型. 3.函数名其实 ...
- elk架构图
一.概述 笔者为了节约宝贵的服务器资源,把一些可拆分的服务合并在同一台主机.大家可以根据自己的实际业务环境自由拆分,延伸架构.
- 【算法】MD5加密
1.什么是MD5 MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍 ...
- Linux安装ElasticSearch-2.2.0-分词器插件(IK)
1.在gitpub上搜索elasticsearch-analysis,能够看到所有elasticsearch的分词器: 2.安装IK分词器:https://github.com/medcl/elast ...
- 【Algorithm】二分查找
今天在学习<编程之美>的时候,看到一个二分查找的题目,发现原来我真的不懂二分查找. 二分查找时候注意的事项: 在求二分查找的中间点时没有使用 midIndex = (minIndex + ...
- 【C语言】练习1-21
题目来源:<The C programming language>中的习题 练习1-21:编写程序entab,将空格串替换为最好数量的制表符和空格,但要保持单词之间的间隔不变. 思路: 对 ...
- 1 时间戳 2 C# 如何生成一个时间戳 3 js 时间加一分钟... 4 js string->date 5 js 取得当天0点 / 23:59:59 时间
var str = 'Jan 23, 2019 10:25:47 AM';var strnow = new Date(str); 时间戳(timestamp),一个能表示一份数据在某个特定时间之前已经 ...
- Python ---chart
# -*- coding:utf-8 -*- import random import matplotlib.pyplot as plt from pylab import * import os i ...