简单模拟题,需要做的就是按照规则用map把物品名称映射一下,然后再遍历数组中的每个元素,如果符合要求就给结果+1即可,最后输出其中的内容。
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 index = dictionary[ruleKey];
int res=0;
for(auto &&item : items){
if(item[index] == ruleValue) res++;
}
return res;
}
};