算法题目一:如果数组中数字重复最多的次数大于数组长度的一半,返回1,否则返回0
来源:互联网 发布:淘宝怎么兴业扫码付款 编辑:程序博客网 时间:2024/06/06 03:13
public class TestOne { public static void main(String[] args) { int[] str = {5,5,5,5,5,5,5,5,5,5,5,5,4,4,4,6,4,6,3,7}; System.out.println(solution(str)); } public static int solution(int[] A){ int L = A.length; Map<Integer,Integer> map = new HashMap<>(); for(int i = 0 ; i < L ; i ++){ map.put(A[i],0); } int M = map.size(); int Amid = L / 2; int i = 0; int count = 0; int[] B = new int[M]; int[] compirecounts = new int[M]; for(Integer key : map.keySet()){ B[i] = key; i++; } for(int j = 0;j<M;j++){ count = 0; for(int k = 0; k < A.length;k++){ if(B[j]==A[k]){ count++; } } compirecounts[j]=count; } if(getMax(compirecounts)>Amid){ return 1; }else { return -1; } } public static int getMax(int[] A){ int max = Integer.MIN_VALUE; for(int i = 0;i < A.length;i++){ if(A[i]>max) max=A[i]; } return max; }}
0 0
- 算法题目一:如果数组中数字重复最多的次数大于数组长度的一半,返回1,否则返回0
- 找到给定数组中出现次数大于长度一半的数字,若没有,则返回0
- 找出数组中的一个元素出现次数大于数组长度的一半的元素,若不存在返回0
- 算法题目---数组中出现次数超过一半的数字
- 数组中重复次数最多的数字
- 数组中出现次数大于一半的数字
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- php算法:找出数组中出现次数超过数组长度一半的数字
- 找出数组中出现次数超过长度一半的数字
- 数组中出现次数超过长度一半的数字
- 题目1370:数组中出现次数超过一半的数字
- **题目1370:数组中出现次数超过一半的数字
- 题目1370:数组中出现次数超过一半的数字
- 题目1370:数组中出现次数超过一半的数字
- 算法: 返回一串字符串中重复次数最多的字符
- 求出现次数大于数组长度一半的那个数
- 求数组中出现次数大于数组长度一半的元素
- 查找数组中重复次数最多的数字
- JAVA帮助文档全系列说明
- sqlite3 笔记整理
- tomcat单个ip配置多个域名及配置用户角色查看服务器状态
- hdu2196(树的直径)
- postgresql主备之the database system is starting up
- 算法题目一:如果数组中数字重复最多的次数大于数组长度的一半,返回1,否则返回0
- 物理地址、逻辑地址
- [手游新项目历程]第13天-游戏玩家怪物移动同步
- nginx: [alert] version 1.4.0 of nginx.pm is required, but 1.2.0 was found
- 2809: [Apio2012]dispatching|splay启发式合并
- bzoj2535: [Noi2010]Plane 航空管制
- 使用免费的产品搭建直播系统
- cocos2dx-3.x iOS 植入芒果广告
- 数据库中间件Mycat之主从读写分离