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;
        }
    }
    
    
  • 相关阅读:
    电源积累
    电感的分类及作用
    电容退耦原理分享
    电容选型
    上拉电阻
    LVTTL与LVCMOS区别
    可重入函数与不可重入函数
    永不改变的PCB设计黄金法则
    os_cpu_a.asm
    [原创]Getting Started with Skywalking
  • 原文地址:https://www.cnblogs.com/clnsx/p/12306783.html
Copyright © 2011-2022 走看看