2021-02-21:手写代码:高性能路由,也就是一个字符串和多个匹配串进行模糊匹配。一个数组arr里是["*a*","moonfdd"],字符串"moonfdd"能匹配到,理由是arr里有。字符串"xayy"也能匹配到,理由是arr里的"*a*",第1个星对应"x",第2个星对应"yy"。
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
}
执行结果如下:
公众号:福大大架构师每日一题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具