题目描述:

自己的提交:

class Solution:
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
products.sort()
res = []
for i in range(1,len(searchWord)+1):
ans = []
k = 3
for p in products:
if len(p) >= i and p[:i] == searchWord[:i]:
ans.append(p)
k -= 1
if k == 0:
break
res.append(ans)
return res

优化:

class Solution:
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
products.sort()
search = ""
pos, n = 0, len(products)
ans = list()
for ch in searchWord:
search += ch
while pos < n and products[pos] < search:
pos += 1
result = list()
for i in range(3):
if pos + i < n and products[pos + i].startswith(search):
result.append(products[pos + i])
else:
break
ans.append(result)
return ans

另:

class Solution:
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
products.sort()
N = len(searchWord)
Ans = []
for i in range(N):
cnt = 0
M = len(products)
new_products = []
for j in range(M):
v = products[j]
l = len(v)
if l >= i+1 and v[i] == searchWord[i]:
new_products.append(v)
Ans.append(new_products[:3])
products = new_products
return Ans

方法二:前缀树

def make_trie(root, word):
node = root
for c in word:
node.words.append(word)
print(node.words)
if not c in node.mp:
node.mp[c] = Node()
node = node.mp[c]
node.words.append(word) class Solution(object):
def suggestedProducts(self, products, searchWord):
"""
:type products: List[str]
:type searchWord: str
:rtype: List[List[str]]
"""
root = Node()
for word in products:
make_trie(root, word)
ans = []
for c in searchWord:
if root is None or (not c in root.mp):
root = None
else:
root = root.mp[c]
if root is None:
ans.append([])
else:
ret = sorted(root.words)
if len(ret) > 3:
ret = ret[:3]
ans.append(ret)
return ans

leetcode-164周赛-1268-搜索推荐系统的更多相关文章

  1. LeetCode:二叉搜索树中第K小的数【230】

    LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...

  2. LeetCode:二叉搜索树中的搜索【700】

    LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...

  3. 【python】Leetcode每日一题-搜索排序数组2

    [python]Leetcode每日一题-搜索排序数组2 [题目描述] 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k( ...

  4. LeetCode 5273. 搜索推荐系统 Search Suggestions System

    地址 https://leetcode-cn.com/problems/search-suggestions-system/ 题目描述给你一个产品数组 products 和一个字符串 searchWo ...

  5. Leetcode 701. 二叉搜索树中的插入操作

    题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根 ...

  6. [LeetCode] 164. Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  7. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  8. Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)

    Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  9. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  10. [LeetCode] Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. 第二则java读取excel文件代码

    // 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全 String savePath = this.getServletContext().ge ...

  2. Mongo 备份

    1. Windows下远程连接服务器上的MongoDB数据库 使用的是mongo命令,如果安装mongodb时配置了环境变量,可以直接快捷键windows+R打开cmd. Cmd  --- mongo ...

  3. websocket 中使用Service层的方法

    创建公共Utils 类 ApplicationContextRegister @Component @Lazy(false) public class ApplicationContextRegist ...

  4. javaweb判断当前请求是否为移动设备访问的方法

    由于移动端和pc端还是稍微有些区别的,我觉得最好是在一个地儿统一判断,而且不要改动原先的代码,这样可以从一定程度上减少bug的数量.我的想法是首先应该判断当前请求是否为移动端,然后设一个标识到sess ...

  5. linux软件包rpm的使用

    一rpm包管理器 (一)rpm的介绍 rpm不仅是文件的后缀,也是一个工具,外部命令,程序包管理器 功能:将编译好的应用程序的各组成文件打包一个或几个程序包文件,从而方便快捷地实现程序包的安装.卸载. ...

  6. 《NULL-2019团队》第一次作业:OUC二手交易平台

    前言 项目名称:OUC二手交易平台 项目简介:针对在校大学生的二手交易平台,相对于现在市面已有的二手APP,将其使用的普遍性范围缩小,针对在校大学生,这样可以有效的保证交易的真实性和公平性.   NA ...

  7. sql中简单的触发器功能

    触发器分为DML触发器和DDL触发器DML触发器包含After触发器,执行insert update delete语句后会触发after触发器,会事务回滚DML触发器还包含instead of触发器, ...

  8. ! Unknown property attribute "class"

    当时是在用Xcode 7进行编译ASDK的代码发现报错了 当时就蒙圈了,@property(class)--这是啥呀,太久没看过object-c了,但是不至于@property是没有class属性的, ...

  9. 过滤PostgreSQL配置文件中被注释的部分

    以下正则可以过滤掉PostgreSQL配置文件被注释的部分,包括'#'前带空格的部分,但参数前带空格的部分不会过滤掉 postgres@linux-ij7j:/opt/pg8122/data> ...

  10. 声明式语法重写基于容器CICD构建流水线

    调试了一下午,一句话都不想说了,看代码. ----- 参考文档 https://blog.csdn.net/weixin_42010394/article/details/90670945 实践代码 ...