2021-02-21:手写代码:高性能路由,也就是一个字符串和多个匹配串进行模糊匹配。一个数组arr里是["a","moonfdd"],字符串"moonfdd"能匹配到,理由是arr里有。字符串"xayy"也能匹配到,理由是arr里的"a",第1个星对应"x",第2个星对应"yy"。

福哥答案2021-02-21:

1.前缀树。字符匹配和星号匹配。abcd和abcd,当左c和右对应的时候,下一步分两种情况,左d和右*对应,左c和右c对应。有代码。

2.ACOK算法。当时和面试官聊的时候,面试官说了ACOK算法,但这个算法在网上没找到。百度了一番,感觉就是Aho-Corasick automaton算法,也就是AC自动机。AC自动机,没找到解法,所以没代码。

代码用golang编写,代码如下:

package main

import "fmt"

func main() {
fmt.Println("力扣208 测试")
trie := Constructor()
trie.Insert("apple")
trie.Search("apple") // 返回 true
trie.Search("app") // 返回 false
trie.StartsWith("app") // 返回 true
trie.Insert("app")
trie.Search("app") // 返回 true
fmt.Println("--------------------")
fmt.Println("高性能路由 测试")
ret := ""
ret = RouteMatching("fudada", []string{"fudada*"})
fmt.Println("ret = ", ret)
ret = RouteMatching("fudada", []string{"fu******da*"})
fmt.Println("ret = ", ret)
ret = RouteMatching("fudada", []string{"fudada**"})
fmt.Println("ret = ", ret) } type TrieNode struct {
pass int
end int
nextMap map[byte]*TrieNode
}
type Trie struct {
root *TrieNode
} /** Initialize your data structure here. */
func Constructor() Trie {
return Trie{root: &TrieNode{nextMap: make(map[byte]*TrieNode)}}
} /** Inserts a word into the trie. */
func (this *Trie) Insert(word string) {
wordLen := len(word)
if wordLen == 0 {
return
}
node := this.root
node.pass++
for i := 0; i < wordLen; i++ { // 从左往右遍历字符
if node.nextMap[word[i]] == nil {
node.nextMap[word[i]] = &TrieNode{nextMap: make(map[byte]*TrieNode)}
}
node = node.nextMap[word[i]]
node.pass++
}
node.end++
} /** Returns if the word is in the trie. */
func (this *Trie) Search(word string) bool {
wordLen := len(word)
if wordLen == 0 {
fmt.Println(false)
return false
}
node := this.root
for i := 0; i < wordLen; i++ { // 从左往右遍历字符
if node.nextMap[word[i]] == nil {
fmt.Println(false)
return false
}
node = node.nextMap[word[i]]
}
fmt.Println(node.end > 0)
return node.end > 0
} /** Returns if there is any word in the trie that starts with the given prefix. */
func (this *Trie) StartsWith(prefix string) bool {
word := prefix
wordLen := len(word)
if wordLen == 0 {
fmt.Println(false)
return false
}
node := this.root
for i := 0; i < wordLen; i++ { // 从左往右遍历字符
if node.nextMap[word[i]] == nil {
fmt.Println(false)
return false
}
node = node.nextMap[word[i]]
}
fmt.Println(node.pass > 0)
return node.pass > 0
} func RouteMatching(url string, fuzzyMatches []string) string {
fuzzyMatchesLen := len(fuzzyMatches)
if fuzzyMatchesLen == 0 && len(url) == 0 {
return ""
}
trie := Constructor()
for i := 0; i < fuzzyMatchesLen; i++ {
trie.Insert(fuzzyMatches[i])
}
return process(url, 0, trie.root, "")
} func process(url string, index int, root *TrieNode, retPre string) string {
urlLen := len(url)
if index >= urlLen {
if root.end > 0 {
return retPre
} else {
if root.nextMap['*'] != nil {
return process(url, index, root.nextMap['*'], retPre+"*")
}
return ""
}
}
ret := ""
//1.匹配字符
if root.nextMap[url[index]] != nil {
ret = process(url, index+1, root.nextMap[url[index]], retPre+url[index:index+1])
if ret != "" {
return ret
}
}
//2.匹配*
if root.nextMap['*'] != nil {
ret = process(url, index, root.nextMap['*'], retPre+"*")
if ret != "" {
return ret
}
ret = process(url, index+1, root, retPre)
if ret != "" {
return ret
}
}
return ret
}

执行结果如下:


左神前缀树java代码

评论

