列表

详情


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

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

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

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

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

 

示例 1:

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

示例 2:

输入:items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
输出:2
解释:只有两件物品匹配检索规则,这两件物品分别是 ["phone","blue","pixel"] 和 ["phone","gold","iphone"] 。注意,["computer","silver","phone"] 未匹配检索规则。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) { } };

rust 解法, 执行用时: 8 ms, 内存消耗: 4 MB, 提交时间: 2023-09-12 17:57:10

/*
impl Solution {
    pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
        items.iter().fold(0, |cnt, item| (item[match &rule_key as &str { "type" => 0, "color" => 1, "name" => 2, _ => 3}] == rule_value) as i32 + cnt)
    }
}
*/

impl Solution {
    pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
        items.iter().fold(0, |cnt, item| match rule_key.as_str() {
            "type" => if item[0] == rule_value { cnt + 1 } else { cnt }
            "color" => if item[1] == rule_value { cnt + 1 } else { cnt }
            "name" => if item[2] == rule_value { cnt + 1 } else { cnt }
            _ => cnt
        })
    }
}

/*
impl Solution {
    pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
        let i = match rule_key.as_str() {
            "type" => 0,
            "color" => 1,
            "name" => 2,
            _ => panic!(),
        };
        
        items
            .into_iter()
            .filter(|item| item[i] == rule_value)
            .count() as i32
    }
}
*/

javascript 解法, 执行用时: 72 ms, 内存消耗: 44.9 MB, 提交时间: 2023-09-12 17:55:51

/**
 * @param {string[][]} items
 * @param {string} ruleKey
 * @param {string} ruleValue
 * @return {number}
 */
var countMatches = function(items, ruleKey, ruleValue) {
    const index = {"type":0, "color":1, "name":2}[ruleKey];
    let res = 0;
    for (const item of items) {
        if (item[index] === ruleValue) {
            res++;
        }
    }
    return res;
};

java 解法, 执行用时: 4 ms, 内存消耗: 46.6 MB, 提交时间: 2023-09-12 17:55:21

class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        int index = new HashMap<String, Integer>() {{
            put("type", 0);
            put("color", 1);
            put("name", 2);
        }}.get(ruleKey);
        int res = 0;
        for (List<String> item : items) {
            if (item.get(index).equals(ruleValue)) {
                res++;
            }
        }
        return res;
    }
}

python3 解法, 执行用时: 68 ms, 内存消耗: 21.2 MB, 提交时间: 2023-09-12 17:55:01

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        index = {"type": 0, "color": 1, "name": 2}[ruleKey]
        return sum(item[index] == ruleValue for item in items)

golang 解法, 执行用时: 40 ms, 内存消耗: 6.9 MB, 提交时间: 2021-06-09 10:47:38

func countMatches(items [][]string, ruleKey string, ruleValue string) int {
    ans := 0
    i := 0
    if ruleKey == "color" {
        i = 1
    } else if ruleKey == "name" {
        i = 2
    }

    for _, item := range items {
        if item[i] == ruleValue {
            ans++
        }
    }
    return ans
}

上一题