当前位置:网站首页>Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
2022-07-06 11:00:00 【Sanli akaman】
Record
Remember the interview algorithm of a company : Find the number of times a number appears in an ordered array , The requirement is not to use violent cracking ;
I knew how to do it , As a result, I couldn't write two points, so I couldn't tear it out , It's really enough ..
Code
package com.vleus.algorithm.strings;
/** * @author vleus * @date 2021 year 09 month 26 Japan 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;
// }
// Two points search
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));
}
}
边栏推荐
- MySQL25-索引的创建与设计原则
- [recommended by bloggers] C # generate a good-looking QR code (with source code)
- Mysql24 index data structure
- Mysql27 index optimization and query optimization
- Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
- Mysql23 storage engine
- CSDN问答模块标题推荐任务(二) —— 效果优化
- MySQL主從複制、讀寫分離
- MySQL flush operation
- MySQL master-slave replication, read-write separation
猜你喜欢
CSDN博文摘要(一) —— 一个简单的初版实现
Redis的基础使用
IDEA 导入导出 settings 设置文件
MySQL18-MySQL8其它新特性
MySQL30-事务基础知识
Mysql23 storage engine
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
Pytoch LSTM implementation process (visual version)
MySQL20-MySQL的数据目录
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
随机推荐
[leectode 2022.2.13] maximum number of "balloons"
There are three iPhone se 2022 models in the Eurasian Economic Commission database
解决:log4j:WARN Please initialize the log4j system properly.
MySQL21-用户与权限管理
【博主推荐】SSM框架的后台管理系统(附源码)
MySQL29-数据库其它调优策略
MySQL master-slave replication, read-write separation
[reading notes] rewards efficient and privacy preserving federated deep learning
Ansible实战系列二 _ Playbook入门
csdn-Markdown编辑器
API learning of OpenGL (2003) gl_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T
Swagger, Yapi interface management service_ SE
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
MySQL完全卸载(Windows、Mac、Linux)
MySQL20-MySQL的数据目录
Breadth first search rotten orange
CSDN博文摘要(一) —— 一个简单的初版实现
windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
Postman Interface Association
Mysql33 multi version concurrency control