当前位置:网站首页>[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 code generation - stateflow (3)
- High level application of SQL statements in MySQL database (II)
- [Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
- Pycaret | a few lines of code to solve machine learning modeling
- [unreal] key to open the door blueprint notes
- [200 Shengxin literatures] 96 joint biomarkers of immune checkpoint inhibitor response in advanced solid tumors
- Blender多镜头(多机位)切换
- Eslint reports an error
- Summary of demand R & D process nodes and key outputs
- Share a blog (water blog)
猜你喜欢

2837xd代码生成模块学习(3)——IIC、eCAN、SCI、Watchdog、eCAP模块

Mysql索引

Bugkuctf-web24 (problem solving ideas and steps)

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

2837xd代码生成模块学习(1)——GPIO模块
What is the relationship between realizing page watermarking and mutationobserver?

Matlab生成dsp程序——官方例程学习(6)

Alibaba cloud SMS service

In SQL injection, why must the ID of union joint query be equal to 0

2837xd 代码生成——StateFlow(1)
随机推荐
高考那些事
UE5——AI追逐(蓝图、行为树)
阿里云短信服务
PI control of three-phase grid connected inverter - off grid mode
2837xd code generation - stateflow (4)
Mixed development of uni app -- Taking wechat applet as an example
Remember a simple Oracle offline data migration to tidb process
Alibaba cloud SLS log service
UE illusory engine programmed plant generator setup -- how to quickly generate large forests
Matlab generates DSP program -- official routine learning (6)
Save video opencv:: videowriter
【UE5】蓝图制作简单地雷教程
Introduction et prévention des essais de pénétration
Commutateur Multi - lentilles Blender
How much is it to develop a system software in Beijing, and what funds are needed to develop the software
UE5——AI追逐(藍圖、行為樹)
Bugkuctf-web24 (problem solving ideas and steps)
ZK configuration center -- configuration and use of config Toolkit
MySQL default transaction isolation level and row lock
2837xd code generation - stateflow (3)