博客
关于我
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/

你可能感兴趣的文章
Nexus(1):Nexus的安装与配置
查看>>
NFinal学习笔记 02—NFinalBuild
查看>>
NFS
查看>>
NFS Server及Client配置与挂载详解
查看>>
NFS 服务配置篇
查看>>
NFS共享文件系统搭建
查看>>
nfs复习
查看>>
NFS安装配置
查看>>
NFS服务器配置-服务启动与停止
查看>>
NFS的安装以及windows/linux挂载linux网络文件系统NFS
查看>>
NFS的常用挂载参数
查看>>
NFS网络文件系统
查看>>
NFS远程目录挂载
查看>>
nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
查看>>
NFV商用可行新华三vBRAS方案实践验证
查看>>
ng build --aot --prod生成文件报错
查看>>
ng 指令的自定义、使用
查看>>
ng6.1 新特性:滚回到之前的位置
查看>>
nghttp3使用指南
查看>>
Nginx
查看>>