CODE 8: Valid Palindrome

原创 2013年09月12日 22:13:27

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 boolean isPalindrome(String s) {
		// Start typing your Java solution below
		// DO NOT write main() function
		if (null == s) {
			return false;
		}
		if (s.length() <= 1) {
			return true;
		}
		int i = 0;
		int j = s.length() - 1;
		while (i < j) {
			char a = s.charAt(i);
			char b = s.charAt(j);
			if (a < '0' || (a > '9' && a < 'A') || (a > 'Z' && a < 'a')
					|| a > 'z') {
				i++;
				continue;
			}
			if (b < '0' || (b > '9' && b < 'A') || (b > 'Z' && b < 'a')
					|| b > 'z') {
				j--;
				continue;
			}
			if (a == b || ((a < b) && (a + 32 == b))
					|| ((a > b) && (a - 32 == b))) {
				i++;
				j--;
			} else {
				break;
			}
		}
		if (i < j) {
			return false;
		} else {
			return true;
		}
	}


 

Valid Palindrome -- LeetCode

原题链接: http://oj.leetcode.com/problems/valid-palindrome/  这道题是判断一个字符串是不是回文串。因为只是看一个字符串,算法还是比较简单,就是从两头...
  • linhuanmars
  • linhuanmars
  • 2014-04-02 07:05:57
  • 10634

Valid Palindrome II问题及解法

680. Valid Palindrome II LeetCode
  • u011809767
  • u011809767
  • 2017-09-19 09:36:19
  • 656

Leetcode算法学习日志-680 valid Palindrome II

Leetcode-680 Valid Palindrome II 题目原文
  • Zarlove
  • Zarlove
  • 2017-09-18 16:16:23
  • 1151

125. Valid Palindrome [easy] (Python)

125. Valid Palindrome [easy] (Python)题目链接https://leetcode.com/problems/valid-palindrome/题目原文 Given...
  • coder_orz
  • coder_orz
  • 2016-05-03 14:43:25
  • 1684

【LeetCode-面试算法经典-Java实现】【125-Valid Palindrome(回文字验证)】

【125-Valid Palindrome(回文字验证)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a string, determine if it i...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-14 06:18:42
  • 2642

[C++]Valid Palindrome 有效回文

leetcode 原题链接:https://leetcode.com/problems/valid-palindrome/ Given a string, determine if ...
  • lyy_hit
  • lyy_hit
  • 2015-08-21 11:02:46
  • 508

LeetCode125——Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori...
  • booirror
  • booirror
  • 2015-01-27 18:05:48
  • 2519

[C++]LeetCode: 25 Valid Palindrome

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characte...
  • cinderella_niu
  • cinderella_niu
  • 2014-11-20 13:56:49
  • 645

LWC 50:680. Valid Palindrome II

LWC 50:680. Valid Palindrome II传送门:680. Valid Palindrome IIProblem: Given a non-empty string s, yo...
  • u014688145
  • u014688145
  • 2017-09-21 20:29:10
  • 594

Leetcode-680. Valid Palindrome II(判断回文)

题目大意:给定一个非空字符串s,您最多可以删除一个字符。判断你是否能把它变成一串“回文”。 题目解析:最开始用的暴力,结果超时了,换用递归 解题代码: class Solution { publ...
  • zsheng_
  • zsheng_
  • 2017-10-05 10:37:23
  • 205
收藏助手
不良信息举报
您举报文章:CODE 8: Valid Palindrome
举报原因:
原因补充:

(最多只允许输入30个字)