博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oral_quiz->#排序数组中数字出现的次数#
阅读量:6715 次
发布时间:2019-06-25

本文共 4315 字,大约阅读时间需要 14 分钟。

hot3.png

#include 
bool g_bRightNumber = false;void MyGetNumberHelp(int* data, int start, int end, int** first, int** last, int k);int MyGetNumber(int* data, int length, int k) { g_bRightNumber = false; if(data == NULL || length <=0) return 0; int** first = &data; int* lastn = data+length-1; int** last = &lastn; MyGetNumberHelp(data, 0, length-1, first, last, k); int number = *last-*first+1; delete first; delete last; return g_bRightNumber ? number : 0;}void MyGetNumberHelp(int* data, int start, int end, int** first, int** last, int k) { if(start > end) return; int mid = (start+end) >> 1; if(data[mid] > k) MyGetNumberHelp(data, start, mid-1, first, last, k); else if(data[mid] < k) MyGetNumberHelp(data, mid+1, end, first, last, k); else { if(!g_bRightNumber) g_bRightNumber = true; if(mid>0 && mid<*last-data) { if(data[mid-1] < k) { *first = &data[mid]; MyGetNumberHelp(data, start, mid-1, first, last, k); } else if(data[mid+1] > k) { *last = &data[mid]; MyGetNumberHelp(data, mid+1, end, first, last, k); } else { MyGetNumberHelp(data, mid+1, end, first, last, k); MyGetNumberHelp(data, start, mid-1, first, last, k); } } }}//=========================hht's=============================int GetFirstK(int* data, int length, int k, int start, int end) { if(start > end) return -1; int middleIndex = (start+end) >> 1; int middleData = data[middleIndex]; if(middleData == k) { if((middleIndex > 0 && data[middleIndex-1] != k) || middleIndex == 0) return middleIndex; else end = middleIndex-1; } else if(middleData > k) end = middleIndex - 1; else start = middleIndex + 1; return GetFirstK(data, length, k, start, end);}int GetLastK(int* data, int length, int k, int start, int end) { if(start > end) return -1; int middleIndex = (start+end) >> 1; int middleData = data[middleIndex]; if(middleData == k) { if((middleIndex < length-1 && data[middleIndex+1] != k) || middleIndex == length-1) return middleIndex; else start = middleIndex + 1; } else if(middleData < k) start = middleIndex + 1; else end = middleIndex - 1; return GetLastK(data, length, k, start, end);}int GetNumberOfK(int* data, int length, int k) { int number = 0; if(data != NULL && length >= 0) { int first = GetFirstK(data, length, k, 0, length-1); int last = GetLastK(data, length, k, 0, length-1); if(first > -1 && last > -1) number = last-first+1; } return number;}// ====================测试代码====================void Test(const char* testName, int data[], int length, int k, int expected){ if(testName != NULL) printf("%s begins: ", testName);// int result = MyGetNumber(data, length, k); int result = GetNumberOfK(data, length, k); if(result == expected) printf("Passed.\n"); else printf("Failed.\n");}// 查找的数字出现在数组的中间void Test1(){ int data[] = {1, 2, 3, 3, 3, 3, 4, 5}; Test("Test1", data, sizeof(data) / sizeof(int), 3, 4);}// 查找的数组出现在数组的开头void Test2(){ int data[] = {3, 3, 3, 3, 4, 5}; Test("Test2", data, sizeof(data) / sizeof(int), 3, 4);}// 查找的数组出现在数组的结尾void Test3(){ int data[] = {1, 2, 3, 3, 3, 3}; Test("Test3", data, sizeof(data) / sizeof(int), 3, 4);}// 查找的数字不存在void Test4(){ int data[] = {1, 3, 3, 3, 3, 4, 5}; Test("Test4", data, sizeof(data) / sizeof(int), 2, 0);}// 查找的数字比第一个数字还小,不存在void Test5(){ int data[] = {1, 3, 3, 3, 3, 4, 5}; Test("Test5", data, sizeof(data) / sizeof(int), 0, 0);}// 查找的数字比最后一个数字还大,不存在void Test6(){ int data[] = {1, 3, 3, 3, 3, 4, 5}; Test("Test6", data, sizeof(data) / sizeof(int), 6, 0);}// 数组中的数字从头到尾都是查找的数字void Test7(){ int data[] = {3, 3, 3, 3}; Test("Test7", data, sizeof(data) / sizeof(int), 3, 4);}// 数组中的数字从头到尾只有一个重复的数字,不是查找的数字void Test8(){ int data[] = {3, 3, 3, 3}; Test("Test8", data, sizeof(data) / sizeof(int), 4, 0);}// 数组中只有一个数字,是查找的数字void Test9(){ int data[] = {3}; Test("Test9", data, sizeof(data) / sizeof(int), 3, 1);}// 数组中只有一个数字,不是查找的数字void Test10(){ int data[] = {3}; Test("Test10", data, sizeof(data) / sizeof(int), 4, 0);}// 鲁棒性测试,数组空指针void Test11(){ Test("Test11", NULL, 0, 0, 0);}int main(int argc, char* argv[]){ Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); Test8(); Test9(); Test10(); Test11(); return 0;}

转载于:https://my.oschina.net/ITHaozi/blog/282312

你可能感兴趣的文章
条件+努力=?
查看>>
HBase分布式安装
查看>>
随笔-文件的读写
查看>>
tcp 状态以及三次握手
查看>>
我的友情链接
查看>>
WAITED TOO LONG FOR A ROW CACHE ENQUEUE LOCK!的分析
查看>>
nginx禁止ip直接访问
查看>>
hadoop常用服务管理命令
查看>>
10.28 rsync工具10.29-10.30 rsync选项10.31 rsync通过ssh同步
查看>>
Fault,Error and Failure
查看>>
Go语言的通道(1)-无缓冲通道
查看>>
spring oauth从请求中获取token
查看>>
6.18docker(一)Compose 模板文件
查看>>
每天学点GDB 9
查看>>
为什么要用 /dev/null 2>&1 这样的写法
查看>>
AngularJs创建省,市,区的3级列表
查看>>
wp7 独立存储
查看>>
项目UML设计(团队)
查看>>
Divideing Jewels
查看>>
洛谷P4169 天使玩偶 (算竞进阶习题)
查看>>