LeetCode 1773. 统计匹配检索规则的物品数量缩略图

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

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

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

  • ruleKey == "type" 且 ruleValue == typei 。
  • ruleKey == "color" 且 ruleValue == colori 。
  • ruleKey == "name" 且 ruleValue == namei 。

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

1773. 统计匹配检索规则的物品数量 – 力扣(Leetcode)

LeetCode 1773. 统计匹配检索规则的物品数量插图
OpenCV步步精深-可心科创工作室

思路:

读懂就行, 题意是找到键对应值匹配的数量。

python3实现:

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        # 题意是找到键对应值匹配的数量
        type_list = []
        color_list = []
        name_list = []

        for elem in items:
            type_list.append(elem[0])
            color_list.append(elem[1])
            name_list.append(elem[2])
        
        # 定位idx
        if ruleKey == "type":
            if ruleValue in type_list:
                return type_list.count(ruleValue)
            else:
                return 0
        if ruleKey == "color":
            if ruleValue in color_list:
                return color_list.count(ruleValue)
            else:
                return 0
        if ruleKey == "name":
            if ruleValue in name_list:
                return name_list.count(ruleValue)
            else:
                return 0
LeetCode 1773. 统计匹配检索规则的物品数量插图1
OpenCV步步精深-可心科创工作室

更多干货见 OpenCV步步精深

作者 admin

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注