查找整数数组中的支配者,即出现次数超过50%的数
2013-08-23 15:20
351 查看
/** * 查找整数数组中的支配者,即出现次数超过50%的数 */ package test; import java.util.ArrayList; import java.util.Arrays; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {2,4,8,1,5,10,3,3,3,3,3,3,3,3,3,-5,0}; System.out.println("it is "+Judge(a)); } public static int Judge(int[] ints){ Arrays.sort(ints); System.out.println(ints.toString());//不能查看数组元素 int count = 1;//每个数的出现次数 for(int i=0; i<ints.length-1; i++){ if(ints[i]==ints[i+1]){ count++; if((double)count/ints.length>0.5){ return ints[i]; } }else{ count = 1; } } return -1; } }
相关文章推荐
- 查找数组中出现次数超过一半的数字
- Java实现——求整数数组中出现次数超过数组长度一半的元素
- 剑指Offer-29-java实现查找数组中出现次数超过一半的元素
- 现在有一个整数数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
- 找出整数数组中出现次数超过数组长度一半的元素(Java)
- 查找数组中出现次数超过一半的数
- 现在有一个整数数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
- 现在有一个整数数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
- 查找数组中出现次数超过一次的数字
- PHP 查找一个在数组中出现次数超过一半的数,时间复杂度O(n),空间复杂度O(1)
- 剑指Offer-29-查找数组中出现次数超过一般的元素
- 查找数组中超过出现次数超过一半的数字(找发帖水王)
- 数字数组查找数组中超过出现次数超过一半的数字(找发帖水王)
- 查找数组中出现次数超过数组长度一半的数字
- “支配着” 是在数组中出现频率超过一半的整数,求一个数组中的支配者
- 现在有一个整数数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
- 查找数组中某个出现次数超过数组长度一半的数字
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- 查找数组中出现次数超过一半的元素
- 剑指offer 数组中出现次数超过一半的数字