当前位置:网站首页>[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)
- Project practice, redis cluster technology learning (11)
- Commutateur Multi - lentilles Blender
- 2837xd 代码生成——StateFlow(2)
- 虚幻——动画蓝图、状态机制作人物走跑跳动作
- C language programming problems
- 2837xd 代码生成——补充(1)
- Share a blog (water blog)
- 2837xd code generation module learning (1) -- GPIO module
- Tools used for Yolo object recognition and data generation
猜你喜欢
2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
Error reporting on the first day of work (incomplete awvs unloading)
Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
ZK configuration center -- configuration and use of config Toolkit
[illusory] weapon slot: pick up weapons
Matlab代码生成之SIL/PIL测试
Memories of a chat
What wires are suitable for wiring on bread board?
2837xd 代碼生成——補充(1)
UE4夜间打光笔记
随机推荐
XA Transaction SQL Statements
2837xd 代码生成——StateFlow(3)
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
2837xd code generation - Summary
The primary market project galaxy will conduct public offering on coinlist on February 17
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
2837xd 代码生成——补充(1)
【虚幻】按键开门蓝图笔记
Blender多镜头(多机位)切换
2837xd 代码生成——补充(2)
Image recognition - Data Acquisition
Following nym, the new project Galaxy token announced by coinlist is gal
虚幻材质编辑器基础——如何连接一个最基本的材质
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 - Supplement (1)
Cmake command - Official Document
2837xd 代码生成——补充(3)
Remember a simple Oracle offline data migration to tidb process
Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content
虚幻AI蓝图基础笔记(万字整理)