当前位置:网站首页>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));
}
}
边栏推荐
- CSDN博文摘要(一) —— 一个简单的初版实现
- CSDN问答标签技能树(二) —— 效果优化
- [Li Kou 387] the first unique character in the string
- Postman Interface Association
- Mysql27 - Optimisation des index et des requêtes
- Postman uses scripts to modify the values of environment variables
- [BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
- Why is MySQL still slow to query when indexing is used?
- Opencv uses freetype to display Chinese
- February 13, 2022 - Maximum subarray and
猜你喜欢
![[recommended by bloggers] asp Net WebService background data API JSON (with source code)](/img/04/c721e6177b578b30cbbf334cb1b6c9.png)
[recommended by bloggers] asp Net WebService background data API JSON (with source code)

Mysql24 index data structure

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

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

Mysql21 - gestion des utilisateurs et des droits

Swagger、Yapi接口管理服务_SE

API learning of OpenGL (2003) gl_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T
![[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)](/img/b7/aae35f049ba659326536904ab089cb.png)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)

Adaptive Bezier curve network for real-time end-to-end text recognition

Postman uses scripts to modify the values of environment variables
随机推荐
Invalid global search in idea/pychar, etc. (win10)
A trip to Macao - > see the world from a non line city to Macao
CSDN博文摘要(一) —— 一个简单的初版实现
MySQL23-存儲引擎
Mysql25 index creation and design principles
MySQL19-Linux下MySQL的安装与使用
Global and Chinese market of operational amplifier 2022-2028: Research Report on technology, participants, trends, market size and share
35 is not a stumbling block in the career of programmers
La table d'exportation Navicat génère un fichier PDM
Mysql34 other database logs
CSDN Q & a tag skill tree (V) -- cloud native skill tree
Mysql28 database design specification
Ansible实战系列一 _ 入门
Water and rain condition monitoring reservoir water and rain condition online monitoring
February 13, 2022-2-climbing stairs
Installation and use of MySQL under MySQL 19 Linux
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
Win10: how to modify the priority of dual network cards?
Adaptive Bezier curve network for real-time end-to-end text recognition
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine