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

非常感谢你阅读本文~
欢迎【点赞】【⭐收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子 https://le-yi.blog.csdn.net/ 博客原创~


文章目录

  • 1773. 统计匹配检索规则的物品数量:
  • 样例 1
  • 样例 2
  • 提示
  • 分析
  • 题解
    • java
    • c
    • c++
    • python
    • go
    • rust
  • 原题传送门:https://leetcode-cn.com/problems/count-items-matching-a-rule/


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>, 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
    }
}

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


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


你可能感兴趣的:(LeetCode力扣算法题目,java,c语言,python,go语言,rust)