2021-02-21:手写代码:高性能路由,也就是一个字符串和多个匹配串进行模糊匹配。一个数组arr里是["*a*","moonfdd"],字符串"moonfdd"能匹配到,理由是arr里有。字符串"xayy"也能匹配到,理由是arr里的"*a*",第1个星对应"x",第2个星对应"yy"。的更多相关文章

  1. 2019前端面试系列——JS高频手写代码题

    实现 new 方法 /* * 1.创建一个空对象 * 2.链接到原型 * 3.绑定this值 * 4.返回新对象 */ // 第一种实现 function createNew() { let obj ...

  2. ClownFish:比手写代码还快的通用数据访问层

    http://www.cnblogs.com/fish-li/archive/2012/07/17/ClownFish.html 阅读目录 开始 ClownFish是什么? 比手写代码还快的执行速度 ...

  3. 手写代码自动实现自动布局,即Auto Layout的使用

    手写代码自动实现自动布局,即Auto Layout的使用,有需要的朋友可以参考下. 这里要注意几点: 对子视图的约束,若是基于父视图,要通过父视图去添加约束. 对子视图进行自动布局调整,首先对UIVi ...

  4. 如果选择构建ui界面方式,手写代码,xib和StoryBoard间的博弈

    代码手写UI这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用. 大型多人合作项目使用代码构建UI,主要是看中纯代码在版本管理时的优势,检查追踪改动以及进行代码合并相对容易一些. 另外,代 ...

  5. .netER的未来路,关于基础是否重要和应该自己手写代码吗?

    http://www.cnblogs.com/onepiece_wang/p/5558341.html#!comments 引用"基础知识的学习,一开始可能是背书,但是在后续若干年的工作过程 ...

  6. 手写代码UI,xib和StoryBoard间的的优劣比较

    在UI制作方面,逐渐分化三种主要流派:使用代码手写UI:使用单个xib文件组织viewController或者view:使用StoryBoard来通过单个或很少的几个文件构建UI.三种方式各有优劣,也 ...

  7. UI到底应该用xib/storyboard完成,还是用手写代码来完成?

    UI到底应该用xib/storyboard完成,还是用手写代码来完成? 文章来源:http://blog.csdn.net/libaineu2004/article/details/45488665 ...

  8. java 手写 jvm高性能缓存

    java 手写 jvm高性能缓存,键值对存储,队列存储,存储超时设置 缓存接口 package com.ws.commons.cache; import java.util.function.Func ...

  9. Appium初始化设置:手写代码连接手机、appium-desktop连接手机

    一.包名获取的三种方式 1)找开发要2)mac使用命令:adb logcat | grep START win使用命令:adb logcat | findstr START 或者可以尝试使用第3条命令 ...

  10. gcd手写代码及STL中的使用方法

    一.手写代码 inline int gcd(int x,int y){ if(y==0) return x; else return(gcd(y,x%y)); } 二.STL中的使用方法 注:在STL ...

随机推荐

  1. jenkins freestyle deploy web

    gitlab connection 选择定义好的gitlab仓库 参数化构建过程 git参数 名称 branch 描述 自定义 参数类型 分支 默认值 $branch 选项参数 名称 Status 选 ...

  2. 7. 基础增删改 - 使用Portal Webapi进行会员信息的增删改

    我们可以通过使用Portal Web API在Portal页面中跨所有Microsoft Dataverse表执行创建.更新和删除操作,下面我们就一起来看一下如何通过使用AJAX函数来进行操作. AJ ...

  3. centOS7 + MongoDB 3.6.22 集群搭建 - 切片+副本集 - 个人学习

    因为我是学习这个,所以是安装成功之后自己再记录一下过程,mongodb是重新安装的,参考博客:MongoDB 3.6.9 集群搭建 - 切片+副本集 1. 服务结构介绍 结构图: 结构图解: 1. S ...

  4. 痞子衡嵌入式:分享一个i.MXRT系列配套DRAM压力测试上位机工具(i.MXRT DRAM Tester)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦SE团队制作的i.MXRT配套DRAM压力测试上位机工具. 事情源于痞子衡的技术交流群里的提问,有群友在恩智浦官方技术公众号 [恩 ...

  5. FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup

    前言 「语音处理」是实时互动领域中非常重要的一个场景,在声网发起的「RTC Dev Meetup丨语音处理在实时互动领域的技术实践和应用」活动中,来自百度.寰宇科技和依图的技术专家,围绕该话题进行了相 ...

  6. Typora怎么插入行内公式?内联公式设置

    文件 偏好设置 把内联公式那个勾上就可以了.然后就能写latex了,像这样,$\alpha\$ \(\alpha\) 输入一个\$后再按esc,会自动补全为$$

  7. GO实现Redis:GO实现内存数据库(3)

    实现Redis的database层(核心层:处理命令并返回) https://github.com/csgopher/go-redis datastruct/dict/dict.go type Con ...

  8. 迁移学习(PCL)《PCL: Proxy-based Contrastive Learning for Domain Generalization》

    论文信息 论文标题:PCL: Proxy-based Contrastive Learning for Domain Generalization论文作者:论文来源:论文地址:download 论文代 ...

  9. jQ-DOM属性的操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. QML 界面切换的几种方法(带示例代码)

    QML 界面切换的几种方法(带示例代码)