[力扣]——125.验证回文串

java 复制代码
class Solution {
    public static boolean isValidChar(char ch){
        if((ch >= 'a' && ch <= 'z') ||
           (ch >= '0' && ch <= '9')){
               return true;
       }
 
        return false;
   }
 
    public boolean isPalindrome(String s) {
    // 将大小写统一起来
        s = s.toLowerCase();
        int left = 0, right = s.length()-1;
        while(left < right){
            // 1. 从左侧找到一个有效的字符
            while(left < right && !isValidChar(s.charAt(left))){
                left++;
           }
 
            // 2. 从右侧找一个有效的字符
            while(left < right && !isValidChar(s.charAt(right))){
                right--;
           }
 
            if(s.charAt(left) != s.charAt(right)){
                return false;
           }else{
                left++;
                right--;
           }
       }
 
        return true;
   }
}
相关推荐
木子墨51615 小时前
LeetCode 热题 100 精讲 | 矩阵与图论进阶篇:矩阵置零 · 螺旋矩阵 · 旋转图像 · 搜索二维矩阵 II · 岛屿数量 · 腐烂的橘子
c++·算法·leetcode·矩阵·力扣·图论
念越1 天前
算法每日一题 Day07|双指针求解和为S的两个数
算法·力扣
旖-旎3 天前
深搜(二叉树剪枝)(3)
数据结构·c++·算法·力扣·剪枝·递归
念越4 天前
算法每日一题 Day05|双指针解决盛最多水的容器问题
算法·力扣
念越5 天前
算法每日一题 Day03|快慢双指针解决快乐树问题
算法·力扣
旖-旎5 天前
递归(快速幂)(5)
c++·算法·力扣·递归
加农炮手Jinx6 天前
LeetCode 146. LRU Cache 题解
算法·leetcode·力扣
加农炮手Jinx6 天前
LeetCode 128. Longest Consecutive Sequence 题解
算法·leetcode·力扣
旖-旎8 天前
栈(验证栈序列)(5)
c++·算法·leetcode·力扣·
j_xxx404_8 天前
C++算法:哈希表(简介|两数之和|判断是否互为字符重排)
数据结构·c++·算法·leetcode·蓝桥杯·力扣·散列表