当前位置:网站首页>记某公司面试算法题:查找一个有序数组某个数字出现的次数
记某公司面试算法题:查找一个有序数组某个数字出现的次数
2022-07-06 09:13:00 【三笠·阿卡曼】
记录
记某公司面试算法题:查找一个有序数组某个数字出现的次数,要求是不能使用暴力破解的方式;
本来知道怎么做,结果因为不会写二分导致没手撕出来,着实够菜的。。
代码
package com.vleus.algorithm.strings;
/** * @author vleus * @date 2021年09月26日 20:26 */
public class GetNumCount {
// public static int getNumCount(int[] array, int num) {
//
// if (array.length == 0) {
// return 0;
// }
//
// int count = 0;
// for (int i = 0; i < array.length; i++) {
// if (array[i] == num) {
// count++;
// }
// }
//
// return count;
// }
//二分查找
private static int times(int[] arr, int n) {
int low = 0;
int high = arr.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] >= n) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
public static int getNumCount2(int[] arr, int num) {
int first = times(arr, num);
int last = times(arr, num + 1);
int times = (first == arr.length || arr[first] != num) ? 0 : last - first;
return times;
}
public static void main(String[] args) {
int[] arr = {
1, 2, 3, 3, 3, 4, 5};
System.out.println(getNumCount2(arr,6));
}
}
边栏推荐
- Advantages and disadvantages of evaluation methods
- CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
- Mysql24 index data structure
- Moteur de stockage mysql23
- frp内网穿透那些事
- There are three iPhone se 2022 models in the Eurasian Economic Commission database
- 导入 SQL 时出现 Invalid default value for ‘create_time‘ 报错解决方法
- Valentine's Day is coming, are you still worried about eating dog food? Teach you to make a confession wall hand in hand. Express your love to the person you want
- Bytetrack: multi object tracking by associating every detection box paper reading notes ()
- ++Implementation of I and i++
猜你喜欢

MySQL24-索引的数据结构

Isn't there anyone who doesn't know how to write mine sweeping games in C language

【博主推荐】C# Winform定时发送邮箱(附源码)

CSDN blog summary (I) -- a simple first edition implementation

Solution: log4j:warn please initialize the log4j system properly

Redis的基础使用

解决:log4j:WARN Please initialize the log4j system properly.

【博主推荐】asp.net WebService 后台数据API JSON(附源码)

Mysql21 user and permission management

MySQL29-数据库其它调优策略
随机推荐
Pytoch LSTM implementation process (visual version)
Use of dataset of pytorch
Nanny hand-in-hand teaches you to write Gobang in C language
Kubesphere - deploy the actual combat with the deployment file (3)
Idea import / export settings file
【博主推荐】C#生成好看的二维码(附源码)
API learning of OpenGL (2001) gltexgen
csdn-Markdown编辑器
MySQL34-其他数据库日志
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
Moteur de stockage mysql23
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
Breadth first search rotten orange
C language string function summary
Swagger、Yapi接口管理服务_SE
Postman environment variable settings
Copy constructor template and copy assignment operator template
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Redis的基础使用
API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB