Skip to main content

🟢 剑指 Offer II 018. 有效的回文

LeetCode 提示

题目难度 简单

原题链接 🔗 leetcode

题解1#

class Solution {    private boolean canCount(char c) {        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');    }    private boolean isSame(char a, char b) {        return (a <= '9' && b <= '9' && a == b) || (a >= 'A' && b >= 'A' && (Math.abs(a - b) == 'a'-'A' || a == b));    }
    public boolean isPalindrome(String s) {        int n = s.length();        int lo = 0, hi = n-1;        while (lo < hi) {            while (!canCount(s.charAt(lo)) && lo < n-1) {                lo += 1;            }            while (!canCount(s.charAt(hi)) && hi > 0) {                hi -= 1;            }            if (lo >= hi) {                return true;            }                        if (!isSame(s.charAt(lo), s.charAt(hi))) {                return false;            }            lo += 1;            hi -= 1;        }
        return true;    }}