import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param arr int整型一维数组 * @return bool布尔型 */ public boolean uniqueOccurrences (int[] arr) { int [] number = new int [1001]; for (int i = 0; i < arr.length; i++) { number[arr[i]]++; } HashSet<Integer> hashSet = new HashSet<>(); for (int i = 0; i < number.length; i++) { if(number[i]>0){ if(hashSet.contains(number[i])){ return false; }else{ hashSet.add(number[i]); } } } return true; } }
本题知识点分析:
1.哈希表hashSet去重
2.数组遍历
3.数学模拟,统计
本题解题思路分析:
1.先统计各种牛的数量,因为题目中说了,体重在0到1000,常量级,我直接用数组做
2.只要数组中大于0的说明有这个体重的牛群,每次去判断hashSet是否已经有这个数量,如果没有,添加
3.如果hashSet重复,返回false
4.如果跳出循环,返回true,说明数量都不相同。