解题思路
根据题意进行模拟即可,利用哈希表把输入的ruleKey
转换为items[i]
的下标,然后再遍历一遍items
,找出符合条件的物品数量。
代码如下:
class Solution {
public:
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
unordered_map<string, int> dictionary = {{"type", 0}, {"color", 1}, {"name", 2}};
int res = 0, index = dictionary[ruleKey];
for (auto &&item : items) {
if (item[index] == ruleValue) {
res++;
}
}
return res;
}
};
标签:index,1773,int,items,rule,ruleKey,res
From: https://www.cnblogs.com/hql5/p/16839392.html