zoukankan      html  css  js  c++  java
  • leetcode 125. Valid Palindrome

    题目内容

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

    Note: For the purpose of this problem, we define empty string as valid palindrome.

    Example:
    Input: "A man, a plan, a canal: Panama"
    Output: true
    Example 2:
    
    Input: "race a car"
    Output: false
    

    分析过程

    • 题目归类:
      正则表达式
    • 题目分析:
      本题主要考的是 java 的正则表达式的应用,顺便复习正则表达式的方法。

    知识点:正则表达式
    在Java中,使用pattern 来构建正则表达式,使用 matcher 来处理正则表达式

    Pattern.compile("[^0-9A-Za-Z]").matcher(s).replaceAll("");
    或者直接
    s.replaceAll("[^0-9A-Za-Z]","");
    
    • 边界分析:
      • 空值分析
      • 循环边界分析
    • 方法分析:
      • 数据结构分析
      • 状态机
      • 状态转移方程
      • 最优解
    • 测试用例构建

    代码实现

    import java.util.regex.*;
    import java.util.*;
    class Solution {
        public boolean isPalindrome(String s) {
            if(s == null)
                return true;
            s =  s.replaceAll("[^0-9a-zA-Z]","");
            int i = 0;
            int j = s.length()-1;
            
            while(i<j){
                if(Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(s.charAt(j))){
                    i++;
                    j--;
                }else{
                    return false;
                }
            }
            return true;
        }
    }
    
    
  • 相关阅读:
    Python3 爬取验证代理
    Python每天学一点之Threading和queue
    Python每天学一点之argparse
    [安恒月赛]反序列化字符逃逸
    $AFO$
    洛谷$P3647 [APIO2014]$连珠线 换根$dp$
    线性基学习笔记
    $vjudge CSP-S$专题专练题解
    $POJ2942 Knights of the Round Table$ 图论
    $tarjan$简要学习笔记
  • 原文地址:https://www.cnblogs.com/clnsx/p/12306783.html
Copyright © 2011-2022 走看看