publicclass FindKinN ...{ publicstaticvoid main(String[] args) ...{ int[] a =...{1,1,1,1,2,2,1,1,2,2,2,1}; int count,cur,n; count =0; cur =0; n = a.length; for (int i =0; i < n; i++) ...{ if (count ==0) cur = a[i]; if (cur == a[i]) count++; else count--; } System.out.println("你要找的是:"+ cur); } }
一个含N个整数的数组,其中一个元素出现次数 k&gt;N/2,找出这个元素。限O(N)时间,O(1)空间。 public class FindKinN ...{ public static void main(String[] args) ...{ int[] a = ...{1,1,1,1,2,2,1,1,2,2,2,1}; int count,cur,n; ...