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.

思想是把有效字符挨个保存在一个字符数组中,然后前后进行比较看是否相等即可判断,acsii里边,0-9对应的是48-57;A-Z对应的是65-90;a-z对应的是97-122

或者可以用Character.toLowerCase(a)和Character.toUpperCase(c[i])进行大小写字母的转换

s=s.replaceAll("[^a-zA-Z0-9]","").toLowerCase(); //巧妙的运用了正则表达式匹配以及String的replaceAll方法,真是不错

public class Solution {
    public boolean isPalindrome(String s) {
        char[] c=new char[s.length()];//必须定义大小
        int count=0;
 //定义了一个count,作用是记录出现的非字母数字的其他符号的个数
        if(s!=null){    //有一个char[] c=s.toCharArray();会把s原封不动的放在c里,包括空格和标点
            for(int i=0;i<s.length();i++){
                char temp=s.charAt(i);
                if(temp>='A' && temp<='Z' || temp>='a' && temp<='z' || temp>='0' && temp<='9')
                    c[i-count]=temp;
                else count++;
            }
            for(int i=0;i<(s.length()-count+1)/2;i++){
                if(c[i]>='0' && c[i]<='9'){
                    if(c[i]!=c[s.length()-count-1-i])   return false;
                }   
                else if(c[i]>='A' && c[i]<='Z'){
                    if(c[i]!=c[s.length()-count-1-i] && c[i]-c[s.length()-count-1-i]!=-32) return false;
                }    
                else if(c[i]>='a' && c[i]<='z'){
                    if(c[i]!=c[s.length()-count-1-i] && (c[i]-c[s.length()-count-1-i])!=32)   return false;
                }
            }
        }
        return true;
    }
}

上边的程序可以简化为

public class Solution {
    public boolean isPalindrome(String s) {
        char[] c=new char[s.length()];//必须定义大小
        int count=0;
        if(s!=null){
            for(int i=0;i<s.length();i++){
                char temp=s.charAt(i);
                if(temp>='A' && temp<='Z' || temp>='a' && temp<='z' || temp>='0' && temp<='9')
                    c[i-count]=temp;
                else count++;
            }
            for(int i=0;i<(s.length()-count+1)/2;i++){
                if(c[i]>='0' && c[i]<='9'){
                    if(c[i]!=c[s.length()-count-1-i])   return false;
                }   
                else if(c[i]>='A' && c[i]<='Z' || c[i]>='a' && c[i]<='z'){
                    if(Character.toLowerCase(c[i])!=Character.toLowerCase(c[s.length()-count-1-i])) return false;
                }    
            }
        }
        return true;
    }
}

看看下边的这个是多么的简单。充分说明了没文化真可怕啊

public class Solution {
    public boolean isPalindrome(String s) {
        s=s.replaceAll("[^a-zA-Z0-9]","").toLowerCase(); //巧妙的运用了正则表达式匹配
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)!=s.charAt(s.length()-1-i))
                return false;
        }
        return true;
    }
}

再看看下边这个循环,用start和end代替之后,多酷,多一种思考方式哦

public class Solution {
    public boolean isPalindrome(String s) {
        s=s.replaceAll("[^a-zA-Z0-9]","").toLowerCase();
        int start=0,end=s.length()-1;
        while(start<end){
            if(s.charAt(start)!=s.charAt(end))
                return false;
            start++;
            end--;
        }
        return true;
    }
}

下边这个是利用了栈的特性

public class Solution {
    public boolean isPalindrome(String s) {
        s=s.replaceAll("[^a-zA-Z0-9]","").toLowerCase();
        Stack<Character> stack=new Stack<Character>();
        int index=0;
        int len=s.length();  
        while(index<len/2){
            stack.push(s.charAt(index));  //把前一半压入栈
            index++;
        }
        if(len%2==1)  index++;  //完全是数学逻辑,
        while(index<len){
            if(s.charAt(index)!=stack.pop())    return false;
            else index++;
        }
        return true;   //长度小于2的情况都包含在这个里边了
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值