当前位置:网站首页>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-07-05 05:28:00 【ThE wAlkIng D】
Title Description
)
Problem analysis
The two methods
The first one is : Violent settlement .
The second kind : Use bisection search , Because it is a sorted array , For the value found , Let him judge whether it is equal , If equal, let count+1;
Code instance
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;
}
}
边栏推荐
- Reader writer model
- Haut OJ 1347: addition of choice -- high progress addition
- Control Unit 控制部件
- Haut OJ 1221: a tired day
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- Reflection summary of Haut OJ freshmen on Wednesday
- 挂起等待锁 vs 自旋锁(两者的使用场合)
- Haut OJ 1316: sister choice buys candy III
- Romance of programmers on Valentine's Day
- 用STM32点个灯
猜你喜欢
[to be continued] [UE4 notes] L2 interface introduction
[turn to] MySQL operation practice (III): table connection
对象的序列化
剑指 Offer 05. 替换空格
[turn]: OSGi specification in simple terms
[to be continued] [UE4 notes] L3 import resources and project migration
C language Essay 1
剑指 Offer 05. 替换空格
浅谈JVM(面试常考)
Solution to the palindrome string (Luogu p5041 haoi2009)
随机推荐
[to be continued] [UE4 notes] L3 import resources and project migration
Haut OJ 1241: League activities of class XXX
Yolov5 adds attention mechanism
ssh免密登录设置及使用脚本进行ssh登录并执行指令
[转]:Apache Felix Framework配置属性
Time complexity and space complexity
xftp7与xshell7下载(官网)
读者写者模型
Solon 框架如何方便获取每个请求的响应时间?
YOLOv5-Shufflenetv2
远程升级怕截胡?详解FOTA安全升级
Drawing dynamic 3D circle with pure C language
[binary search] 69 Square root of X
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
Palindrome (csp-s-2021-palin) solution
GBase数据库助力湾区数字金融发展
剑指 Offer 04. 二维数组中的查找
[to be continued] [depth first search] 547 Number of provinces
Heap sort summary
Solon Auth 认证框架使用演示(更简单的认证框架)