当前位置:网站首页>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));
}
}
边栏推荐
- MySQL主從複制、讀寫分離
- Swagger, Yapi interface management service_ SE
- Kubernetes - problems and Solutions
- A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
- Global and Chinese market of operational amplifier 2022-2028: Research Report on technology, participants, trends, market size and share
- [Li Kou 387] the first unique character in the string
- MySQL33-多版本并发控制
- [C language foundation] 04 judgment and circulation
- Install MySQL for Ubuntu 20.04
- Mysql34 other database logs
猜你喜欢

Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved

MySQL19-Linux下MySQL的安装与使用

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

Mysql27 - Optimisation des index et des requêtes
![[Thesis Writing] how to write function description of jsp online examination system](/img/f8/13144e0febf4a576bbcc3290192079.jpg)
[Thesis Writing] how to write function description of jsp online examination system

MySQL主从复制、读写分离

CSDN question and answer module Title Recommendation task (II) -- effect optimization

CSDN markdown editor
![[C language foundation] 04 judgment and circulation](/img/59/4100971f15a1a9bf3527cbe181d868.jpg)
[C language foundation] 04 judgment and circulation

连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
随机推荐
windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
MySQL23-存儲引擎
Kubernetes - problems and Solutions
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
Ansible practical Series II_ Getting started with Playbook
解决扫描不到xml、yml、properties文件配置
Global and Chinese market of transfer switches 2022-2028: Research Report on technology, participants, trends, market size and share
[BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
API learning of OpenGL (2002) smooth flat of glsl
记一次某公司面试题:合并有序数组
MySQL other hosts cannot connect to the local database
MySQL completely uninstalled (windows, MAC, Linux)
虚拟机Ping通主机,主机Ping不通虚拟机
npm一个错误 npm ERR code ENOENT npm ERR syscall open
Discriminant model: a discriminant model creation framework log linear model
Mysql35 master slave replication
Timestamp with implicit default value is deprecated error in MySQL 5.6
Some problems in the development of unity3d upgraded 2020 VR
Ansible practical Series III_ Task common commands