[力扣]——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;
   }
}
相关推荐
玛卡巴卡ldf5 小时前
【LeetCode 手撕算法】(动态规划)爬楼梯、杨辉三角、打家劫舍、完全平方数、零钱兑换、单词拆分、最长递增子序列、乘积最大子数组、分割等和子集
java·数据结构·算法·leetcode·动态规划·力扣
玛卡巴卡ldf3 天前
【LeetCode 手撕算法】(栈)有效括号、最小栈、字符串解码、每日温度、柱状图最大矩形
java·数据结构·算法·leetcode·力扣
小辉同志6 天前
62. 不同路径
c++·力扣·多维动态规划
玛卡巴卡ldf6 天前
【LeetCode 手撕算法】(回溯)全排列DFS、子集、电话号码字母组合 九键、组合总和、括号生成、单词搜索、分割回文数
java·算法·leetcode·力扣
mask哥7 天前
15种算法模式java实现详解
java·算法·力扣
旖-旎11 天前
深搜练习(N皇后)(10)
c++·算法·深度优先·力扣
加农炮手Jinx12 天前
LeetCode 26. Remove Duplicates from Sorted Array 题解
算法·leetcode·力扣
加农炮手Jinx12 天前
LeetCode 88. Merge Sorted Array 题解
算法·leetcode·力扣
旖-旎14 天前
深搜练习(组合总和)(7)
c++·算法·深度优先·力扣
旖-旎15 天前
深搜练习(组合)(5)
c++·算法·深度优先·力扣