题解 | #不同的体重#
不同的体重
https://www.nowcoder.com/practice/4a6411ef749445e88baf7f93d1458505
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param arr int整型一维数组 * @return bool布尔型 */ public boolean uniqueOccurrences (int[] arr) { // write code here 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,说明数量都不相同。
编程语言:
java