返回介绍

solution / 1700-1799 / 1773.Count Items Matching a Rule / README

发布于 2024-06-17 01:03:14 字数 4685 浏览 0 评论 0 收藏 0

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

English Version

题目描述

给你一个数组 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",我们可以直接取 ruleKey 的第一个字符来确定 item 的下标 $i$。然后遍历 items 数组,统计 item[i] == ruleValue 的个数即可。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 items 的长度。

class Solution:
  def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
    i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
    return sum(v[i] == ruleValue for v in items)
class Solution {
  public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
    int i = ruleKey.charAt(0) == 't' ? 0 : (ruleKey.charAt(0) == 'c' ? 1 : 2);
    int ans = 0;
    for (var v : items) {
      if (v.get(i).equals(ruleValue)) {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
    int i = ruleKey[0] == 't' ? 0 : (ruleKey[0] == 'c' ? 1 : 2);
    return count_if(items.begin(), items.end(), [&](auto& v) { return v[i] == ruleValue; });
  }
};
func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int) {
  i := map[byte]int{'t': 0, 'c': 1, 'n': 2}[ruleKey[0]]
  for _, v := range items {
    if v[i] == ruleValue {
      ans++
    }
  }
  return
}
function countMatches(items: string[][], ruleKey: string, ruleValue: string): number {
  const key = ruleKey === 'type' ? 0 : ruleKey === 'color' ? 1 : 2;
  return items.reduce((r, v) => r + (v[key] === ruleValue ? 1 : 0), 0);
}
impl Solution {
  pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
    let key = if rule_key == "type" { 0 } else if rule_key == "color" { 1 } else { 2 };
    items
      .iter()
      .filter(|v| v[key] == rule_value)
      .count() as i32
  }
}
int countMatches(char*** items, int itemsSize, int* itemsColSize, char* ruleKey, char* ruleValue) {
  int k = strcmp(ruleKey, "type") == 0 ? 0 : strcmp(ruleKey, "color") == 0 ? 1
                                       : 2;
  int res = 0;
  for (int i = 0; i < itemsSize; i++) {
    if (strcmp(items[i][k], ruleValue) == 0) {
      res++;
    }
  }
  return res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文