当前位置:网站首页>剑指 Offer 53 - I. 在排序数组中查找数字 I
剑指 Offer 53 - I. 在排序数组中查找数字 I
2022-07-05 05:26:00 【ThE wAlkIng D】
题目描述

)
问题解析
两种方法
第一种:暴力解决。
第二种:使用双分查找,由于是排序好的数组,对于找到的那个值,让他加加判断是否相等,如果相等就让count+1;
代码实例
class Solution {
public int search(int[] nums, int target) {
int lefet = 0,right = nums.length()-1,count = 0;
while(left < right){
int mid = (left + right)/2;
if(nums[mid] >= target){
right = middle - 1;
}
if(nums[mid] < target){
left = middle + 1;
}
}
while(left < nums.length&&nums[left++] == target){
count++;
}
return count;
}
}
边栏推荐
- Reflection summary of Haut OJ freshmen on Wednesday
- Talking about JVM (frequent interview)
- Programmers' experience of delivering takeout
- National teacher qualification examination in the first half of 2022
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Software test -- 0 sequence
- [turn to] MySQL operation practice (III): table connection
- Optimization scheme of win10 virtual machine cluster
- 服务熔断 Hystrix
- 小程序直播+电商,想做新零售电商就用它吧!
猜你喜欢

Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade

C语言杂谈1

2022年上半年国家教师资格证考试

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
![[turn to] MySQL operation practice (I): Keywords & functions](/img/b1/8b843014f365b786e310718f669043.png)
[turn to] MySQL operation practice (I): Keywords & functions

浅谈JVM(面试常考)

GBase数据库助力湾区数字金融发展

读者写者模型

YOLOv5-Shufflenetv2
随机推荐
Haut OJ 1245: large factorial of CDs --- high precision factorial
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
Yolov5 ajouter un mécanisme d'attention
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
用STM32点个灯
远程升级怕截胡?详解FOTA安全升级
[allocation problem] 135 Distribute candy
读者写者模型
Haut OJ 1347: addition of choice -- high progress addition
Use the command character to close the keyboard command of the notebook
[speed pointer] 142 circular linked list II
Bubble sort summary
2022年上半年国家教师资格证考试
第六章 数据流建模—课后习题
A preliminary study of sdei - see the essence through transactions
object serialization
YOLOv5-Shufflenetv2
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Haut OJ 1221: a tired day
FVP和Juno平台的Memory Layout介绍