当前位置:网站首页>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;
}
}边栏推荐
- 05mysql lock analysis
- New can also create objects. Why do you need factory mode?
- 【校验】只能输入数字(正负数)
- 0616项目二结束~~总总结
- 0613 ~ self study
- Section 11 cache avalanche, hot data failure follow Daewoo redis ------- directory post
- Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t
- 0630~ professional quality course
- How to prepare for hyperinflation
- Alibaba 1688 keyword search product API usage display
猜你喜欢

【OpenCV】—阈值化

手写博客平台~第二天

【obs】依赖库: x264 vs 构建

Brats18 - Multimodal MR image brain tumor segmentation challenge continued

Inheritance and Derive

The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered

Go language file operation

Codeforces Round #794 (Div. 2)(A.B.C)

Common methods of number and math classes

Handwritten blog platform ~ the next day
随机推荐
0621~ES&Lucene
下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑
0627~ holiday knowledge summary
Go to bed capacity exchange
How to read "STL source code analysis"?
Shanghai Jiaotong University team used joint deep learning to optimize metabonomics research
Interview assault 66: what is the difference between request forwarding and request redirection?
【OpenCV】—阈值化
Example of single table query in ORM student management system
0630~ professional quality course
0613~自习
Ship new idea 2022.2 was officially released, and the new features are really fragrant!
船新 IDEA 2022.2 正式发布,新特性真香!
Use of jumpserver
steam API
JS数组方法 sort() 排序规则解析
Handwritten blog platform ~ the next day
0613 ~ self study
0629~SaaS平台设计~全局异常处理
Custom web framework