题解 | #农场牛的标识II#
农场牛的标识II
https://www.nowcoder.com/practice/a69dc54eeffa43218d06f62cc733c6c0
题目考察的知识点
考察哈希表的应用
题目解答方法的文字分析
其实跟上一道题目一模一样,直接套用就行。用哈希表存储出现的次数,最后取出只出现一次的就行了。
本题解析所用的编程语言
使用Java解答
完整且正确的编程代码
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int singleNumberII (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; } }