当前位置:网站首页>剑指 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;
}
}
边栏推荐
猜你喜欢
随机推荐
PMP candidates, please check the precautions for PMP examination in July
TF-A中的工具介绍
room数据库的使用
Haut OJ 1321: mode problem of choice sister
Developing desktop applications with electron
How can the Solon framework easily obtain the response time of each request?
[转]:Apache Felix Framework配置属性
[allocation problem] 135 Distribute candy
[trans]: spécification osgi
C language Essay 1
[speed pointer] 142 circular linked list II
Introduction to memory layout of FVP and Juno platforms
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
Solon 框架如何方便获取每个请求的响应时间?
Pointnet++的改进
JVM call not used once in ten years
Bucket sort
Use the command character to close the keyboard command of the notebook
Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
Use of snippets in vscode (code template)



![[trans]: spécification osgi](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)





