【剑指Offer 38】数字在排序数组中出现的次数
2017-07-22 本文已影响11人
3e1094b2ef7b
题目:统计一个数字:在排序数组中出现的次数。
代码如下:
package demo;
/**
* 数字在排序数组中出现的次数
*
* @author xiangdonglee
*
*/
public class Test38 {
/**
* 找排序数组中的第一个k
*
* @param data
* 排序数组
* @param k
* 要查找的k
* @param start
* @param end
* @return 第一个k在数组中的下标
*/
private static int getFirstK(int[] data, int k, int start, int end) {
if (data == null || data.length < 1 || start > end) {
return -1;
}
int middleIndex = (start + end) / 2;
int middleData = data[middleIndex];
if (middleData == k) {
if ((middleIndex > 0 && data[middleIndex - 1] != k) || middleIndex == 0) {
return middleIndex;
} else {
end = middleIndex - 1;
}
} else if (middleIndex > k) {
end = middleIndex - 1;
} else {
start = middleIndex + 1;
}
return getFirstK(data, k, start, end);
}
/**
* 求排序数组中的最后一个k
*
* @param data
* @param k
* @param start
* @param end
* @return
*/
private static int getLastK(int[] data, int k, int start, int end) {
if (data == null || data.length < 1 || start > end) {
return -1;
}
int middleIndex = (start + end) / 2;
int middleData = data[middleIndex];
if (middleData == k) {
if ((middleIndex < data.length - 1 && data[middleIndex + 1] != k) || middleIndex == data.length - 1) {
return middleIndex;
} else {
start = middleIndex + 1;
}
} else if (middleData < k) {
start = middleIndex + 1;
} else {
end = middleIndex - 1;
}
return getLastK(data, k, start, end);
}
/**
* 求k在排序数组中出现的次数
*
* @param data
* 排序数组
* @param k
* 要查找的k
* @return k在data数组中出现的次数
*/
public static int getNumberOfK(int[] data, int k) {
int number = 0;
if (data != null && data.length > 0) {
int first = getFirstK(data, k, 0, data.length - 1);
int last = getLastK(data, k, 0, data.length - 1);
if (first > -1 && last > -1) {
number = last - first + 1;
}
}
return number;
}
public static void main(String[] args) {
System.out.println("查找的数字出现在数组中间:");
int[] data1 = { 1, 2, 3, 3, 3, 3, 4, 5 };
System.out.println("1,2,3,3,3,3,4,5中,3出现的次数:" + getNumberOfK(data1, 3));
System.out.println();
System.out.println("查找的数字出现在数组开头:");
int[] data2 = { 3, 3, 3, 3, 1, 2, 4, 5 };
System.out.println("3,3,3,3,1,2,4,5中,3出现的次数:" + getNumberOfK(data2, 3));
System.out.println();
System.out.println("查找的数字出现在数组结尾:");
int[] data3 = { 1, 2, 4, 5, 3, 3, 3, 3 };
System.out.println("1,2,4,5,3,3,3,3中,3出现的次数:" + getNumberOfK(data3, 3));
System.out.println();
System.out.println("查找的数字不存在:");
int[] data4 = { 1, 3, 3, 3, 3, 4, 5 };
System.out.println("1,3,3,3,3,4,5中,2出现的次数:" + getNumberOfK(data4, 2));
System.out.println();
System.out.println("查找的数字比第一个数字还小,不存在:");
int[] data5 = { 1, 2, 3, 3, 3, 3, 4, 5 };
System.out.println("1,2,3,3,3,3,4,5中,0出现的次数:" + getNumberOfK(data5, 0));
System.out.println();
System.out.println("查找的数字比最后一个数字还大,不存在:");
int[] data6 = { 1, 2, 3, 3, 3, 3, 4, 5 };
System.out.println("1,2,3,3,3,3,4,5中,6出现的次数:" + getNumberOfK(data6, 6));
System.out.println();
System.out.println("数组中只有查找的数字:");
int[] data7 = { 3, 3, 3, 3 };
System.out.println("3,3,3,3中,3出现的次数:" + getNumberOfK(data7, 3));
System.out.println();
System.out.println("数组中一直重复一个数字,但不是查找的数字:");
int[] data8 = { 3, 3, 3, 3 };
System.out.println("3,3,3,3中,6出现的次数:" + getNumberOfK(data8, 6));
System.out.println();
System.out.println("数组中只有一个数字,且是查找的数字");
int[] data9 = { 3 };
System.out.println("3中,3出现的次数:" + getNumberOfK(data9, 3));
System.out.println();
System.out.println("数组中只有一个数字,且不是查找的数字");
int[] data10 = { 3 };
System.out.println("3中,6出现的次数:" + getNumberOfK(data10, 6));
System.out.println();
}
}
运行结果