23. leetcode 169. Majority Element

169. Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than  n/2  times.

You may assume that the array is non-empty and the majority element always exist in the array.

思路1:hash表

23. leetcode 169. Majority Element

思路2:

此题目是求数组中出现超过一半以上次数的数,所以设置一个element存储当前出现次数最多的数,设置count记录它当前出现的频数,每遇到一个和element不同的数就将count减一。

23. leetcode 169. Majority Element

上一篇:基础知识系列☞Abstract和Virtual→及相关知识


下一篇:Week1 - 169.Majority Element