Java [Leetcode 125]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.

解题思路:

设置两个指针,一个在字符串头部,一个在字符串尾部,分别向中间移动,遇到非字母或数字则继续向中间移动,如两个都为字母或者数字,那么则比较两者是否相同。

代码如下:

public class Solution {
    public boolean isPalindrome(String s) {
    	int i = 0, j = s.length() - 1;
    	char head, tail;
    	if(j < 0)
    		return true;
    	while(i < j){
    		head = s.charAt(i);
    		tail = s.charAt(j);
    		if(!Character.isLetterOrDigit(head)){
    			i++;
    		}
    		if(!Character.isLetterOrDigit(tail)){
    			j--;
    		}
    		if(Character.isLetterOrDigit(head) && Character.isLetterOrDigit(tail)){
    			if(Character.toLowerCase(head) != Character.toLowerCase(tail)){
    				return false;
    			}
    			i++;
    			j--;
    		}
    	}
    	return true;
    }
}

  

时间: 2024-10-21 13:33:13

Java [Leetcode 125]Valid Palindrome的相关文章

leetCode 125. Valid Palindrome 字符串

125. 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:Hav

leetcode 125. Valid Palindrome ----- java

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 th

[LeetCode] 125. 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 th

Java for LeetCode 125 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. 解题思路: 注意题目,忽略大小写,忽略非字母或数字,JA

leetcode 125. Valid Palindrome

题目描述 c语言中的tolowe(A)函数,将字符A转换为小写: isalpha(a) 判断a是不是一个字符 isdigital(a)判断a是不是一个数字,isalnum(a)判断a是不是一个数字或者字符 只判断字符串中字符是不是回文 空字符串是一个回文字符串 class Solution { public: bool isPalindrome(string s) { int i = 0; int j = s.size() -1; while(i < j){ while(!isalnum(s[i

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  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:

125. Valid Palindrome【easy】

125. Valid Palindrome[easy] 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. No

125. Valid Palindrome(js)

125. Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a

【LeetCode】125 - 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 th