当前位置:网站首页>Number of times a number appears in an ascending array
Number of times a number appears in an ascending array
2022-07-24 18:13:00 【Xiao Liu xuezhuo】
Knowledge point Array Two points
describe
Given a length of n And a nonnegative integer k , Ask for statistics k The number of occurrences in the array
Data range :0 < n < 1000 , 0 < k < 100, The value of each element in the array satisfies 0 < val < 100
requirement : Spatial complexity O(1), Time complexity O(logn)

Obviously , The given array is non descending , That is, the array is in ascending order , It's just that there are elements of the same size . This problem is to use binary search to locate the element first k Position in the array , Then look for elements of the same size as yourself in the left and right directions respectively from this position and count the number . Because arrays are ordered , So stop searching in this direction as long as you find an element that is not as big as yourself in each direction .
The main investigation is whether you can write binary search , And some fine nodes when writing binary search , For example, recursive call , Sub array head and tail critical value processing , And how recursive calls exit . Here is the code , Write a separate article to sort out the details .
public class Solution {
public int GetNumberOfK(int [] array , int k) {
if (array == null || array.length == 0) {
return 0;
}
// Let's look in two k Where it appears in the array , Then traverse left and right from this position k Number of occurrences
int length = array.length;
int index = binarySearch(array, k, 0, length);
if (-1 == index) {
return 0;
}
int i = index - 1;
int j = index + 1;
int count = 1;
boolean findLeft = true;
boolean findRight= true;
while(findLeft || findRight) {
if (findLeft) {
if (i >= 0 && k == array[i]) {
count++;
i--;
} else {
findLeft = false;
}
}
if (findRight) {
if (j < length && k == array[j]) {
count++;
j++;
} else {
findRight = false;
}
}
}
return count;
}
// Binary search algorithm
public int binarySearch(int [] array, int k, int start, int end) {
if (start == end) {
return -1;
}
int mid = (start + end) / 2;
if (k == array[mid]) {
return mid;
} else if (k < array[mid]) {
end = mid;
} else {
start = mid + 1;
}
int index = binarySearch(array, k, start, end);
return index;
}
}边栏推荐
- com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
- Go to bed capacity exchange
- 2022 the latest short video de watermarking analysis API interface sharing
- 猜JWT关键字
- 【校验】只能输入数字(正负数)
- After separation, the impression notes are still difficult to live, but there are many coquettish operations
- 0613 ~ self study
- 0613~自习
- jmeter -- prometheus+grafana服务器性能可视化
- Still reading logs on the command line? Use kibana, visual log analysis yyds!
猜你喜欢

Pycharm configuring opencv Library

如何用WebGPU流畅渲染百万级2D物体?

Go language interface and type

Section 7 Data Dictionary: hash followed by Daewoo redis ------- directory post

Shengxin commonly used analysis graphics drawing 02 -- unlock the essence of volcano map!

关于接口的写法 1链式判读 ?. 2方法执行 (finally)一定会执行

0630~职业素养课

【OpenCV】—阈值化

Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures

T245982 "kdoi-01" drunken flower Yin
随机推荐
Section 9 cache penetration follow Daewoo redis ------- directory posts
How to render millions of 2D objects smoothly with webgpu?
【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单
0623~放假自习
Pay close attention! List of the latest agenda of 2022 open atom open source Summit
Codeforces Round #794 (Div. 2)(A.B.C)
0701~放假总结
Emerging potential of interactive virtual reality technology in drug discovery
(mandatory) override equals must override hashcode (principle analysis)
Flatten array.Flat (infinity)
After separation, the impression notes are still difficult to live, but there are many coquettish operations
Simple test JS code
Growth of operation and maintenance Xiaobai - week 8 of Architecture
How to quickly upload files to Google Lab
再见收费的Navicat!这款开源的数据库管理工具界面更炫酷!
T245982 "kdoi-01" drunken flower Yin
Example of single table query in ORM student management system
Guess JWT keyword
猜JWT关键字
0615~用自定义注解实现RBAC权限管理