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

举报
二当家的白帽子 发表于 2022/04/06 11:16:48 2022/04/06
【摘要】 1773. 统计匹配检索规则的物品数量:给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配 :ruleKey == "type" 且 ruleVa...

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://bbs.huaweicloud.com/community/usersnew/id_1628396583336561 博客原创~


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200