【算法学习】1773. 统计匹配检索规则的物品数量(java / c / c++ / python / go / rust)

简介: 给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配 :ruleKey == "type" 且 ruleValue == typei 。ruleKey == "color" 且 ruleValue == colori 。ruleKey == "name" 且 ruleValue == namei 。统计并返回 匹配检索规则的物品数量

1773. 统计匹配检索规则的物品数量:

给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。

另给你一条由两个字符串 ruleKeyruleValue 表示的检索规则。

如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配

ruleKey == "type" 且 ruleValue == typei 。
ruleKey == "color" 且 ruleValue == colori 。
ruleKey == "name" 且 ruleValue == namei 。

统计并返回 匹配检索规则的物品数量

样例 1

输入:
  items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
  
输出:
  1
  
解释:
  只有一件物品匹配检索规则,这件物品是 ["computer","silver","lenovo"] 。

样例 2

输入:
  items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
  
输出:
  2
  
解释:
  只有两件物品匹配检索规则,这两件物品分别是 ["phone","blue","pixel"] 和 ["phone","gold","iphone"] 。注意,["computer","silver","phone"] 未匹配检索规则。

提示

  • 1 <= items.length <= 104
  • 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
  • ruleKey 等于 "type"、"color" 或 "name"
  • 所有字符串仅由小写字母组成

分析

  • 这道算法题是简单题,也没想到什么能特别优化的地方。
  • 主要是 ruleKey 只有 "type"、"color" 或 "name"三种值,直接上if判断就可以了,用hash表好像除了代码看起来好看外,性能上也差不多。
  • 如果 ruleKey 的可取值范围大的话,就应该用hash表了。

题解

java

class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        // 类型转换可以根据类型的多少去考虑,如果多的话,肯定是hash表比较好
        int type;
        switch(ruleKey) {
          case "type":
            type = 0;
            break;
          case "color":
            type = 1;
            break;
          default:
            // name
            type = 2;
            break;
        }
        int ans = 0;
        // 下面好像没有什么技巧
        for (List<String> item : items) {
          if (ruleValue.equals(item.get(type))) {
            ++ans;
          }
        }
        return ans;
    }
}

c

int countMatches(char *** items, int itemsSize, int* itemsColSize, char * ruleKey, char * ruleValue){
    int type;
    if (strcmp(ruleKey, "type") == 0) {
        type = 0;
    } else if (strcmp(ruleKey, "color") == 0) {
        type = 1;
    } else {
        // name
        type = 2;
    }
    int ans = 0;
    // 下面好像没有什么技巧
    for (int i = 0; i < itemsSize; ++i) {
        if (strcmp(ruleValue, items[i][type]) == 0) {
            ++ans;
        }
    }
    return ans;
}

c++

class Solution {
public:
    int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
        // 类型转换可以根据类型的多少去考虑,如果多的话,肯定是hash表比较好
        int type;
        if ("type" == ruleKey) {
            type = 0;
        } else if ("color" == ruleKey) {
            type = 1;
        } else {
            // name
            type = 2;
        }
        int ans = 0;
        // 下面好像没有什么技巧
        for (vector<string> &item : items) {
            if (ruleValue == item[type]) {
                ++ans;
            }
        }
        return ans;
    }
};

python

class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        if "type" == ruleKey:
            types = 0
        elif "color" == ruleKey:
            types = 1
        else:
            types = 2
        return sum(item[types] == ruleValue for item in items)

go

func countMatches(items [][]string, ruleKey string, ruleValue string) int {
    // 类型转换可以根据类型的多少去考虑,如果多的话,肯定是hash表比较好
    var types int
    switch ruleKey {
      case "type":
        types = 0
      case "color":
        types = 1
      default:
        types = 2
    }
    ans := 0
    // 下面好像没有什么技巧
    for _, item := range items {
      if ruleValue == item[types] {
        ans++
      }
    }
    return ans
}

rust

impl Solution {
    pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
        let types = match rule_key.as_str() {
            "type" => 0,
            "color" => 1,
            _ => 2
        };
        items.iter().filter(|item| {
            rule_value == item[types]
        }).count() as i32
    }
}

在这里插入图片描述


原题传送门:https://leetcode-cn.com/problems/count-items-matching-a-rule/


非常感谢你阅读本文~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://developer.aliyun.com/profile/sqd6avc7qgj7y 博客原创~

相关文章
|
1月前
|
算法 搜索推荐 Java
数据结构与算法(Java篇)笔记--希尔排序
数据结构与算法(Java篇)笔记--希尔排序
|
1月前
|
算法 Java
[Java·算法·中等] LeetCode15. 三数之和
[Java·算法·中等] LeetCode15. 三数之和
30 0
|
6天前
|
机器学习/深度学习 算法 前端开发
Scikit-learn进阶:探索集成学习算法
【4月更文挑战第17天】本文介绍了Scikit-learn中的集成学习算法,包括Bagging(如RandomForest)、Boosting(AdaBoost、GradientBoosting)和Stacking。通过结合多个学习器,集成学习能提高模型性能,减少偏差和方差。文中展示了如何使用Scikit-learn实现这些算法,并提供示例代码,帮助读者理解和应用集成学习提升模型预测准确性。
|
6天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
17 0
|
11天前
|
机器学习/深度学习 数据可视化 数据挖掘
用Python进行健康数据分析:挖掘医疗统计中的信息
【4月更文挑战第12天】Python在医疗健康数据分析中扮演重要角色,具备数据处理、机器学习、可视化及丰富生态的优势。基本流程包括数据获取、预处理、探索、模型选择与训练、评估优化及结果可视化。应用案例包括疾病预测、药物效果分析和医疗资源优化,例如使用RandomForestClassifier进行疾病预测,Logit模型分析药物效果,以及linprog优化医疗资源配置。
|
13天前
|
算法 测试技术 C#
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II
|
13天前
|
算法
【算法学习--字符串】(不含KMP算法)
【算法学习--字符串】(不含KMP算法)
|
14天前
|
算法 安全 Java
java代码 实现AES_CMAC 算法测试
该代码实现了一个AES-CMAC算法的简单测试,使用Bouncy Castle作为安全提供者。静态变量K定义了固定密钥。`Aes_Cmac`函数接受密钥和消息,返回AES-CMAC生成的MAC值。在`main`方法中,程序对给定的消息进行AES-CMAC加密,然后模拟接收ECU的加密结果并进行比较。如果两者匹配,输出&quot;验证成功&quot;,否则输出&quot;验证失败&quot;。辅助方法包括将字节转为16进制字符串和将16进制字符串转为字节。
|
16天前
|
测试技术 Python
288统计出现最多次的字母(PYTHON)
288统计出现最多次的字母(PYTHON)
|
24天前
|
存储 算法 JavaScript
Java入门高频考查算法逻辑基础知识3-编程篇(超详细18题1.8万字参考编程实现)
解决这类问题时,建议采取下面的步骤: 理解数学原理:确保你懂得基本的数学公式和法则,这对于制定解决方案至关重要。 优化算法:了解时间复杂度和空间复杂度,并寻找优化的机会。特别注意避免不必要的重复计算。 代码实践:多编写实践代码,并确保你的代码是高效、清晰且稳健的。 错误检查和测试:要为你的代码编写测试案例,测试标准的、边缘情况以及异常输入。 进行复杂问题简化:面对复杂的问题时,先尝试简化问题,然后逐步分析和解决。 沟通和解释:在编写代码的时候清晰地沟通你的思路,不仅要写出正确的代码,还要能向面试官解释你的
33 0

热门文章

最新文章