当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 记录QT内存泄漏的一种问题和解决方案
- Web APIs DOM节点
- FVP和Juno平台的Memory Layout介绍
- [sum of two numbers] 169 sum of two numbers II - enter an ordered array
- 使用命令符关闭笔记本自带键盘命令
- [paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
- Double pointer Foundation
- Zzulioj 1673: b: clever characters???
- object serialization
- Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
猜你喜欢
随机推荐
Haut OJ 1241: League activities of class XXX
Haut OJ 1245: large factorial of CDs --- high precision factorial
TF-A中的工具介绍
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Generate filled text and pictures
Reader writer model
Gbase database helps the development of digital finance in the Bay Area
Haut OJ 1352: string of choice
Drawing dynamic 3D circle with pure C language
Acwing 4300. Two operations
[turn]: Apache Felix framework configuration properties
Development error notes
YOLOv5添加注意力机制
Yolov5 ajouter un mécanisme d'attention
Zzulioj 1673: b: clever characters???
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Embedded database development programming (V) -- DQL
用STM32点个灯
Reverse one-way linked list of interview questions