leetcode-164周赛-1268-搜索推荐系统
题目描述:



自己的提交:
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-搜索推荐系统的更多相关文章
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...
- LeetCode:二叉搜索树中的搜索【700】
LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...
- 【python】Leetcode每日一题-搜索排序数组2
[python]Leetcode每日一题-搜索排序数组2 [题目描述] 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k( ...
- LeetCode 5273. 搜索推荐系统 Search Suggestions System
地址 https://leetcode-cn.com/problems/search-suggestions-system/ 题目描述给你一个产品数组 products 和一个字符串 searchWo ...
- Leetcode 701. 二叉搜索树中的插入操作
题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根 ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...
- Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)
Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- [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 ...
随机推荐
- springboot themaleaf 各种报错
1.访问themaleaf页面报错 Whitelabel Error Page This application has no explicit mapping for /error, so you ...
- mac终端命令--常用快捷键
Ctrl + c 取消当前行输入的命令,进入下一行.或者中止一个错误的或者发疯的命令 Ctrl + a 光标移动到行首(Ahead of line),相当于通常的Home ...
- Security Spring 配置
<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://w ...
- 配置框架spring和SpringDataJpa整合----员工是爹
<!-- 1.dataSource 配置数据库连接池--> <bean id="dataSource" class="com.mchange.v2.c3 ...
- 【JS】js引擎执行过程
概述 js引擎执行过程主要分为三个阶段,分别是语法分析,预编译和执行阶段,上篇文章我们介绍了语法分析和预编译阶段,那么我们先做个简单概括,如下: 语法分析: 分别对加载完成的代码块进行语法检验,语法正 ...
- 如何在cmd中启动MongoDB服务器和客户端
1 先将MongoDB的bin路径添加到环境变量中 2 打开cmd输入mongod 开启MongoDB服务器 3 输入mongo开启MongoDB客户端
- nucleus学习
task的TCB结构: typedef struct TC_TCB_STRUCT { /* Standard thread information first. This information is ...
- Json中判断是JSONArray还是JSONObject
聪明的人总是能想到别人会遇到的问题,提前给出解决方案. List propList = new ArrayList(); //装载数据的list JSONArray array= JSONArray. ...
- HTML—学习笔记
1 .表格 <br/>换行 <p> align top<img src="./julizi.png" align="top" &g ...
- 在 MacBook 上安装 Ubuntu
建立ubuntu 安裝U盤,加入usb port,按住option key 開機,正常安裝. 完成後,會缺少wifi drive sudo apt-get install bcmwl-kernel-s ...