题目概述
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.
Example 1:
1 |
Input: [3,2,3] |
Example 2:
1 |
Input: [2,2,1,1,1,2,2] |
题目要求找到数组中的众数。我是把数组进行排序后从左到右找,如果当前数字和之前的数字一样,就会给一个记录值加一,如果发现当前数字与其之前的数字不一样,则把记录值和全局最大值进行比较。
代码实现
1 |
class : |
近期评论