首页 > 其他分享 >1773. 统计匹配检索规则的物品数量

1773. 统计匹配检索规则的物品数量

时间:2022-10-29 11:12:25浏览次数:53  
标签:检索 匹配 1773 ruleValue ruleKey 规则 物品

1773. 统计匹配检索规则的物品数量

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

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

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

ruleKey == "type" 且 ruleValue == typei 。
ruleKey == "color" 且 ruleValue == colori 。
ruleKey == "name" 且 ruleValue == namei 。
统计并返回 匹配检索规则的物品数量 。

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

标签:检索,匹配,1773,ruleValue,ruleKey,规则,物品
From: https://www.cnblogs.com/SkyDusty/p/16838278.html

相关文章