当前位置:网站首页>[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)
边栏推荐
- 职业规划和发展
- UE4 night lighting notes
- Junit5 支持suite的方法
- QT qlabel style settings
- Image recognition - data annotation
- 2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
- Personal experience & blog status
- ue4材质的入门和原理笔记
- Centos7 one click compilation and installation of PHP script
- Record the interesting process of using Xray for the first time
猜你喜欢

Introduction and prevention of penetration test

PI control of grid connected inverter (grid connected mode)

PI control of three-phase grid connected inverter - off grid mode
![[unreal] key to open the door blueprint notes](/img/28/f2b00d84dc05183ce93e2a8ee3555f.png)
[unreal] key to open the door blueprint notes

AutoCAD - layer Linetype

Alibaba cloud SLS log service

2837xd 代码生成——补充(1)

MySQL index

虚幻材质编辑器基础——如何连接一个最基本的材质

Image recognition - data annotation
随机推荐
ESLint 报错
Judging right triangle in C language
Sil/pil test of matlab code generation
UE5——AI追逐(藍圖、行為樹)
UE4夜间打光笔记
[unreal] animation notes of the scene
Project practice, redis cluster technology learning (IX)
Bugkuctf-web21 (detailed problem solving ideas and steps)
阿里云SLS日志服务
2837xd Code Generation - stateflow (4)
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
What wires are suitable for wiring on bread board?
Introduction and prevention of penetration test
渗透测试的介绍和防范
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
PI control of three-phase grid connected inverter - off grid mode
How to achieve the top progress bar effect in background management projects
Database -- acid of transaction -- introduction / explanation
2837xd 代码生成——StateFlow(1)