当前位置:网站首页>[leetcode] sword finger offer 53 - I. find the number I in the sorted array
[leetcode] sword finger offer 53 - I. find the number I in the sorted array
2022-07-02 10:14:00 【Chinese fir sauce_】
subject :
The finger of the sword Offer 53 - I. Look up numbers in the sort array I
Count the number of times a number appears in the sort array .
Example 1:
Input : nums = [5,7,7,8,8,10], target = 8
Output : 2
Example 2:
Input : nums = [5,7,7,8,8,10], target = 6
Output : 0
Tips :
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums It is a group of non decreasing numbers
-109 <= target <= 109
Ideas : First, determine the left and right boundaries by dichotomy , Then look from left to right .
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int l = 0,r = n - 1;
int count = 0;
while (l <= r) {
int mid = (l + r) / 2;
// Determine the right boundary
if(nums[mid] >= target) {
r = mid - 1;
}
// Determine left boundary
else {
l = mid + 1;
}
}
for( ;l < n && target == nums[l++];){
count++;
}
return count;
}
}
- Time complexity :O(n)
- Spatial complexity :O(1)
边栏推荐
- 2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
- Remember the use of add method once
- 2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
- 2837xd code generation module learning (1) -- GPIO module
- Record the interesting process of using Xray for the first time
- ue虚幻引擎程序化植物生成器设置——如何快速生成大片森林
- Translation d30 (with AC code POJ 28:sum number)
- Data insertion in C language
- 虚幻AI蓝图基础笔记(万字整理)
- Project practice, redis cluster technology learning (13)
猜你喜欢

Vs+qt set application icon
![[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology](/img/a9/ffd5d8000fc811f958622901bf408d.png)
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology

Mixed development of uni app -- Taking wechat applet as an example

2837xd code generation module learning (2) -- ADC, epwm module, timer0

MySQL transaction
What is the relationship between realizing page watermarking and mutationobserver?

ue4材质的入门和原理笔记

阿里云短信服务

2837xd代码生成模块学习(1)——GPIO模块

Image recognition - Data Cleaning
随机推荐
C language: making barrels
[unreal] key to open the door blueprint notes
Remember the use of add method once
Image recognition - data augmentation
Alibaba cloud SLS log service
Project practice, redis cluster technology learning (13)
2837xd 代碼生成——StateFlow(4)
Summary of demand R & D process nodes and key outputs
ZK configuration center -- configuration and use of config Toolkit
Project practice, redis cluster technology learning (IX)
高考那些事
PI control of grid connected inverter (grid connected mode)
【虚幻】自动门蓝图笔记
阿里云Prometheus监控服务
2837xd code generation - stateflow (1)
职业规划和发展
[200 Shengxin literatures] 95 multiomics exploration TNBC
Project practice, redis cluster technology learning (11)
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
Introduction and prevention of penetration test