题目一:数字在排序数组中出现的次数。
统计一个数字在排序数组中出现的次数。例如,输入排序
数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数
组中出现了4次,因此输出4。
设计一个改进的二分查找法,
分别查找数组中第一个该数字和最后一个该数字,
找到之后;利用索引计算中间该数字的个数;
package helen.c;
public class ArrayFind {
void test(String testName, int data[], int k, int expected){//{3, 3, 3, 3, 4, 5};
if(testName != null)
System.out.printf("%s begins: ", testName);
int result = arrayFind(data, k);
if(result == expected)
System.out.printf("Passed.\n");
else
System.out.printf("Failed.\n");
}
// 查找的数字出现在数组的中间
void test1(){
int data[] = {1, 2, 3, 3, 3, 3, 4, 5};
test("Test1", data, 3, 4);
}
// 查找的数组出现在数组的开头
void test2(){
int data[] = {3, 3, 3, 3, 4, 5};
test