leetcode 125. 验证回文串

1 题目描述

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

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

示例 1:

输入: “A man, a plan, a canal: Panama”
输出: true

示例 2:

输入: “race a car”
输出: false

2 注意事项

看清题目要求,只考虑字母和数字字符

class Solution {
    public boolean isPalindrome(String s) {
        s = s.toLowerCase();
        int low = 0, high = s.length()-1;
        while (low < high) {
            String a = s.charAt(low)+"";
            while (!a.matches("[a-z|0-9]")) {
                low++;
                if (low >= high) return true;
                a = s.charAt(low)+"";
            }

            a = s.charAt(high)+"";
            while (!a.matches("[a-z|0-9]")) {
                high--;
                if (low >= high) return true;
                a = s.charAt(high)+"";
            }

            if (s.charAt(low++) != s.charAt(high--)) return false;
        }
        return true;
    }
}

《leetcode 125. 验证回文串》

    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bd3a8f32b717778ac20a8bb
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