博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
strstr
阅读量:6884 次
发布时间:2019-06-27

本文共 1177 字,大约阅读时间需要 3 分钟。

N(n*m)的时间复杂度

public class Solution {public String strStr(String haystack, String needle) {     int nLen = needle.length();    int hLen = haystack.length();        if(nLen==hLen && nLen==0)        return "";    if(nLen == 0)        return haystack;    for(int i=0; i<= hLen-nLen; i++){        int j = 0;        for( ; j

 

kmp:  N(m+n)

 

constructs dfa[][] in time and space to RM.  R is the set of charactor occur. 

 

 

 

package com.kmp; public class KMP {     private String pat;    private int[][] dfa; //二维字典         public KMP(String pat){        this.pat = pat;        int M = pat.length();        int R = 256;        //二维字典的构造        dfa = new int[R][M];        dfa[pat.charAt(0)][0] = 1;        for (int X = 0, j = 1; j< M; j++){            //复制前面某一列的所以跳转,至当前列,这某一列可视为参照列            for( int c = 0; c < R; c++) dfa[c][j] = dfa[c][X];            dfa[pat.charAt(j)][j] = j + 1; //第j个字节,刚好在第j个位置上,说明匹配成功            X = dfa[pat.charAt(j)][X]; //更新参照列的位置        }    }         public int find(String content){        // 查找逻辑,类似游戏        int i, j;        int N = content.length();        int M = pat.length();        for (i = 0, j = 0; i

 

转载于:https://www.cnblogs.com/leetcode/p/4005042.html

你可能感兴趣的文章
使用百度地图实现详细地址自动补全(补全bug''事件只能绑定到一个上的问题')...
查看>>
Emoji表情处理工具类
查看>>
刚刚考过dev401,出去玩了!有时间我把题目给大家贴出来。
查看>>
20145209 《信息安全系统设计基础》第3周学习总结
查看>>
python 进程
查看>>
Grunt插件uglify
查看>>
export 与 export default
查看>>
linux配置网卡
查看>>
正则表达式语法
查看>>
013、Dockerfile构建镜像(2019-01-02 周三)
查看>>
c# mvc如何获取xml文件
查看>>
mongodb Java(八)
查看>>
JavaScript随机数
查看>>
ASP.NET验证控件——RequiredFieldValidator
查看>>
strstr
查看>>
MySQL 条件 select case 的实现(解决 零 做分母的问题 )
查看>>
openNebula rgister img instance vms error collections
查看>>
error Infos
查看>>
PL/sql配置相关
查看>>
Linux 查询服务数据
查看>>