题解 | #不同的体重#
不同的体重
https://www.nowcoder.com/practice/4a6411ef749445e88baf7f93d1458505?tpId=354&tqId=10595896&ru=/exam/oj&qru=/ta/interview-202-top/question-ranking&sourceUrl=%2Fexam%2Foj
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,说明数量都不相同。
本题使用编程语言: Java
高频面试算法题解 文章被收录于专栏
高频面试算法题解,每天一小步,人生一大步,跟着一起刷起来!