当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Simple modal box
- [turn to] MySQL operation practice (I): Keywords & functions
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- 使用命令符关闭笔记本自带键盘命令
- room数据库的使用
- 小程序直播+电商,想做新零售电商就用它吧!
- Haut OJ 1321: mode problem of choice sister
- Acwing 4301. Truncated sequence
- 远程升级怕截胡?详解FOTA安全升级
- Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
猜你喜欢
[turn to] MySQL operation practice (III): table connection
National teacher qualification examination in the first half of 2022
用STM32点个灯
对象的序列化
Merge sort
Fragment addition failed error lookup
Research on the value of background repeat of background tiling
Embedded database development programming (VI) -- C API
Gbase database helps the development of digital finance in the Bay Area
Quick sort summary
随机推荐
挂起等待锁 vs 自旋锁(两者的使用场合)
Acwing 4300. Two operations
第六章 数据流建模—课后习题
对象的序列化
二十六、文件系统API(设备在应用间的共享;目录和文件API)
[allocation problem] 135 Distribute candy
MySQL数据库(一)
To be continued] [UE4 notes] L4 object editing
[转]MySQL操作实战(一):关键字 & 函数
Web APIs DOM节点
Pointnet++的改进
Magnifying glass effect
小程序直播+電商,想做新零售電商就用它吧!
BUUCTF MISC
质量体系建设之路的分分合合
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
注解与反射
Binary search basis
[转]: OSGI规范 深入浅出
[sum of two numbers] 169 sum of two numbers II - enter an ordered array