题目如下:
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. 最简单的方法,遍历整个数组,两层for循环,可以用set容器来排除前面已经出现过的元素,当重复数组元素很多时可以起到很好的效果,代码如下:
class Solution {
public:
int majorityElement(vector<int> & nums) {
int n = nums.size();
set<int> temp;
for (int i = 0; i < n; ++i) {
if (temp.find(nums[i]) == temp.end()) {
temp.insert(nums[i]);
int count