当前位置:网站首页>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
边栏推荐
- shell编程之免交互
- SQL statement learning and the use of pymysql
- Synchronous use reference of the new version of data warehouse (for beginners)
- 第7章 异常处理
- Shell编程之正则表达式(Shell脚本文本三剑客之grep)
- 剑指 Offer 笔记: T58 - I. 翻转单词顺序
- 剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
- Local virtual machine initialization script
- 微机和单片机的区别
- 为什么TCP三次握手的时候ACK=Seq+1
猜你喜欢

Modelarts voice detection and text classification

Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法
Synchronous use reference of the new version of data warehouse (for beginners)

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

makefile模板

美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生

Wilcoxon rank sum and signed rank
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!

Why is ack=seq+1 when TCP shakes hands three times

MATLAB画带延时系统的伯德图
随机推荐
新版数据仓库的同步使用参考(新手向)
Local virtual machine initialization script
C programming language (2nd Edition) -- Reading Notes -- 1.5.2
【机器学习-白板推导系列】学习笔记---概率图模型和指数族分布
The C programming language (2nd) -- Notes -- 1.6
Solution of digital tube flash back after proteus8 professional version cracking
源码编译安装LAMP
Could not load dynamic library ‘libcudnn.so.8‘;
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
Beyond Compare 3 下一个差异段/向下搜索箭头 找不到了
Analysis of the use of JUC framework from runnable to callable to futuretask
What is private traffic?
Shell编程之正则表达式(Shell脚本文本三剑客之grep)
Could not load dynamic library ‘libcudnn.so.8‘;
82.(cesium之家)cesium点在3d模型上运动
LeetCode 02: 剑指 Offer 58 - I. 翻转单词顺序(简单); T123. 验证回文串 ; T9. 回文数
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
CH340模块无法识别/烧写不进的一种可能性
Modelarts image classification and object detection