LeetCode(String)1773. Count Items Matching a Rule

落日映苍穹つ 2023-09-23 22:05 108阅读 0赞

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:

  1. class Solution {
  2. public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
  3. int sum=0;//1.定义返回值sum为0
  4. for(int i =0;i<items.size();i++){
  5. //2.遍历items,如果ruleKey为type,color, name时,//ruleValue和取出[0],[1],[2]位置比较,两值相等+1
  6. if(ruleKey.equals("type")) {
  7. if(ruleValue.equals(items.get(i).get(0))) sum++;
  8. }else if(ruleKey.equals("color")){
  9. if(ruleValue.equals(items.get(i).get(1))) sum++;
  10. }else{
  11. if(ruleValue.equals(items.get(i).get(2))) sum++;
  12. }
  13. }
  14. return sum;//3.返回sum的值
  15. }
  16. }

代码2:

  1. public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
  2. int index = 0, sum = 0;//1.定义index和sum的初始值为0
  3. //2.使用 switch case 识别列表中的哪个索引,为了匹配ruleValue
  4. switch (ruleKey) {
  5. case "type":
  6. index = 0;
  7. break;
  8. case "color":
  9. index = 1;
  10. break;
  11. default:
  12. index = 2;
  13. }
  14. for (List<String> item : items) {
  15. //3 List<String> item,结果显示一行数据,在根据索引的值比较是否相等,如果有相等元素+1 例:["phone","blue","pixel"]
  16. if (item.get(index).equals(ruleValue))
  17. sum ++;
  18. }
  19. return sum;//4.返回sum的值
  20. }

和代码2解题思路相同,switch换成if进行判断

  1. class Solution {
  2. public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
  3. int result = 0;
  4. int index = 0;
  5. if(ruleKey.equals("color")) index = 1;
  6. if(ruleKey.equals("name")) index = 2;
  7. for(int i = 0 ; i< items.size() ; i++)
  8. if((items.get(i).get(index)).equals(ruleValue) ) result++;
  9. return result;
  10. }
  11. }

IDEA本地运行代码

为了了解 List items的数据结构,添加了运行在本地代码,方便学习和理解!

  1. public class Test {
  2. public static void main(String args[]){
  3. List<List<String>> items = new ArrayList<>();
  4. List<String> l1 =new ArrayList<>();
  5. l1.add("phone");
  6. l1.add("blue");
  7. l1.add("pixel");
  8. List<String> l2 =new ArrayList<>();
  9. l2.add("computer");
  10. l2.add("silver");
  11. l2.add("lenovo");
  12. List<String> l3 =new ArrayList<>();
  13. l3.add("phone");
  14. l3.add("gold");
  15. l3.add("iphone");
  16. items.add(l1);
  17. items.add(l2);
  18. items.add(l3);
  19. String ruleKey = "color";
  20. String ruleValue ="silver";
  21. System.out.println(countMatches(items, ruleKey,ruleValue));
  22. }
  23. public static int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
  24. int sum=0;//1.定义返回值sum为0
  25. for(int i =0;i<items.size();i++){
  26. //2.遍历items,如果ruleKey为type,color, name时,//ruleValue和取出[0],[1],[2]位置比较,两值相等+1
  27. if(ruleKey.equals("type")) {
  28. if(ruleValue.equals(items.get(i).get(0))) sum++;
  29. }else if(ruleKey.equals("color")){
  30. if(ruleValue.equals(items.get(i).get(1))) sum++;
  31. }else{
  32. if(ruleValue.equals(items.get(i).get(2))) sum++;
  33. }
  34. }
  35. return sum;//3.返回sum的值
  36. }
  37. }
  38. 1

发表评论

表情:
评论列表 (有 0 条评论,108人围观)

还没有评论,来说两句吧...

相关阅读