【剑指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()一行就能搞定 解题思路 二分查找到给定的数字及其坐标.以该坐标为中点,向前向后找到这个数字的 始 – 终 ...
随机推荐
- 串操作,C++实现
对串的基本操作都全已经实现 对kmp,kf字符串替换等功能全都已经实现 由于时间原因.没来得及注释,希望大家参考见谅. 串操作hstring.h头文件实现 //kallen 1 #ifndef _HS ...
- 获取当前<script>节点
/* get current JavaScript dom object. */ var all_js = document.getElementsByTagName("script&quo ...
- Winform 异步更新listbox
public partial class Form1 : Form { private BackgroundWorker bw; public Form1() { InitializeComponen ...
- Android UI学习前言:Android UI系统的知识结构
Android UI系统的知识结构如下图所示: 对于 一个GUI系统地使用,首先是由应用程序来控制屏幕上元素的外观和行为,这在各个GUI系统中是不相同的,但是也具有相通性.Android系统在这方面, ...
- Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)
Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)
- 【socket】TCP 和 UDP 在socket编程中的区别
一.TCP与UDP的区别 基于连接与无连接 对系统资源的要求(TCP较多,UDP少) UDP程序结构较简单 流模式与数据报模式 TCP保证数据正确性,UDP可能丢包 TCP保证数据顺序,UD ...
- XML文件序列化和反序列化的相关内容
问题缘由: XML反序列化出错,XML 文档(2, 2)中有错误,不应有 <configuration xmlns=''> 解决方法: 其实这个是很简单的,因为一般来说都是XML文档书写错 ...
- 你所不知道的ref
在c#中有个关键字叫ref,它的作用是使参数按引用传递,基本用法如下: class RefExample { static void Method(ref int i) { i = ; } stati ...
- PD name 和 comment 互换
1 PowerDesigner中批量根据对象的name生成comment的脚本 执行方法:Open PDM -- Tools -- Execute Commands -- Run Script --- ...
- 2817 Tangent的愤怒 - Wikioi
题目描述 Description 如果机房马上要关门了,或者你急着要和MM约会,请直接跳到第六个自然段. 第二段:本题改编自Usaco Training 4.4.2... 第三段:本题加大了数据强度. ...