题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
public class Solution { public int MoreThanHalfNum_Solution(int [] array) { int[] a = new int[10001]; for (int i1 : array) { a[i1] = a[i1] + 1; if (a[i1] > array.length / 2) { return i1; } } return array[0]; } }
hash变种 时间o(n),空间o(n)