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

  1. class Solution {
  2. public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
  3. int count = 0;
  4. if (ruleKey.equals("type")) {
  5. for (List<String> item : items ) {
  6. if (item.get(0).equals(ruleValue))
  7. ++count;
  8. }
  9. } else if (ruleKey.equals("color")) {
  10. for (List<String> item : items ) {
  11. if (item.get(1).equals(ruleValue))
  12. ++count;
  13. }
  14. } else { //name
  15. for (List<String> item : items ) {
  16. if (item.get(2).equals(ruleValue))
  17. ++count;
  18. }
  19. }
  20. return count;
  21. }
  22. }