起初:
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int key = 0;
int num = 0;
if(ruleKey=="type"){key = 0;}
if(ruleKey=="color"){key = 1;}
if(ruleKey=="name"){key = 2;}
for(int i = 0; i< items.size(); i++){
if(items.get(i).get(key)==ruleValue){
num++;
}
}
return num;
}
}
提交发现错误。
怀疑是字符串匹配语句出现问题
更改:(使用equals方法)
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int key = 0;
int num = 0;
if(ruleKey.equals("color")){key = 1;}
if(ruleKey.equals("name")){key = 2;}
for(int i = 0; i< items.size(); i++){
if(ruleValue.equals(items.get(i).get(key))){
num++;
}
}
return num;
}
}