LeetCode刷题(不断更新)

冲冲冲

125. 验证回文串

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

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

示例 1:

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

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

my solution:
class Solution {
    public boolean isPalindrome(String s) {
        //判空
        if ("".equals(s)) return true;
        //过滤非字母非数字字母
        String filterS = s.replaceAll("[^A-Za-z0-9]","");
        //颠倒字符串
        String reverseS  = new StringBuilder(filterS).reverse().toString();
        //进行比较
        return filterS.equalsIgnoreCase(reverseS);
    }
}
other solutions:
// 用俩指针遍历到中间
class Solution {
    public boolean isPalindrome(String s) {
        if (s.isEmpty()) return true;
        char chead,ctail;
        int head = 0;
        int tail = s.length()-1;
        while (head < tail) {
            chead = s.charAt(head);
            ctail = s.charAt(tail);
            if (!Character.isLetterOrDigit(chead)) {
                head ++;
            }else if (!Character.isLetterOrDigit(ctail)) {
                tail --;
            }else{
                if (Character.toLowerCase(chead)!=Character.toLowerCase(ctail)) {
                    return false;
                }
                head ++;
                tail --;
            }
        }
        return true;
        
    }
}
class Solution {
    public boolean isPalindrome(String s) {
        //判空
        if("".equals(s)) return true;
        //整两指针
        int head = 0;
        int tail = s.length()-1;
        //进行判断
        while(head < tail){
            while(head < tail && !Character.isLetterOrDigit(s.charAt(head))){
                head ++;
            }
            while(head < tail && !Character.isLetterOrDigit(s.charAt(tail))){
                tail --;
            }
            if(Character.toLowerCase(s.charAt(head))!=Character.toLowerCase(s.charAt(tail))){
                return false;
            }
            head ++;
            tail --;
            
        }
        return true;
    }
}
// 自己建立字母和数字字符的映射,可以提升速度
class Solution {
    private static final char[]charMap = new char[256];
    static{
        for(int i=0;i<10;i++){
            charMap[i+'0'] = (char)(1+i);  // numeric
        }
        for(int i=0;i<26;i++){
            charMap[i+'a'] = charMap[i+'A'] = (char)(11+i);  //alphabetic, ignore cases
        }
}
    public boolean isPalindrome(String s) {
        char[]pChars = s.toCharArray();
        int start = 0,end=pChars.length-1;
        char cS,cE;
        while(start<end){
            cS = charMap[pChars[start]];
            cE = charMap[pChars[end]];
            if(cS!=0 && cE!=0){
                if(cS!=cE)return false;
                start++;
                end--;
            }else{
                if(cS==0)start++;
                if(cE==0)end--;
            }
        }
        return true;
        }
}
class Solution {
    private static final char[] charMap = new char[256];
        static{
            for (int i = 0;i < 10;i++){
                charMap['0'+i] = (char)(1+i);
            }
            for (int i = 0;i < 26;i++){
                charMap['a'+i] = charMap['A'+i] = (char)(11+i);
            }
        }
    public boolean isPalindrome(String s) {
        char[] sChar = s.toCharArray();
        int head = 0, tail = s.length()-1;
        while(head<tail){
            while(head<tail && charMap[sChar[head]] == 0) head++;
            while(head<tail && charMap[sChar[tail]] == 0) tail--;
            if(charMap[sChar[head]] != charMap[sChar[tail]]) return false;
            head++;
            tail--;
        }
        return true;
    }
    //思路1:过滤非字母数字;倒置字符串;进行对比
    //思路2:整两指针,从两端向中间移动做对比。
    //思路3:
}
posted @ 2020-05-13 17:23  gg12138  阅读(87)  评论(0编辑  收藏  举报