首頁>技術>

題目

給你一個數組 items ,其中 items[i] = [typei, colori, namei] ,

描述第 i 件物品的型別、顏色以及名稱。

另給你一條由兩個字串 ruleKey 和 ruleValue 表示的檢索規則。

如果第 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"

所有字串僅由小寫字母組成

解題思路分析

1、遍歷;時間複雜度O(n),空間複雜度O(1)

func countMatches(items [][]string, ruleKey string, ruleValue string) int {	res := 0	for i := 0; i < len(items); i++ {		if ruleKey == "type" && items[i][0] == ruleValue {			res++		} else if ruleKey == "color" && items[i][1] == ruleValue {			res++		} else if ruleKey == "name" && items[i][2] == ruleValue {			res++		}	}	return res}
總結

Easy題目,直接遍歷判斷即可

6
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • JavaScript ES6-ES2021中激動人心的新特性