Easy-题目60:125. Valid Palindrome

题目原文:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
题目大意:
给一个字符串,只考虑字母数字且忽略大小写的情况下,判断是不是回文的。
题目分析:
在普通判断回文的算法基础上简单加以改进即可,遇到大写字母转换为小写字母,遇到非字母和数字跳过,遇到两边指针指向的字符不相等直接返回false跳出去。
源码:(language:java)

public class Solution {
    public boolean isPalindrome(String s) {
        int len=s.length();
        if(len==0||len==1)
            return true;
        int i=0,j=len-1;
        while(i<j) {
            char ch1=s.charAt(i);
            char ch2=s.charAt(j);
            if(ch1>='A'&&ch1<='Z')
                ch1+=32;
            if(ch2>='A'&&ch2<='Z')
                ch2+=32;
            if (!((ch1>='a'&&ch1<='z') || (ch1>='0' && ch1<='9'))) {
                i++;
                continue;
            }
            if (!((ch2>='a'&&ch2<='z') || (ch2>='0' && ch2<='9'))) {
                j--;
                continue;
            }
            if(ch1!=ch2)
                return false;
            else {
                i++;
                j--;
            }
        }
        return true;
    }
}

成绩:
5ms,beats 97.26%,众数9ms,15.18%
cmershen的碎碎念
其实本题的思路不难想,但我也想不到为什么会击败这么多提交代码……,可能是计算机中加32比较好操作吧(只需改动1bit)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值