博客
关于我
leetcode之统计匹配检索规则的物品数量(C++)
阅读量:165 次
发布时间:2019-02-28

本文共 857 字,大约阅读时间需要 2 分钟。

参考链接

  1. https://leetcode-cn.com/problems/count-items-matching-a-rule/

题目描述

给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。

另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。

如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配 :

ruleKey == “type” 且 ruleValue == typei 。

ruleKey == “color” 且 ruleValue == colori 。
ruleKey == “name” 且 ruleValue == namei 。
统计并返回匹配检索规则的物品数量。
在这里插入图片描述

解题思路

简单地遍历即可。可以使用哈希表存储ruleKey和列索引的映射,避免每次都进行判断。

代码

class Solution {   public:    int countMatches(vector
>& items, string ruleKey, string ruleValue) { unordered_map
mp; mp["type"] = 0; mp["color"] = 1; mp["name"] = 2; int res = 0; for (int i = 0; i < items.size(); i ++) { if (items[i][mp[ruleKey]] == ruleValue) { res ++; } } return res; }};

转载地址:http://hoxj.baihongyu.com/

你可能感兴趣的文章
Nginx学习总结(11)——提高Nginx服务器的安全性,稳定性和性能的12种技巧
查看>>
Nginx学习总结(12)——Nginx各项配置总结
查看>>
Nginx学习总结(13)——Nginx 重要知识点回顾
查看>>
Nginx学习总结(14)——Nginx配置参数详细说明与整理
查看>>
Nginx学习总结(15)—— 提升 Web 应用性能的十个步骤
查看>>
Nginx学习总结(8)——Nginx服务器详解
查看>>
nginx学习笔记002---Nginx代理配置_案例1_实现了对前端代码的方向代理_并且配置了后端api接口的访问地址
查看>>
Nginx学习笔记(一) Nginx架构
查看>>
Nginx安装SSL模块 nginx: the “ssl” parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx
查看>>
nginx安装stream模块配置tcp/udp端口转发
查看>>
nginx安装Stream模块配置tcp/udp端口转发
查看>>
Nginx安装与常见命令
查看>>
nginx安装与配置
查看>>
Nginx安装及配置详解
查看>>
nginx安装并配置实现端口转发
查看>>
nginx安装配置
查看>>
Nginx实战之1.1-1.6 Nginx介绍,安装及配置文件详解
查看>>
Nginx实战经验分享:从小白到专家的成长历程!
查看>>
nginx实现二级域名转发
查看>>
Nginx实现动静分离
查看>>