当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Haut OJ 1321: mode problem of choice sister
- object serialization
- Add level control and logger level control of Solon logging plug-in
- 注解与反射
- BUUCTF MISC
- [speed pointer] 142 circular linked list II
- 64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
- TF-A中的工具介绍
- Haut OJ 1316: sister choice buys candy III
- When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
猜你喜欢

小程序直播+電商,想做新零售電商就用它吧!

sync.Mutex源码解读
![[to be continued] [depth first search] 547 Number of provinces](/img/c4/b4ee3d936776dafc15ac275d2059cd.jpg)
[to be continued] [depth first search] 547 Number of provinces

Introduction to tools in TF-A
![[merge array] 88 merge two ordered arrays](/img/e9/a73d9f22eead8e68c1e45c27ff6e6c.jpg)
[merge array] 88 merge two ordered arrays

Embedded database development programming (V) -- DQL

Binary search basis

Quick sort summary

TF-A中的工具介绍
![[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research](/img/17/db8614b177f33ee4f67b7d65a8430f.png)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
随机推荐
小程序直播+电商,想做新零售电商就用它吧!
ssh免密登录设置及使用脚本进行ssh登录并执行指令
[turn]: OSGi specification in simple terms
读者写者模型
Csp-j-2020-excellent split multiple solutions
Research on the value of background repeat of background tiling
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
Optimization scheme of win10 virtual machine cluster
[interval problem] 435 Non overlapping interval
SAP method of modifying system table data
[turn to] MySQL operation practice (I): Keywords & functions
Page countdown
Haut OJ 1241: League activities of class XXX
[allocation problem] 135 Distribute candy
Service fusing hystrix
A three-dimensional button
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
PMP考生,请查收7月PMP考试注意事项
质量体系建设之路的分分合合
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8