给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。
另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。
如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配 :
ruleKey == "type" 且 ruleValue == typei 。
ruleKey == "color" 且 ruleValue == colori 。
ruleKey == "name" 且 ruleValue == namei 。
统计并返回 匹配检索规则的物品数量 。
虽然是三维数组,但其实问题不大,有一维是字符串,其实是二维的字符串数组
int countMatches(char *** items, int itemsSize, int* itemsColSize, char * ruleKey, char * ruleValue){
int i;
int ans = 0, cmpIdx;
for(i = 0; i < itemsSize; ++i) {
if(strcmp(ruleKey, "type") == 0) {
cmpIdx = 0;
}else if(strcmp(ruleKey, "color") == 0) {
cmpIdx = 1;
}else if(strcmp(ruleKey, "name") == 0) {
cmpIdx = 2;
}else {
cmpIdx = 0;
}
if(strcmp(items[i][cmpIdx], ruleValue) == 0)
++ ans;
}
return ans;
}