LeetCode 5273. 搜索推荐系统 Search Suggestions System
地址 https://leetcode-cn.com/problems/search-suggestions-system/
题目描述
给你一个产品数组 products 和一个字符串 searchWord ,products 数组中每个产品都是一个字符串。
请你设计一个推荐系统,在依次输入单词 searchWord 的每一个字母后,推荐 products 数组中前缀与 searchWord 相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。
请你以二维列表的形式,返回在输入 searchWord 每个字母后相应的推荐产品的列表。
样例
示例 : 输入:products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
输出:[
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
解释:按字典序排序后的产品列表是 ["mobile","moneypot","monitor","mouse","mousepad"]
输入 m 和 mo,由于所有产品的前缀都相同,所以系统返回字典序最小的三个产品 ["mobile","moneypot","monitor"]
输入 mou, mous 和 mouse 后系统都返回 ["mouse","mousepad"]
示例 : 输入:products = ["havana"], searchWord = "havana"
输出:[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
示例 : 输入:products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
输出:[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
示例 : 输入:products = ["havana"], searchWord = "tatiana"
输出:[[],[],[],[],[],[],[]]
提示: <= products.length <=
<= Σ products[i].length <= * ^
products[i] 中所有的字符都是小写英文字母。
<= searchWord.length <=
searchWord 中所有字符都是小写英文字母。
算法1
据大佬们说 有什么前缀树的解答
我想了想用trie树的方案 没结果
就开始硬怼了。
首先在products 按照字典序排序,然后另开数组记录对应的单词 与 搜索单词相同的字母数
再遍历这个记录相同字母数的数组 找到相同一个单字的词汇 找到相同两个单字的词汇。。。。。。
遍历或者找到或者找不到为空, 中途剪枝为如果找到符合要求的3个词汇也提前退出(由于已经按照字典序排序,最开始找到的符合要求的3个词汇肯定是字典序优先的)
C++ 代码
class Solution {
public:
vector<vector<string>> ret;
int GetsameCharCount(const string& target,const string& src){
int ret =;
for(int i=;i < target.size() && i < src.size();i++ ){
if(target[i] == src[i]) ret++;
else{
break;
}
}
return ret;
}
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
sort(products.begin(),products.end());
vector<int> sameCharCount;
for(int i = ; i < products.size();i++){
int t = GetsameCharCount(products[i],searchWord);
sameCharCount.push_back(t);
}
for(int i = ; i < searchWord.size();i++){
vector<string> v;
for(int j = ; j < sameCharCount.size();j++){
if(sameCharCount[j] >i){
v.push_back(products[j]);
}
if(v.size() == ) break;
}
ret.push_back(v);
}
return ret;
}
};

LeetCode 5273. 搜索推荐系统 Search Suggestions System的更多相关文章
- 【leetcode】1268. Search Suggestions System
题目如下: Given an array of strings products and a string searchWord. We want to design a system that su ...
- [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- LeetCode 81——搜索旋转排序数组 II
1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...
- LeetCode:搜索旋转排序数组【33】
LeetCode:搜索旋转排序数组[33] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ...
- Design Search Autocomplete System
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- 集束搜索beam search和贪心搜索greedy search
贪心搜索(greedy search) 贪心搜索最为简单,直接选择每个输出的最大概率,直到出现终结符或最大句子长度. 集束搜索(beam search) 集束搜索可以认为是维特比算法的贪心形式,在维特 ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
随机推荐
- spring单元测试下模拟rabbitmq
gradle添加引用 compile 'org.springframework.boot:spring-boot-starter-amqp' testCompile 'com.github.fridu ...
- 【JS档案揭秘】第三集 深入最底层探秘原型链
关于这部分我看过大量的文章,数不胜数,包括阮一峰的继承三部曲,还有各种慕课的视频教程,网上无数继承方法的对比.也对很多概念存在长期错误的理解.今天做一个正确的总结,用来给原型链和继承这块知识画上句号, ...
- vue解惑之v-on(事件监听指令)
一.v-on指令 vue中用v-on指令来监听DOM事件,并触发相应的代码.比如v-on:click,表示监听了点击事件. 二.事件修饰符 在事件处理函数中调用 event.preventDefaul ...
- centos_redis 安装脚本
#!/bin/bash # change to root echo "start to install build env..." sudo yum groupinstall &q ...
- You don't have permission to access / on this server,Forbidden
wampserver配置虚拟主机Forbidden,apache You don't have permission to access 找到httpd.conf文件(通常在安装apache的conf ...
- mysqld_safe error: log-error set to '/data/log/mysqld.log', however file don't exists. Create writable for user 'mysql'.The server quit without updating PID file (/data/mysql/mysqld.pid)
[oot@cent65 bin]# service mysqld startStarting MySQL.2019-10-28T15:56:47.786960Z mysqld_safe error: ...
- sql server2017开启远程连接
1.安装完SQL server2017之后,选择SQL 身份验证登录,可以先用windows身份验证登录把密码更改好了,然后服务器右键重新启动 ,再断开连接 ,选择SQL身份验证登录验证,关闭SQL ...
- MySQL解惑——GROUP BY隐式排序
MySQL中GROUP BY隐式排序是什么概念呢? 主要是其它RDBMS没有这样的概念,如果没有认真了解过概念,对这个概念会感觉有点困惑,我们先来看看官方文档的介绍: 官方文档MySQL 5.7 Re ...
- November 10th, Week 45th, Sunday, 2019
Perfection has no place in love. 爱从不完美. Perfection has no place in love, and we should always try to ...
- JavaScript-----7.循环
1.循环 在JS中主要有以下三种类型的循环 for循环 while循环 do...while循环 2. for循环 2.1 语法结构如下: for (初始化变量: 条件表达式: 操作表达式) { // ...