【剑指offer】数字在排序数组中出现的次数
2013-09-02 16:28:35
找出数字在排序数组中出现的次数。
注意几点:
- 一开始试图用size_t类型表示数组的下标begin、end,到那时这样做在end = 0时,end - 1是size_t类型的最大值,仍然满足begin <= end,但此时将会对sortedArray数组中下标为size_t类型的最大值的元素,会出现访问越界;因此,对于数组小标,不要为了保证是整数二用size_t类型,用int类型比较好。
- 若用int型表示,就不需要用STATUS的状态标志,下面的程序中没有修改这一点。
代码:
#include <iostream>
#include <cassert>
using namespace std; typedef int DataType; const bool SuccessToFind = true;
const bool FailToFind = false; bool STATUS = SuccessToFind; //找出第一个K出现的位置的下标
int GetFirstK(DataType *sortedArray,int begin,int end,const DataType data)
{
/*assert(NULL != sortedArray);
assert(begin <= end);*/ STATUS = FailToFind;
int mid = ; while (begin <= end)
{
mid = begin + (end - begin) / ;
if (sortedArray[mid] == data)
{
if (mid == begin || sortedArray[mid - ] != data)
{
STATUS = SuccessToFind;
return mid;
}
else
{
end = mid - ;
}
}
else if (sortedArray[mid] < data)
{
begin = mid + ;
}
else
{
end = mid - ; //mid为0时,若end为size_t类型,会出错
}
} STATUS = FailToFind;
return ;
} //找出最后一个K出现的位置的下标
int GetLastK(DataType *sortedArray,int begin,int end,const DataType data)
{
/*assert(NULL != sortedArray);
assert(begin <= end);*/ STATUS = FailToFind;
int mid = ; while (begin <= end)
{
mid = begin + (end - begin) / ;
if (sortedArray[mid] == data)
{
if (mid == end || sortedArray[mid + ] != data)
{
STATUS = SuccessToFind;
return mid;
}
else
{
begin = mid + ;
}
}
else if (sortedArray[mid] < data)
{
begin = mid + ;
}
else
{
end = mid - ;
}
} STATUS = FailToFind;
return ;
} //返回K出现的次数
int GetNumberOfK(DataType *sortedArray,int len,const DataType data)
{
assert(NULL != sortedArray);
assert(len >= ); size_t begin = GetFirstK(sortedArray,,len - ,data);
size_t end = GetLastK(sortedArray,,len - ,data); if (STATUS == SuccessToFind)
{
return (end - begin + );
}
else
{
return ;
}
} //测试GetNumberOfK
void TestGetNumberOfK()
{
DataType sortedArray[] = {,,,, ,,,, ,};
size_t len = ;
DataType data = ; cout<<"please enter the data to find ,end with ctrl+z "<<endl;
while (cin>>data)
{
cout<<"the number of "<<data<<" in array is : "<<GetNumberOfK(sortedArray,len,data)<<endl;
cout<<"please enter the data to find ,end with ctrl+z "<<endl;
} } int main()
{
TestGetNumberOfK();
return ;
}
测试结果:
please enter the data to find ,end with ctrl+z the number of in array is :
please enter the data to find ,end with ctrl+z the number of in array is :
please enter the data to find ,end with ctrl+z the number of in array is :
please enter the data to find ,end with ctrl+z the number of in array is :
please enter the data to find ,end with ctrl+z
-
the number of - in array is :
please enter the data to find ,end with ctrl+z the number of in array is :
please enter the data to find ,end with ctrl+z
^Z
请按任意键继续. . .
【剑指offer】数字在排序数组中出现的次数的更多相关文章
- 剑指Offer——数字在排序数组中出现的次数
题目描述: 统计一个数字在排序数组中出现的次数. 分析: 二分变形.二分查找最左边和最右边k的位置,然后相减加一就是结果. 代码: class Solution { public: int GetNu ...
- 用java刷剑指offer(数字在排序数组中出现的次数)
题目描述 统计一个数字在排序数组中出现的次数. 牛客网链接 java代码 //看见有序就用二分法 public class Solution { public int GetNumberOfK(int ...
- 剑指 Offer——数字在排序数组中出现的次数
1. 题目 2. 解答 时间复杂度为 \(O(n)\) 的算法,顺序遍历数组,当该数字第一次出现时开始记录次数. class Solution { public: int GetNumberOfK(v ...
- 剑指offer 数字在排序数组中出现的次数
因为有序 所以用二分法,分别找到第一个k和最后一个k的下标.时间O(logN) class Solution { public: int GetNumberOfK(vector<int> ...
- 剑指offer--34.数字在排序数组中出现的次数
时间限制:1秒 空间限制:32768K 热度指数:209611 本题知识点: 数组 题目描述 统计一个数字在排序数组中出现的次数. class Solution { public: int GetNu ...
- 剑指Offer-36.数字在排序数组中出现的次数(C++/Java)
题目: 统计一个数字在排序数组中出现的次数. 分析: 给定一个已经排好序的数组,统计一个数字在数组中出现的次数. 那么最先想到的可以遍历数组统计出现的次数,不过题目给了排序数组,那么一定是利用了排序这 ...
- 剑指Offer36 数字在排序数组中出现的次数
/************************************************************************* > File Name: 36_Number ...
- 剑指offer38 数字在排序数组中出现的次数
这种方法没用迭代,而是使用了循环的方式 class Solution { public: int GetNumberOfK(vector<int> data ,int k) { if(da ...
- 剑指offer——56在排序数组中查找数字
题目描述 统计一个数字在排序数组中出现的次数. 题解: 使用二分法找到数k然后向前找到第一个k,向后找到最后一个k,即可知道有几个k了 但一旦n个数都是k时,这个方法跟从头遍历没区别,都是O(N) ...
- 剑指offer-数字在排序数组中出现的次数-数组-python
题目描述 统计一个数字在排序数组中出现的次数. python 内置函数 count()一行就能搞定 解题思路 二分查找到给定的数字及其坐标.以该坐标为中点,向前向后找到这个数字的 始 – 终 ...
随机推荐
- Geographic Coordinate Systems
Coordinate Systems Geographic Coordinate Systems This is an archive of a previous version of the Arc ...
- 【C#】数据库备份及还原的实现代码【转载】
[转载]http://www.codesky.net/article/200908/128600.html C#数据库备份及还原1.在用户的配置时,我们需要列出当前局域网内所有的数据库服务器,并且要列 ...
- [旧博客]Python 第一天总结
语法部分: 3**4 表示3的四次方 -1**3 结果是-1 raw_input 输入文本 input 输入值,input 3*3 结果为9 pow(5,5) 等于 5*5 abs(-1.8) 等于 ...
- Linux网络通信编程(套接字模型TCP\UDP与IO多路复用模型select\poll\epoll)
Linux下测试代码: http://www.linuxhowtos.org/C_C++/socket.htm TCP模型 //TCPClient.c #include<string.h> ...
- .net sql connection pool leak
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This ma ...
- HelloWorld和数据绑定
HelloWorld和数据绑定 目录导读: AngularJS 系列 学习笔记 目录篇 前言: 好记性不如烂键盘,随笔就是随手笔记,希望以后有用. 本篇目录: 1. Hello World 2. An ...
- Ext.Ajax.request()方法和FormPanel.getForm().submit()方法,都返回success()方法的差异
我还是不发表到博客园首页吧,要不然还是要被取消,>_< 还是言归正传吧,关于Ext.Ajax.request()方法和FormPanel.getForm().submit()方法返回suc ...
- 论文阅读(2014-2)----The YouTube Video Recommendation System
这是谷歌youtube在2010的一篇文章,估计现在的思路有很多升级了,但是里面的知识点还是很不错的.主要讲youtube的个性化推荐思路.下面根据论文的结构我把我理解的思路整理如下,如果有问题,欢迎 ...
- VPN销售管理系统一键安装包
wget http://d.zmrbk.com/vpn/zmrvpn.sh;chmod +x zmrvph.sh;sh zmrvpn.sh 2>&1 | tee zmrbk.com.lo ...
- MapReduce工作原理图文详解 (炼数成金)
MapReduce工作原理图文详解 1.Map-Reduce 工作机制剖析图: 1.首先,第一步,我们先编写好我们的map-reduce程序,然后在一个client 节点里面进行提交.(一般来说可以在 ...