数字在排序数组中出现的次数
题目描述
统计一个数字在排序数组中出现的次数。
1 /** 2 * 3 * @author gentleKay 4 * 题目描述 5 * 统计一个数字在排序数组中出现的次数。 6 */ 7 8 public class Main36 { 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 int[] array = {1,2,3,4,5,1,2,3,4,5,2,1,2,2,3,4,5,3,3,2,1,1}; 13 int count = Main36.GetNumberOfK(array, 2); 14 System.out.println(count); 15 } 16 17 public static int GetNumberOfK(int [] array , int k) { 18 if (array == null || array.length == 0) { 19 return 0; 20 } 21 int count = 0; 22 for (int i=0;i<array.length;i++) { 23 if (array[i] == k) { 24 count++; 25 } 26 } 27 return count; 28 } 29 30 }