当前位置:网站首页>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;
}
}
边栏推荐
- Haut OJ 2021 freshmen week II reflection summary
- Haut OJ 1350: choice sends candy
- 【ES实战】ES上的native realm安全方式使用
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- [to be continued] [UE4 notes] L1 create and configure items
- 剑指 Offer 06.从头到尾打印链表
- To be continued] [UE4 notes] L4 object editing
- 剑指 Offer 04. 二维数组中的查找
- Binary search basis
- [allocation problem] 455 Distribute cookies
猜你喜欢
On-off and on-off of quality system construction
用STM32点个灯
[to be continued] [UE4 notes] L2 interface introduction
Palindrome (csp-s-2021-palin) solution
Service fusing hystrix
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
The present is a gift from heaven -- a film review of the journey of the soul
游戏商城毕业设计
[turn to] MySQL operation practice (III): table connection
随机推荐
object serialization
SAP method of modifying system table data
Haut OJ 1357: lunch question (I) -- high precision multiplication
Solon Auth 认证框架使用演示(更简单的认证框架)
质量体系建设之路的分分合合
剑指 Offer 04. 二维数组中的查找
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
PMP考试敏捷占比有多少?解疑
Haut OJ 1347: addition of choice -- high progress addition
[to be continued] [UE4 notes] L2 interface introduction
[to be continued] [UE4 notes] L1 create and configure items
[interval problem] 435 Non overlapping interval
剑指 Offer 53 - II. 0~n-1中缺失的数字
Service fusing hystrix
C language Essay 1
Software test -- 0 sequence
软件测试 -- 0 序
[depth first search] 695 Maximum area of the island
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Maximum number of "balloons"