题目描述:

自己的提交:

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. vue-cli脚手架工具新老版本安装对比

    1.老版本 Shift+鼠标右键 选择打开命令窗口 1.创建项目之前,需先确保本机已经安装node 在命令窗口中执行node -v npm -v   2.一般情况下用npm安装东西比较慢,可以使用淘宝 ...

  2. c++后台开发面试常见知识点总结(四)数据库

    数据库的索引类型 聚集索引和非聚集索引的区别(叶节点存储内容) 唯一性索引和主码索引的区别 索引的优缺点,什么时候使用索引,什么时候不能使用索引(重点) 索引最左前缀问题 数据库中事务的ACID 数据 ...

  3. Maven介绍及安装

      1.maven是一个管理第三方库的jar package 2.从该页面下载相应的Maven jar包(http://maven.apache.org/download.cgi),linux OS下 ...

  4. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  5. 【集群】Redis集群设计原理

    Redis集群设计包括2部分:哈希Slot和节点主从 节点主从: 主从设计不算什么新鲜玩意,在数据库中我们也经常用主从来做读写分离,直接上图: 图上能看得到的信息: 1, 只有1个Master,可以有 ...

  6. [NOIP模拟26]题解

    今天的考试题改自闭了……所以滚来写陈年题解. A.*****贪婪***** RT,出题人告诉我们这题要贪心. 最优的策略一定是拖到必须断的时候再断开(虽然并不知道为什么). 如果一段序列满足题目中的性 ...

  7. JavaScript实现注册时检查邮箱,名称,密码等是否符合规则

    大概实现了,用户名是否存在,邮箱是否已注册,密码是否符合复杂度. //对用户名校验是否存在function checkname(){ //alert("checkname"); v ...

  8. 8、数值分析与matlab

    1.今天要拷matlab代码了,而且是很恶心的算法,估计也没几个人能看得懂,就连我自己都看不懂. 我也不知道这样做的意义何在,可能只是证明我在这世上曾经学过那么那么难的东西吧 首先是一个matlab版 ...

  9. PHP处理地址匹配出省市区

    function handleAddress($address ='广东省深圳市龙华新区大浪街道同胜科技大厦'){ preg_match('/(.*?(省|自治区|北京市|天津市))/', $addr ...

  10. P3203 [HNOI2010]弹飞绵羊(LCT)

    弹飞绵羊 题目传送门 解题思路 LCT. 将每个节点的权值设为\(1\),连接\(i\)和\(i+ki\),被弹飞就连上\(n\),维护权值和\(sum[]\).从\(j\)弹飞需要的次数就是\(sp ...