zoukankan      html  css  js  c++  java
  • 010正则表达式匹配

    写在前面,太难了,目前还在思考,只是先把答案写出来,等后面想完了再补上思考过程

    一、java代码

    /*
     * @lc app=leetcode.cn id=10 lang=java
     *
     * [10] 正则表达式匹配
     */
    
    // @lc code=start
    class Solution {
        public boolean isMatch(String s, String p) {
            //S串长度
            //p串长度
            int m=s.length();
            int n=p.length();
    
            boolean[][]f=new boolean[m+1][n+1];
            f[0][0]=true;
            for(int i=0;i<=m;++i){
                for(int j=1;j<=n;++j){
                    //判断p串后面是否有*号,*让p[j-2]重复0次
                    if(p.charAt(j-1)=='*'){
                        f[i][j]=f[i][j-2];
                        //
                        if(matches(s,p,i,j-1)){
                            f[i][j]=f[i][j]||f[i-1][j];
                        }
                    }
                    else{
                        if(matches(s,p,i,j)){
                            f[i][j]=f[i-1][j-1];
                        }
                    }
                }
            }
            return f[m][n];
        }
        
        public boolean matches(String s,String p,int i,int j){
            if (i==0){
                return false;
            }
            if (p.charAt(j-1)=='.'){
                return true;
            }
            return s.charAt(i-1)==p.charAt(j-1);
        }
    }
    // @lc code=end
    
    
    
    

    二、图解过程

    2.1

    2.2

    2.3

    2.4

    2.5

    2.6

    2.7

  • 相关阅读:
    (转)详谈高端内存和低端内存
    高级声明------定义一个函数指针数组指针
    A Bug's Life POJ
    How Many Answers Are Wrong HDU
    A
    B
    数据处理----离散化
    Serval and Parenthesis Sequence CodeForces
    D
    C
  • 原文地址:https://www.cnblogs.com/lxr-xiaorong/p/13463856.html
Copyright © 2011-2022 走看看