2013-09-02 16:28:35

找出数字在排序数组中出现的次数。

注意几点:

  1. 一开始试图用size_t类型表示数组的下标begin、end,到那时这样做在end = 0时,end - 1是size_t类型的最大值,仍然满足begin <= end,但此时将会对sortedArray数组中下标为size_t类型的最大值的元素,会出现访问越界;因此,对于数组小标,不要为了保证是整数二用size_t类型,用int类型比较好。
  2. 若用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】数字在排序数组中出现的次数的更多相关文章

  1. 剑指Offer——数字在排序数组中出现的次数

    题目描述: 统计一个数字在排序数组中出现的次数. 分析: 二分变形.二分查找最左边和最右边k的位置,然后相减加一就是结果. 代码: class Solution { public: int GetNu ...

  2. 用java刷剑指offer(数字在排序数组中出现的次数)

    题目描述 统计一个数字在排序数组中出现的次数. 牛客网链接 java代码 //看见有序就用二分法 public class Solution { public int GetNumberOfK(int ...

  3. 剑指 Offer——数字在排序数组中出现的次数

    1. 题目 2. 解答 时间复杂度为 \(O(n)\) 的算法,顺序遍历数组,当该数字第一次出现时开始记录次数. class Solution { public: int GetNumberOfK(v ...

  4. 剑指offer 数字在排序数组中出现的次数

    因为有序 所以用二分法,分别找到第一个k和最后一个k的下标.时间O(logN) class Solution { public: int GetNumberOfK(vector<int> ...

  5. 剑指offer--34.数字在排序数组中出现的次数

    时间限制:1秒 空间限制:32768K 热度指数:209611 本题知识点: 数组 题目描述 统计一个数字在排序数组中出现的次数. class Solution { public: int GetNu ...

  6. 剑指Offer-36.数字在排序数组中出现的次数(C++/Java)

    题目: 统计一个数字在排序数组中出现的次数. 分析: 给定一个已经排好序的数组,统计一个数字在数组中出现的次数. 那么最先想到的可以遍历数组统计出现的次数,不过题目给了排序数组,那么一定是利用了排序这 ...

  7. 剑指Offer36 数字在排序数组中出现的次数

    /************************************************************************* > File Name: 36_Number ...

  8. 剑指offer38 数字在排序数组中出现的次数

    这种方法没用迭代,而是使用了循环的方式 class Solution { public: int GetNumberOfK(vector<int> data ,int k) { if(da ...

  9. 剑指offer——56在排序数组中查找数字

    题目描述 统计一个数字在排序数组中出现的次数.   题解: 使用二分法找到数k然后向前找到第一个k,向后找到最后一个k,即可知道有几个k了 但一旦n个数都是k时,这个方法跟从头遍历没区别,都是O(N) ...

  10. 剑指offer-数字在排序数组中出现的次数-数组-python

    题目描述 统计一个数字在排序数组中出现的次数.   python 内置函数 count()一行就能搞定   解题思路 二分查找到给定的数字及其坐标.以该坐标为中点,向前向后找到这个数字的 始 – 终 ...

随机推荐

  1. Windows Phone使用sliverlight toolkit实现页面切换动画效果

    使用应用时,好多app在页面切换的时候都有一个动画效果,感觉很炫,也大大增加了用户体验,怎么实现呢? 界面的切换,可以用Windows Phone Toolkit中的TransitionService ...

  2. Mysql创建函数时报错

    先去查询  show variables like '%func%' ; 这个语句,如果该语句最后输出的值是OFF 那么就用下面的语句去修改就可以:set global log_bin_trust_f ...

  3. 关于fputs和fgets的几个细节

    C语言中两个标准IO fputs和fgets都是针对行来进行数据的读取的!这里关于这两个IO函数我有几个小细节想在这里和大家分享一下,希望能够对大家产生帮助! 首先贴上这两个函数的函数声明,下面以这两 ...

  4. 关于Socket编写简单聊天工具的总结(原创)

    这段时间再看socket编程,虽然现在是刚刚接触,但是还是忍不住想写一篇总结,来激励自己努力学习,写的不好的地方,还请大家指教啊! 下面针对一个简单的发送消息和文件的程序说说吧.   首先是服务器需要 ...

  5. php入门之表单创建和基本处理

    为了方便后面学习数组,这里引入了过渡章节就是表单,至于为什么,等真的学习到数组的时候你就会发现它的妙处拉. ============================================== ...

  6. 《CSS3使用指南》读书笔记

    一.CSS3的来龙去脉 1.CSS3的新特性: 1)不依赖图片的视觉效果 2)盒容器变形 3)独一无二的字体 4)强大的选择器 5)过渡与动画 6)媒体信息查询 7)多列布局 2.CSS标准发布分5个 ...

  7. String Subtraction

    Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the ...

  8. [转]java gridbag 说明

    gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 gridheight = 1; // 列占一个单元格 weightx = 0.0 ...

  9. Xen学习——原理要点归纳总结

    Xen是半虚拟化,需要修改操作系统内核.Vmware是完全虚拟化. XEN的系统架构: Xen Hypervisor: 直接运行在硬件上,介于操作系统和硬件之间的一层软件,负责管理CPU.内存.中断. ...

  10. XML文件序列化和反序列化的相关内容

    问题缘由: XML反序列化出错,XML 文档(2, 2)中有错误,不应有 <configuration xmlns=''> 解决方法: 其实这个是很简单的,因为一般来说都是XML文档书写错 ...