数组中出现次数超过一半的数字
来源:互联网 发布:守望先锋吧被关 知乎 编辑:程序博客网 时间:2024/06/05 08:11
题目描述
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
代码如下
import java.util.*;/** * Created by CQH on 2017/9/21. */public class NewCode1 { //思想:如果数组中有满足条件的数字,那么这个数字出现的次数和一定超过其余数字 // 也就是说,这个数字一定是数组中间的那个数(数组排好序后) public int MoreThanHalfNum_Solution(int [] array) { Arrays.sort(array); int mid = array[array.length /2]; int count = 0; for(int i = 0; i<array.length; i++){ if(array[i] == mid){ count++; } } if(count > array.length /2){ return mid; }else { return 0; } }}
阅读全文
0 0
- 数组中超过出现次数超过一半的数字
- 74.数组中超过出现次数超过一半的数字
- 74 数组中超过出现次数超过一半的数字
- 【数组4】数组中出现次数超过一半的数字
- 数组4:数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 找出数组中出现次数超过长度一半的数字
- 2-数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字[算法]
- 数组中出现次数超过一半的数字
- 找出数组中出现次数超过一半的数字--百度
- 数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 寻找数组中出现次数超过一半的数字
- 面试训练数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- C# 获取文件名和扩展名
- 声纹识别技术助力远程身份认证
- Mybatis框架个人总结
- junit 测试问题 No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency
- CSS hack介绍
- 数组中出现次数超过一半的数字
- Spark源码解析SparkStreaming数据接收
- Android SD卡文件存储
- IP 协议帧格式
- 江南大学吴小俊:深度学习不能代表人工智能的全部
- 使用js动态绘制报表
- 【Angular】Angular+Ionic报错:No provider for *Service!
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
- mt6392介绍