题目

解题思路

  1. 通过遍历收集每个杆上存在的颜色种类;
  2. 选择存储数据的容器,Map加Set进行收集和去重;

代码展示

class Solution {
    public int countPoints(String rings) {
        int sum = 0;
        Map<Integer, Set<Character>> data = new HashMap<>();
        for (int i = 1; i < rings.length(); i += 2){
            Set<Character> temp = data.getOrDefault(rings.charAt(i) - 48, new HashSet<>());
            temp.add(rings.charAt(i - 1));
            data.put(rings.charAt(i) - 48, temp);
        }
        for (Set<Character> set : data.values()){
            if(set.size() == 3){
                sum++;
            }
        }
        return sum;
    }
}
11-03 04:59