-
[LeetCode] 1773. Count Items Matching a RuleLeetCode 2021. 11. 9. 17:25728x90
https://leetcode.com/problems/count-items-matching-a-rule/
Count Items Matching a Rule - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Count Items Matching a Rule - Easy

[타입, 색, 이름] 의 형태로 item들이 저장되어있다.
이 때, ruleKey (타입, 색, 이름) 가 ruleValue인 것의 개수를 구하는 문제다.
먼저 key라는 변수를 만들어 ruleKey가 type 일경우 0, color일 경우 1, name일 경우 2로 저장한다.
그리고 item들을 하나씩 보며 item[key] 가 ruleValue와 같다면 ret++을 해주자.
전체 풀이 코드
class Solution { public: int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) { int ret=0, key = ruleKey=="type"?0:ruleKey=="color"?1:2; for (auto item:items) if(item[key]==ruleValue) ret++; return ret; } };'LeetCode' 카테고리의 다른 글
[LeetCode] 1413. Minimum Value to Get Positive Step by Step Sum (0) 2021.11.11 [LeetCode] 781. Rabbits in Forest (0) 2021.11.10 [LeetCode] 20. Valid Parentheses (0) 2021.11.09 [LeetCode] 1302. Deepest Leaves Sum (0) 2021.11.09 [LeetCode] 1178. Number of Valid Words for Each Puzzle (0) 2021.11.09