题目:统计一个数字在排序数组中出现的次数。例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数组红出现了4次,因此输出4.
public class count{ public int getCount(int[] array,int num){ if(array == null || array.length == 0) return -1; else if(array[0] == array[array.length-1]){ if(num == array[0]) return array.length; else return 0; }else{ int count = 0; for(int i=0;i<array.length;i++){ if(num == array[i]) count++; } return count; } } public static void main(String[] args){ int[] array = {1,2,3,3,3,3,4,5}; count c = new count(); int result = c.getCount(array,5); System.out.println(result); } }