package AlgorithmTest; /** * Created by dell on 2015/10/1. */ //一个数组中有一个数,出现的次数超过数组大小的一半 public class FindMostApperseTest { public static void main(String[] args) { int[] array = new int[]{2,1,1,2,3,1,1,1}; System.out.println(findMostApperse(array)); } public static int findMostApperse(int[] array){ int count = 0; int candidate = Integer.MIN_VALUE; for (int i : array){ if (0 == count){ candidate = i; ++count; }else{ if (candidate == i){ ++count; }else{ --count; } } } return candidate; } }
找数组中出现超过一半的数 JAVA
最新推荐文章于 2021-02-26 16:36:44 发布