【剑指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()一行就能搞定 解题思路 二分查找到给定的数字及其坐标.以该坐标为中点,向前向后找到这个数字的 始 – 终 ...
随机推荐
- 《PHP扩展开发及内核应用》
https://github.com/walu/phpbook/blob/master/preface.md
- Qt移植 Window --Linux
1.把源代码复制到Linux目录,使用qmake命令,注意在shell中直接使用qmake命令注意设置PATH环境变量 2. 在目录中会生成Makeflie文件 3. make即可 /usr/bin/ ...
- DataGridView显示时间格式
默认显示时间不显示秒yyyy-MM-dd HH:mm dataGridView.Columns["日期时间字段"].DefaultCellStyle.Format = " ...
- jQuery(function($){...})与(function($){...})(jQuery)知识点分享
写jQuery插件时一些经验分享一下. 一.推荐写法 jQuery(function($){ //coding }); 全写为 jQuery(document).ready(function($){ ...
- 百度地图api的实现
获取客户端IP地址经纬度所在城市 <?php $getIp=$_SERVER["REMOTE_ADDR"]; echo 'IP:',$getIp; echo ''; $con ...
- SSH-KEY服务及批量分发与管理实战
SSH服务 一.SSH服务介绍 SSH是Secure Shell Protocol的简写,由IETF网络工作小组制定:在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后再进行数据 ...
- 开始学习python
刚刚离开学校,到公司实习,发现所有的技术都是崭新的,所有的工具都是熟悉中带着陌生. 就像是孤身一人到了一个曾经只闻其名的偌大城市,看什么都觉得新鲜,做什么都心有畏惧.幸好 搞软件并没有那么多人情世故, ...
- Android的ProgressBar以及自定义进度条
1.xml文件 activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an ...
- Nginx upstream的5种权重分配方式
.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,后端服务器down掉,能自动剔除 .weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况. upstre ...
- ios App 打包
ios 版本的 App 打包两种方式: 1. 命令行 xcodebuild exportArchive -exportFormat ipa 2. 通过 xcode Product -> Arch ...