当前位置:网站首页>输入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;
}
}
}
}
}边栏推荐
- sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)
- 【QNX】命令
- 行锁与隔离级别案例分析
- Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
- pycharm如何修改多行注释快捷键
- Prometeus 2.34.0 新特性
- How pycharm modifies multiline annotation shortcuts
- Connected to surface test questions
- ZCMU--1367: Data Structure
- mysql Add column 失败 因为之前有数据,不是默认null 不行
猜你喜欢

Preparing for the Blue Bridge Cup and ccf-csp

14 MySQL tutorial insert insert data

MySQL exports all table indexes in the database

How pycharm modifies multiline annotation shortcuts

Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
![[code Capriccio - dynamic planning] t583. Deleting two strings](/img/01/fd9ff51ea1d70188e372e925d8a1c7.png)
[code Capriccio - dynamic planning] t583. Deleting two strings

Dos et détails de la méthode d'attaque

transforms.RandomCrop()的输入只能是PIL image 不能是tensor

解决pycharm里面每个字母占一格空格的问题

Runtimeerror: CUDA error: out of memory own solution (it is estimated that it is not applicable to most people in special circumstances)
随机推荐
MySQL add column failed because there was data before, not null by default
深入理解MySQL锁与事务隔离级别
ZCMU--1367: Data Structure
map和filter方法对于稀缺数组的处理
有依赖的背包问题
并发之Synchronized说明
Treasure and niche CTA animation material website sharing
【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
vutils. make_ A little experience of grid () in relation to black and white images
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
17.13 补充知识、线程池浅谈、数量谈、总结
陈强:阿里千亿级大规模数字商业知识图谱助力业务增长
17.13 supplementary knowledge, thread pool discussion, quantity discussion and summary
transforms. The input of randomcrop() can only be PIL image, not tensor
离婚协议中的几个重点
力扣每日一题-第28天-566.重塑矩阵
Please advise tonghuashun which securities firm to choose for opening an account? Is it safe to open an account online now?
How sparksql returns a specific day of the week by date -dayofweek function
背包问题求方案数
RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)