题解 | #农场牛的标识#
农场牛的标识
https://www.nowcoder.com/practice/a9dfab1f870046e4bd829c8a9223f9bc
题目考察的知识点
考察哈希表应用
题目解答方法的文字分析
遍历存储每个数字以及出现的次数,因为只有一个出现一次,后面再遍历哈希表将他挑出来就可以了。
本题解析所用的编程语言
使用Java解答
完整且正确的编程代码
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int singleNumber (int[] nums) { // write code here HashMap<Integer,Integer> map = new HashMap<>(); for(int num:nums){ if(map.containsKey(num)){ map.put(num,map.get(num)+1); }else{ map.put(num,1); } } for(Map.Entry<Integer,Integer> entry:map.entrySet()){ if(entry.getValue()==1) return entry.getKey(); } return 0; } }