当前位置:网站首页>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));
}
}
边栏推荐
- windows下同时安装mysql5.5和mysql8.0
- Mysql35 master slave replication
- Just remember Balabala
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
- The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
- Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
- C语言标准的发展
- [Thesis Writing] how to write function description of jsp online examination system
- 项目实战-后台员工信息管理(增删改查登录与退出)
猜你喜欢
MySQL21-用户与权限管理
CSDN Q & a tag skill tree (V) -- cloud native skill tree
MySQL23-存儲引擎
Redis的基础使用
Postman Interface Association
Asp access Shaoxing tourism graduation design website
Mysql35 master slave replication
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Emotional classification of 1.6 million comments on LSTM based on pytoch
Copie maître - esclave MySQL, séparation lecture - écriture
随机推荐
MySQL21-用户与权限管理
CSDN问答标签技能树(一) —— 基本框架的构建
Discriminant model: a discriminant model creation framework log linear model
Why is MySQL still slow to query when indexing is used?
记某公司面试算法题:查找一个有序数组某个数字出现的次数
Postman uses scripts to modify the values of environment variables
February 13, 2022-2-climbing stairs
CSDN question and answer tag skill tree (I) -- Construction of basic framework
Mysql23 storage engine
Baidu Encyclopedia data crawling and content classification and recognition
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
CSDN blog summary (I) -- a simple first edition implementation
[Thesis Writing] how to write function description of jsp online examination system
CSDN Q & a tag skill tree (V) -- cloud native skill tree
February 13, 2022-3-middle order traversal of binary tree
Swagger、Yapi接口管理服务_SE
windows下同时安装mysql5.5和mysql8.0
CSDN问答标签技能树(五) —— 云原生技能树
Navicat 導出錶生成PDM文件
Generate PDM file from Navicat export table