LeetCode(String)1773. Count Items Matching a Rule
1.问题
You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.
The ith item is said to match the rule if one of the following is true:
ruleKey == “type” and ruleValue == typei.
ruleKey == “color” and ruleValue == colori.
ruleKey == “name” and ruleValue == namei.
Return the number of items that match the given rule.
Example 1:
Input: items = [[“phone”,“blue”,“pixel”],[“computer”,“silver”,“lenovo”],[“phone”,“gold”,“iphone”]], ruleKey = “color”, ruleValue = “silver”
Output: 1
Explanation: There is only one item matching the given rule, which is [“computer”,“silver”,“lenovo”].
Example 2:
Input: items = [[“phone”,“blue”,“pixel”],[“computer”,“silver”,“phone”],[“phone”,“gold”,“iphone”]], ruleKey = “type”, ruleValue = “phone”
Output: 2
Explanation: There are only two items matching the given rule, which are [“phone”,“blue”,“pixel”] and [“phone”,“gold”,“iphone”]. Note that the item [“computer”,“silver”,“phone”] does not match.
Constraints:
- 1 <= items.length <= 104
- 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
- ruleKey is equal to either “type”, “color”, or “name”.
- All strings consist only of lowercase letters.
2.解题思路
方法1:
1.定义返回值sum为0
2.遍历items,如果ruleKey为type,color, name时,//ruleValue和取出[0],[1],[2]位置比较,两值相等+1
3.返回sum的值
方法2:
1.定义index和sum的初始值为0
2.使用 switch case 识别列表中的哪个索引,为了匹配ruleValue
3 List item,结果显示一行数据,在根据索引的值比较是否相等,如果有相等元素+1 例:[“phone”,“blue”,“pixel”]
4.返回sum的值
3.代码
代码1:
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int sum=0;//1.定义返回值sum为0
for(int i =0;i<items.size();i++){
//2.遍历items,如果ruleKey为type,color, name时,//ruleValue和取出[0],[1],[2]位置比较,两值相等+1
if(ruleKey.equals("type")) {
if(ruleValue.equals(items.get(i).get(0))) sum++;
}else if(ruleKey.equals("color")){
if(ruleValue.equals(items.get(i).get(1))) sum++;
}else{
if(ruleValue.equals(items.get(i).get(2))) sum++;
}
}
return sum;//3.返回sum的值
}
}
代码2:
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int index = 0, sum = 0;//1.定义index和sum的初始值为0
//2.使用 switch case 识别列表中的哪个索引,为了匹配ruleValue
switch (ruleKey) {
case "type":
index = 0;
break;
case "color":
index = 1;
break;
default:
index = 2;
}
for (List<String> item : items) {
//3 List<String> item,结果显示一行数据,在根据索引的值比较是否相等,如果有相等元素+1 例:["phone","blue","pixel"]
if (item.get(index).equals(ruleValue))
sum ++;
}
return sum;//4.返回sum的值
}
和代码2解题思路相同,switch换成if进行判断
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int result = 0;
int index = 0;
if(ruleKey.equals("color")) index = 1;
if(ruleKey.equals("name")) index = 2;
for(int i = 0 ; i< items.size() ; i++)
if((items.get(i).get(index)).equals(ruleValue) ) result++;
return result;
}
}
IDEA本地运行代码
为了了解 List
items的数据结构,添加了运行在本地代码,方便学习和理解!
public class Test {
public static void main(String args[]){
List<List<String>> items = new ArrayList<>();
List<String> l1 =new ArrayList<>();
l1.add("phone");
l1.add("blue");
l1.add("pixel");
List<String> l2 =new ArrayList<>();
l2.add("computer");
l2.add("silver");
l2.add("lenovo");
List<String> l3 =new ArrayList<>();
l3.add("phone");
l3.add("gold");
l3.add("iphone");
items.add(l1);
items.add(l2);
items.add(l3);
String ruleKey = "color";
String ruleValue ="silver";
System.out.println(countMatches(items, ruleKey,ruleValue));
}
public static int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int sum=0;//1.定义返回值sum为0
for(int i =0;i<items.size();i++){
//2.遍历items,如果ruleKey为type,color, name时,//ruleValue和取出[0],[1],[2]位置比较,两值相等+1
if(ruleKey.equals("type")) {
if(ruleValue.equals(items.get(i).get(0))) sum++;
}else if(ruleKey.equals("color")){
if(ruleValue.equals(items.get(i).get(1))) sum++;
}else{
if(ruleValue.equals(items.get(i).get(2))) sum++;
}
}
return sum;//3.返回sum的值
}
}
1
还没有评论,来说两句吧...