当前位置:网站首页>[sword finger offer] 53 - I. find the number I in the sorted array
[sword finger offer] 53 - I. find the number I in the sorted array
2022-06-30 17:40:00 【LuZhouShiLi】
The finger of the sword Offer 53 - I. Look up numbers in the sort array I
subject
Count the number of times a number appears in the sort array .
Ideas
Use binary search to find the left boundary respectively left And the right border right, So the numbers target The number of right - left - 1
Code
class Solution {
public int search(int[] nums, int target) {
// Search the right border right
int i = 0,j = nums.length - 1;
while(i <= j)
{
int m = (i + j) / 2;
if(nums[m] < target)
{
i = m + 1;// explain target In the closed zone [m + 1,j] On
}
else if(nums[m] == target)
{
i = m + 1; // Because I want to find the right boundary therefore The right boundary must be in a closed interval [m + 1, j] in
}
else
{
j = m -1;// target In the closed zone [i,m - 1] in
}
}
int right = i; // Locate right boundary here right The number pointing to No target
if(j >= 0 && nums[j] != target) return 0;
// Search left border right
i = 0;
j = nums.length - 1;
while(i <= j)
{
int m = (i + j) / 2;
if(nums[m] < target)
{
i = m + 1;
}
else{
j = m - 1;
}
}
int left = j;// The number that the left boundary points to is not target
return right - left - 1;
}
}
边栏推荐
- 【剑指Offer】52. 两个链表的第一个公共节点
- Daily question brushing record (IX)
- Solution: STM32 failed to parse data using cjson
- NielsenIQ迎来零售实验室负责人Dawn E. Norvell,将加速扩张全球零售战略
- 数据中心的能耗焦虑, 到底有没有最优解?
- 平面相交与平面方程
- 6 张图带你搞懂 TCP 为什么是三次握手?
- In the past, the industrial Internet we knew only appeared as a substitute for the consumer Internet
- 【Proteus仿真】Arduino UNO利用74LS148扩展中断
- [proteus simulation] Arduino uno uses 74ls148 to extend interrupt
猜你喜欢

Cesium-1.72 learning (camera tracking)

Analysis on the construction scheme and necessity of constructing expressway video monitoring platform

Canvas cloud shape animation

广电5G正式启航,黄金频段将如何应用引关注

. Net ORM framework hisql practice - Chapter 1 - integrating hisql

浅析搭建高速公路视频监控平台的建设方案及必要性

Fragmentary knowledge points of MySQL

【架构】1366- 如何画出一张优秀的架构图

splitting. JS password display hidden JS effect

flutter自定义组件
随机推荐
Development: how to install offline MySQL in Linux system?
新技能:通过代码缓存加速 Node.js 的启动
Ten thousand volumes - list sorting [01]
leetcode:1042. 不邻接植花【随机填入符合要求的 + 后面不会形成矛盾 + set.pop】
3D图表有效提升数据大屏档次
5G商用三年,未来创新何去何从?
美国穆格moog伺服阀D661-4577C
k线图精解与实战应用技巧(见位进场)
Interview shock 60: what will cause MySQL index invalidation?
TFTP下载kernel,nfs挂载文件系统
生成对抗网络,从DCGAN到StyleGAN、pixel2pixel,人脸生成和图像翻译。
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
SSH tool pyqt
5g business is officially commercial. What are the opportunities for radio and television?
Nouvelle version de shangdingyun | la fonction favorite est en ligne pour répondre aux besoins d'utilisation personnelle
China Infrastructure Development Association: electronic contract is recommended
Spin lock exploration
ROC-RK3566-PC使用10.1寸IPS触摸屏显示
Bridge emqx cloud data to AWS IOT through the public network
高等数学(第七版)同济大学 总习题一 个人解答