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 ...
随机推荐
- jQuery HTML- 添加元素
添加内容 html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...
- easyui记录
var rows = top.$("#queryDetailGrid").datagird("getRows"); //获取datagird所有行 top.$( ...
- 【leetcode】991. Broken Calculator
题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...
- Java实现按汉语拼音的排序
public class sortByPinyin { public static void main(String[] args) { String[] arr = { "刘刘" ...
- SysTick功能总结
一.初始化SysTick 按1ms来设置systick,也可以除以1000000.按1us来设置 SysTick_Config(SystemCoreClock / 1000); //SysTick开启 ...
- switch 使用使用小技巧
for (int i=0;i<100;i++) { switch (i) { case 1 ... 10: NSLog(@"case 1 ... 10: = %d",i); ...
- Centos6.6部署Redis集群
Centos6.6部署Redis集群 1环境准备 1环境安装redis 1安装ruby 2配置redis主从环境 3部署redis sentinel服务器 5集群使用 13当前集群环境说明 13测试功 ...
- 安装纯净版debian!
kali更新了1.1.0a,不知道新版的内核哪地方有bug,用着用着就卡死了,一怒之下卸载了装debian. 下载的netinst只有200M,基本上就是刚好能用,不要用硬盘装,会找不到网卡,无线也没 ...
- ARM系列处理器的分类
1.ARM ARM即以英国ARM(Advanced RISC Machines)公司的内核芯片作为CPU,同时附加其他外围功能的嵌入式开发板,用以评估内核芯片的功能和研发各科技类企业的产品. ARM ...
- python 反转列表的3种方式
转载自:https://blog.csdn.net/bookaswine/article/details/42468735 方式一:使用reversed()函数 a=[1,2,3,4,5,6,7,8, ...