当前位置:网站首页>记某公司面试算法题:查找一个有序数组某个数字出现的次数
记某公司面试算法题:查找一个有序数组某个数字出现的次数
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));
}
}
边栏推荐
- A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
- MySQL19-Linux下MySQL的安装与使用
- Mysql33 multi version concurrency control
- MySQL completely uninstalled (windows, MAC, Linux)
- Timestamp with implicit default value is deprecated error in MySQL 5.6
- CSDN question and answer tag skill tree (I) -- Construction of basic framework
- MySQL master-slave replication, read-write separation
- CSDN问答模块标题推荐任务(二) —— 效果优化
- [recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
- CSDN question and answer module Title Recommendation task (II) -- effect optimization
猜你喜欢
![[C language] deeply analyze the underlying principle of data storage](/img/d6/1c0cd38c75da0d0cc1df7f36938cfb.png)
[C language] deeply analyze the underlying principle of data storage

【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)

MySQL27-索引优化与查询优化

Mysql36 database backup and recovery

A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon

windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止

CSDN问答模块标题推荐任务(二) —— 效果优化

Mysql34 other database logs

MySQL transaction log

Breadth first search rotten orange
随机推荐
Opencv uses freetype to display Chinese
API learning of OpenGL (2002) smooth flat of glsl
API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB
MySQL23-存储引擎
MySQL master-slave replication, read-write separation
MySQL27-索引优化与查询优化
Mysql32 lock
[recommended by bloggers] background management system of SSM framework (with source code)
MySQL other hosts cannot connect to the local database
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
MySQL26-性能分析工具的使用
MySQL completely uninstalled (windows, MAC, Linux)
[Li Kou 387] the first unique character in the string
MySQL 29 other database tuning strategies
Kubernetes - problems and Solutions
MySQL20-MySQL的数据目录
Mysql27 - Optimisation des index et des requêtes
导入 SQL 时出现 Invalid default value for ‘create_time‘ 报错解决方法
Install mysql5.5 and mysql8.0 under windows at the same time
MySQL23-存儲引擎