当前位置:网站首页>记某公司面试算法题:查找一个有序数组某个数字出现的次数
记某公司面试算法题:查找一个有序数组某个数字出现的次数
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));
}
}
边栏推荐
- 解决:log4j:WARN Please initialize the log4j system properly.
- Mysql36 database backup and recovery
- MySQL 20 MySQL data directory
- Global and Chinese market of transfer switches 2022-2028: Research Report on technology, participants, trends, market size and share
- Moteur de stockage mysql23
- [BMZCTF-pwn] 11-pwn111111
- Mysql25 index creation and design principles
- Mysql21 - gestion des utilisateurs et des droits
- How to find the number of daffodils with simple and rough methods in C language
- There are three iPhone se 2022 models in the Eurasian Economic Commission database
猜你喜欢
MySQL31-MySQL事务日志
API learning of OpenGL (2002) smooth flat of glsl
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
MySQL master-slave replication, read-write separation
解决:log4j:WARN Please initialize the log4j system properly.
A trip to Macao - > see the world from a non line city to Macao
MySQL主从复制、读写分离
Navicat 導出錶生成PDM文件
CSDN问答模块标题推荐任务(一) —— 基本框架的搭建
[C language] deeply analyze the underlying principle of data storage
随机推荐
CSDN问答标签技能树(二) —— 效果优化
[recommended by bloggers] C # generate a good-looking QR code (with source code)
Invalid global search in idea/pychar, etc. (win10)
CSDN question and answer tag skill tree (I) -- Construction of basic framework
[leectode 2022.2.13] maximum number of "balloons"
How to change php INI file supports PDO abstraction layer
MySQL27-索引优化与查询优化
虚拟机Ping通主机,主机Ping不通虚拟机
Opencv uses freetype to display Chinese
[BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
【博主推荐】C#生成好看的二维码(附源码)
[C language foundation] 04 judgment and circulation
MySQL26-性能分析工具的使用
MySQL32-锁
Global and Chinese market of thermal mixers 2022-2028: Research Report on technology, participants, trends, market size and share
Ansible实战系列二 _ Playbook入门
Win10: how to modify the priority of dual network cards?
解决扫描不到xml、yml、properties文件配置
35 is not a stumbling block in the career of programmers
@Controller, @service, @repository, @component differences