当前位置:网站首页>输入n个整数,输出出现次数大于等于数组长度一半的数
输入n个整数,输出出现次数大于等于数组长度一半的数
2022-06-26 18:01:00 【一只懐坏旭】
牛客网·互联网名企笔试/面试题库
一,题目及描述:
输入n个整数,输出出现次数大于等于数组长度一半的数
输入描述:
每个测试输入包含 n个空格分割的n个整数,n不超过100,其中有一个整数出现次数大于等于n/2。
输出描述:
输出出现次数大于等于n/2的数。
二,示例
输入
3 9 3 2 5 6 7 3 2 3 3 3
输出
3
三,解题思路及代码
!!!!首先得注意只存在一个这样的数
思路1:
对输入序列进行排序,中间位置的元素即为出现现次数大于等于n/2的数
代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.nextLine(); //输入接收为字符串,并且每个字符以空格隔开
String[] s = str.split(" "); //将字符串以空格拆分开
int[] arr = new int[s.length]; //准备一个数组
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(s[i]); //将元素转换类型并移动到准备好的数组中
}
System.out.println(arr[arr.length/2]);//取出中间元素即可
}
}
}思路2:
利用Map统计每个元素的出现次数,输出Value大于等于n/2对应的key即可
代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.nextLine(); //输入接收为字符串,并且每个字符以空格隔开
String[] s = str.split(" "); //将字符串以空格拆分开
Integer[] arr = new Integer[s.length]; //准备一个数组(Integer最好,int也可以,此时用Map,所以用包装类型避免拆箱)
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(s[i]); //将元素转换类型并移动到准备好的数组中
}
Map<Integer,Integer> map = new HashMap<>();
for (int j = 0; j < arr.length; j++) {
//统计每个元素的个数
Integer count = map.get(arr[j]); //取出当前key,对应得Value
if(count == null){
map.put(arr[j],1); //如果是第一次放入,即Value置为1
} else {
map.put(arr[j],count+1);//否则在原来的基础+1
}
}
for(Map.Entry<Integer,Integer> entry : map.entrySet()){
//遍历Map找到大于等于数组长度一半的Value对应的key
if(entry.getValue() >= (arr.length/2)){
int x = entry.getKey();
System.out.println(x);
break;
}
}
}
}
}边栏推荐
- Binary search-2
- RSA概念详解及工具推荐大全 - lmn
- RSA concept explanation and tool recommendation - LMN
- vue--vuerouter缓存路由组件
- How about opening an account at Guojin securities? Is it safe?
- [QNX] Command
- 【NPOI】C#跨工作薄复制Sheet模板导出Excel
- Leetcode - 226. Retourner l'arbre binaire (bfs)
- Please advise tonghuashun which securities firm to choose for opening an account? Is it safe to open an account online now?
- 国信证券怎么开户?通过链接办理股票开户安全吗
猜你喜欢

有依赖的背包问题
![[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers

A little experience of next (ITER (dataloader))

sparksql如何通过日期返回具体周几-dayofweek函数

Concept and working principle of data encryption standard (DES)

RSA concept explanation and tool recommendation - LMN

无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!

MySQL exports all table indexes in the database

in和exsits、count(*)查询优化

properties文件乱码
随机推荐
Leetcode daily [2022 - 02 - 16]
力扣每日一题-第28天-566.重塑矩阵
Using redis for user access data statistics hyperloglog and bitmap advanced data types
The king of Internet of things protocol: mqtt
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!
Chen Qiang: Alibaba's 100 billion level large-scale digital business knowledge map helps business growth
Map和List<Map>转相应的对象
17.13 补充知识、线程池浅谈、数量谈、总结
DoS及攻擊方法詳解
Let torch cuda. is_ Experience of available() changing from false to true
行锁与隔离级别案例分析
How sparksql returns a specific day of the week by date -dayofweek function
17.13 supplementary knowledge, thread pool discussion, quantity discussion and summary
Synchronized description of concurrency
【动态规划】剑指 Offer II 091. 粉刷房子
Comp281 explanation
我想知道,我在肇庆,到哪里开户比较好?网上开户是否安全么?
分页查询、JOIN关联查询优化
MySQL add column failed because there was data before, not null by default
【万字总结】以终为始,详细分析高考志愿该怎么填