Question

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

Key to the solution here is to use two pointers. Time complexity O(n), space cost O(1).

 public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() < 2)
return true;
int length = s.length();
int p1 = 0, p2 = length - 1;
char tmp1, tmp2;
while (p1 < p2) {
tmp1 = s.charAt(p1);
while ((!Character.isLetter(tmp1)) && (p1 < p2) && (!Character.isDigit(tmp1))) {
p1++;
tmp1 = s.charAt(p1);
}
if (Character.isLetter(tmp1))
tmp1 = Character.toLowerCase(tmp1); tmp2 = s.charAt(p2);
while (!Character.isLetter(tmp2) && (p1 < p2) && (!Character.isDigit(tmp2))) {
p2--;
tmp2 = s.charAt(p2);
}
if (Character.isLetter(tmp2))
tmp2 = Character.toLowerCase(tmp2);
if (tmp1 != tmp2)
return false;
p1++;
p2--;
}
return true;
}
}

Valid Palindrome 解答的更多相关文章

  1. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

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

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

  3. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  4. Leetcode Valid Palindrome

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

  5. [LintCode] Valid Palindrome 验证回文字符串

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

  6. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  7. 25. Valid Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  8. [Leetcode][JAVA] Valid Palindrome

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

  9. Valid Palindrome [LeetCode]

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

随机推荐

  1. cf446C DZY Loves Fibonacci Numbers

    C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...

  2. Paint House 解答

    Question There are a row of n houses, each house can be painted with one of the three colors: red, b ...

  3. mysql用户和权限管理

    用户和权限管理 Information about account privileges is stored in the user, db, host, tables_priv, columns_p ...

  4. Android NDK R9d 安装

    NDK是一个工具集,可让您实现您的应用程序使用本机代码的语言,如C和C + +.Android NDK 是在SDK前面又加上了“原生”二字,即Native Development Kit,因此又被Go ...

  5. bootstrap 导航布局

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. Direct3D 2D文本绘制

    现在学习下Direct3D在窗口中绘制一些文本信息,ID3DXFont接口负责创建字体和绘制二维的文本.我们介绍下ID3DXFont的用法. 1.创建LPD3DXFONT接口 LPD3DXFONT g ...

  7. 将string 转int

    /** * @param {string} str * @return {number} */ var myAtoi = function(str) { str = str.replace(/^(\s ...

  8. Linux安装中文man手冊

    1.下载中文包: http://pkgs.fedoraproject.org/repo/pkgs/man-pages-zh-CN/manpages-zh-1.5.1.tar.gz/13275fd039 ...

  9. 微信jssdk获取当前位置,以及打开微信地图

    $(function() { var url = window.location.href; var userId = $("#userId").val(); // var ope ...

  10. jquery怎么获取URL的参数

    function request(paras) {                var url = location.href;                var paraString = ur ...