题解 | #不同的体重#
不同的体重
https://www.nowcoder.com/practice/4a6411ef749445e88baf7f93d1458505
题目考察的知识点
考察哈希表的应用
题目解答方法的文字分析
构建<重量类别:数量>的哈希表后,遍历哈希表,这里利用Set数据结构的特性来帮助判断,具体细节见代码即可。
本题解析所用的编程语言
使用Java代码解答
完整且正确的编程代码
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型一维数组
* @return bool布尔型
*/
public boolean uniqueOccurrences (int[] arr) {
// write code here
HashMap<Integer, Integer> map = new HashMap<>();
for(int weight:arr){
if(map.containsKey(weight)){
map.put(weight,map.get(weight)+1);
}else{
map.put(weight,1);
}
}
HashSet<Integer> set = new HashSet<>();
for(Integer key:map.keySet()){
if(set.contains(map.get(key))) return false;
set.add(map.get(key));
}
return true;
}
}



