标签:
<span style="font-size:18px;">public class Solution { public int GetNumberOfK(int [] array , int k) { int num=0; for(int i=0;i<array.length;i++){ if(array[i]==k){ num++; } } return num; } }</span>代码2:
<span style="font-size:18px;">import java.util.*; public class Solution { public int GetNumberOfK(int [] array , int k) { if(array == null || array.length == 0) return 0; HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i = 0;i<array.length;i++) { if(map.containsKey(new Integer(array[i]))) map.put(array[i],map.get(array[i]) + 1); else map.put(array[i],1); } if(!map.containsKey(k)) return 0; return map.get(k); } }</span>
标签:
原文地址:http://blog.csdn.net/baidu_21578557/article/details/51464281