当前位置:网站首页>Sword finger offer notes: T53 - I. find numbers in the sorted array
Sword finger offer notes: T53 - I. find numbers in the sorted array
2022-07-27 11:48:00 【Ignorant little nine】
T53 - 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
Limit :
0 <= The length of the array <= 50000
Ideas
Two points , seek target and target-1 The right border of , Subtraction is the answer
solution 1
class Solution {
public int search(int[] nums, int target) {
return rightMargin(nums, target) - rightMargin(nums, target-1);
}
int rightMargin(int[] nums, int target){
int left =0, right =nums.length-1;
while(left<=right){
int mid = left+((right-left)>>1);
if(nums[mid]>target){
right = mid-1;
}else if(nums[mid]<=target){
left = mid+1;
}
}
return left;
}
}
Execution time :0 ms, In all Java Defeated in submission **100.00%** Users of
Memory consumption :41.3 MB, In all Java Defeated in submission **63.01%** Users of
Time complexity :O(logn). The time complexity of binary search is O(logn)
Spatial complexity :O(1). Just save the left and right boundaries and intermediate values
边栏推荐
- C programming language (2nd Edition) -- Reading Notes -- 1.3
- torch‘ has no attribute ‘inference_mode‘
- zabbix自定义监控项
- ZABBIX custom monitoring items
- 第10章 枚举类与注解
- SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
- JUC框架 从Runnable到Callable到FutureTask 使用浅析
- [machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution
- Principle of PWM and generation of PWM wave
- Everything cannot be searched for startup_ Lpc11x.s file
猜你喜欢

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

LNMP architecture setup (deploy discuz Forum)

Error encountered in adding quick open option to right-click menu:

Detailed explanation of hash table
![[unity entry program] creator kitfps: first person shooting 3D game](/img/2b/78b535973b2898f53752ceeb25ef01.png)
[unity entry program] creator kitfps: first person shooting 3D game

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

Everything cannot be searched for startup_ Lpc11x.s file

求不同采样周期下的传递函数有限零点

VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景

Could not load dynamic library ‘libcudnn.so.8‘;
随机推荐
Temporary use of solo, difficult choice of Blog
w.r.t. ; i.e.;etc.;e.g.是什么意思
第13章 IO流
Why choose smart TV?
Maker Hongmeng application development training notes 03
Maker Hongmeng application development training notes 02
检定和校准的区别
Maker paper data search training notes
82. (cesium home) cesium points move on 3D models
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis
Shell script text three swordsmen sed
多家银行调整现金管理类理财产品申赎规则:申赎确认时效“T+0”变“T+1”
Ask the big guys, is there transaction control for using flick sink data to MySQL? If at a checkpoint
为什么TCP三次握手的时候ACK=Seq+1
你真的会写二分查找吗——变种二分查找
Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法
The C programming language-2nd-- notes -- 4.11.3
LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
Some commonly used shortcut keys for MathType
w.r.t. ; i.e.; etc.; e. G. what does it mean