LeetCode-125-验证回文串

本文介绍了一种解决LeetCode上的《有效回文串》问题的解法,通过遍历字符串并忽略非字母数字字符,同时不区分大小写来判断是否为回文。在遍历过程中,对首尾字符进行比较,若不相等则返回false,否则继续移动指针。最终,当首指针不小于尾指针时返回true,表示字符串是回文。
摘要由CSDN通过智能技术生成
验证回文串

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

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

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-palindrome/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:字符串遍历

主要是使用一些库函数来遍历字符串。

首先,如果s是空或者s的长度为1,则直接返回true;

否则,从s的第一位front和最后一位end开始遍历,遍历过程为:

  • 如果front对应位置的字符frontChar不是字母或数字字符,则front向后挪一位,进行下一轮遍历;
  • 如果end对应位置的字符endChar不是字母或数字字符,则end向前挪一位,进行下一轮遍历;
  • 如果front和end对应位置的字符都是字母或者数字字符,首先,如果frontChar或endChar是字母,则先将之转化为大写字符(因为不需要区分大小写),然后比较frontChar和endChar是否相等,如果不相等,则返回false;如果相等,则front向后挪一位,同时end向前挪一位,进行下一轮遍历。
  • 遍历结束的条件就是front不小于end。
public class LeetCode_125 {
    public static boolean isPalindrome(String s) {
        if (s == null || s.length() == 1) {
            return true;
        }
        int front = 0, end = s.length() - 1;
        while (front <= end) {
            char frontChar = s.charAt(front);
            char endChar = s.charAt(end);
            if ((frontChar >= 'a' && frontChar <= 'z') || (frontChar >= 'A' && frontChar <= 'Z') ||
                    (frontChar >= '0' && frontChar <= '9')) {
                if ((endChar >= 'a' && endChar <= 'z') || (endChar >= 'A' && endChar <= 'Z') ||
                        (endChar >= '0' && endChar <= '9')) {
                    if (Character.isAlphabetic(frontChar)) {
                        frontChar = Character.toUpperCase(frontChar);
                    }
                    if (Character.isAlphabetic(endChar)) {
                        endChar = Character.toUpperCase(endChar);
                    }
                    if (frontChar != endChar) {
                        return false;
                    } else {
                        front++;
                        end--;
                    }
                } else {
                    end--;
                }
            } else {
                front++;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome("A man, a plan, a canal: Panama"));
    }
}

【每日寄语】 人生似水岂无崖,浮云吹作雪,世味煮成茶。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉舞经阁-半卷书

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值