标签:and put 应用 code amp 直接 char ali NPU
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.
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
知识点:正则表达式
在Java中,使用pattern 来构建正则表达式,使用 matcher 来处理正则表达式
Pattern.compile("[^0-9A-Za-Z]").matcher(s).replaceAll("");
或者直接
s.replaceAll("[^0-9A-Za-Z]","");
import java.util.regex.*;
import java.util.*;
class Solution {
public boolean isPalindrome(String s) {
if(s == null)
return true;
s = s.replaceAll("[^0-9a-zA-Z]","");
int i = 0;
int j = s.length()-1;
while(i<j){
if(Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(s.charAt(j))){
i++;
j--;
}else{
return false;
}
}
return true;
}
}
leetcode 125. Valid Palindrome
标签:and put 应用 code amp 直接 char ali NPU
原文地址:https://www.cnblogs.com/clnsx/p/12306783.html