当前位置:网站首页>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));
}
}
边栏推荐
- IDEA 导入导出 settings 设置文件
- Just remember Balabala
- CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
- Mysql22 logical architecture
- csdn-Markdown编辑器
- [leectode 2022.2.13] maximum number of "balloons"
- There are three iPhone se 2022 models in the Eurasian Economic Commission database
- A trip to Macao - > see the world from a non line city to Macao
- Opencv uses freetype to display Chinese
- February 13, 2022-3-middle order traversal of binary tree
猜你喜欢
CSDN问答标签技能树(五) —— 云原生技能树
windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Postman Interface Association
CSDN question and answer tag skill tree (I) -- Construction of basic framework
MySQL33-多版本并发控制
Swagger、Yapi接口管理服务_SE
MySQL28-数据库的设计规范
MySQL transaction log
MySQL 29 other database tuning strategies
随机推荐
解决:log4j:WARN Please initialize the log4j system properly.
Copie maître - esclave MySQL, séparation lecture - écriture
[untitled]
MySQL主从复制、读写分离
Mysql26 use of performance analysis tools
Mysql27 index optimization and query optimization
Mysql30 transaction Basics
CSDN问答模块标题推荐任务(一) —— 基本框架的搭建
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
【博主推荐】C#生成好看的二维码(附源码)
FRP intranet penetration
Baidu Encyclopedia data crawling and content classification and recognition
February 13, 2022 - Maximum subarray and
Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Adaptive Bezier curve network for real-time end-to-end text recognition
SSM integrated notes easy to understand version
MySQL主從複制、讀寫分離
MySQL24-索引的数据结构
Asp access Shaoxing tourism graduation design website