題目連結(jié):
題目意譯:
你被給定一個(gè)陣列 items,其中每個(gè) items[i] = [typei, colori, namei] 依序代表著第 i 個(gè)物品的類型、顏色以及名稱。你同時(shí)也被給定一個(gè)規(guī)定,其以兩個(gè)字串 ruleKey 和 ruleValue 所表示。
如果以下條件之一為真,則第 i 個(gè)物品視為是符合規(guī)定的:
ruleKey == "type" 且 ruleValue == typei.
ruleKey == "color" 且 ruleValue == colori.
ruleKey == "name" 且 ruleValue == namei.
回傳符合給定的規(guī)定之物品數(shù)量。
限制:
1 ≦ items.length ≦ 10 ^ 4
1 ≦ typei.length, colori.length, namei.length, ruleValue.length ≦ 10
ruleKey 為 "type" 、 "color" 或是 "name" 之一。
所有字串由小寫英文字母組成。
範(fàn)例測資:
範(fàn)例 1:
輸入: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
輸出: 1
解釋: 只有一件物品符合規(guī)定,其為 ["computer","silver","lenovo"]。
範(fàn)例 2:
輸入: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
輸出: 2
解釋: 有兩件物品符合規(guī)定,其為 ["phone","blue","pixel"] 和 ["phone","gold","iphone"]。注意到 ["computer","silver","phone"] 這個(gè)物品並不符合。
解題思維:
就是單純地用迴圈把所有物品掃過一次看哪些物品符合 ruleKey 且又符合 ruleValue,統(tǒng)計(jì)數(shù)量即可。
此次分享到此為止,如有任何更加簡潔的想法或是有說明不清楚之地方,也煩請各位大大撥冗討論。