您当前的位置: 首页 >  liyatjj leetcode

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

liyatjj 发布时间:2022-06-02 11:36:16 ,浏览量:5

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

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

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

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

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

示例 1:

输入:items = [[“phone”,“blue”,“pixel”],[“computer”,“silver”,“lenovo”],[“phone”,“gold”,“iphone”]], ruleKey = “color”, ruleValue = “silver” 输出:1 解释:只有一件物品匹配检索规则,这件物品是 [“computer”,“silver”,“lenovo”] 。

来源:LeetCode

首先要读懂题,题目大概就是先寻找所给出的ruleKey是type还是color还是name,从而得到索引值,然后遍历集合items,从i到items.size(),匹配第i个元素的第num个值,看是否和keyValue一致,一致的话加1。

class Solution {
    public int countMatches(List items, String ruleKey, String ruleValue) {
        int num=0,sum=0;
        if(ruleKey.equals("type")){
            num=0;
        }
        else if(ruleKey.equals("color")){
            num=1;
        }
        else{
            num=2;
        }
        for(int i=0;i            
关注
打赏
1688896170
查看更多评论
0.0480s