package com.leetcode.leetcode.array;

/**给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

    说明:本题中,我们将空字符串定义为有效的回文串。

    示例 1:

    输入: "A man, a plan, a canal: Panama"
    输出: true
    示例 2:

    输入: "race a car"
    输出: false

    链接:https://leetcode-cn.com/problems/valid-palindrome*/
public class IsPalindrome {

  public boolean isPalindrome(String s) {
    s = s.replaceAll("[^0-9a-zA-Z]", "").toLowerCase();
    int i = 0;
    int j = s.length()-1;
    while(i<j){
      if(s.charAt(i++)!=s.charAt(j--)){
        return false;
      }

    }
    return true;
  }
}

github:
https://github.com/pzzzsakura/leetcode/blob/master/src/main/java/com/leetcode/leetcode/array/IsPalindrome.java

You Might Also Like

No Comments

Leave a Reply