Leetcode 1773. Count Items Matching a Rule

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Count Items Matching a Rule

2. Solution

解析:根据对应的索引比较对应的值即可。

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
index = 0
if ruleKey == 'color':
index = 1
elif ruleKey == 'name':
index = 2
count = 0
for item in items:
if item[index] == ruleValue:
count += 1
return count

Reference

  1. https://leetcode.com/problems/count-items-matching-a-rule/
如果有收获,可以请我喝杯咖啡!