Divide and Conquer -- Leetcode problem169: 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.

  • 思路一:(Brute force algorithm)题目所讲为查找多数元素,根据题目要求直接思考下去可以很容易向到一个解法,通过一个变量记录每次查询的元素值,并于已经存在于数组之中的元素进行对比之后就可以得到需要寻找Majority Element
    时间复杂度:O( n 2 n^2 n2)

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int count = 0;
        int flag = 0; // major num
        int temp = 0;
        while (temp < nums.size()) {
            for (int i = 0 ; i < nums.size(); i++) {
                flag = nums[temp];
                if (flag == nums[i]) {
                    count++;
                }
            }
            if (count > nums.size()/2) {
                return flag;
            } else {
                temp++;
                count = 0;
            }
        }
    }
};

这种最容易想到的解法由于时间复杂度的问题导致超时,未能通过全部测试(42/44)

  • 思路二:(Map)既然这道题的本质是统计元素出现的个数,那么Map作为一种可以统计数目的容器就可被我们应用,通过一次循环配合Map中的find()函数对数组中不同个数进行统计,找到数目超过「n/2」的数字
    时间复杂度:O(n)
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        map<int, int>count;
        map<int, int>::iterator iter;
            for (int i = 0; i < nums.size(); i++) {
                iter = count.find(nums[i]);
                if (iter == count.end()) {
                    count[nums[i]] = 1;
                } else {
                    count[nums[i]]++;
            }
                if (count[nums[i]] > nums.size()/2)
                    return nums[i];
        }
        return -1;
    }
};
  • 思路三:(Divide and Conquer)这种分治法是最近上课正在学习的内容,现将数组分成左右两半,分别查找各部分中的多数元素后进行判断。
    时间复杂度:O(nlog(n))
class Solution {
public:
    int majorityElement(vector<int>& nums) {
       return majority(nums, 0 , nums.size() - 1);
    }
private:
    int majority(vector<int>& nums, int left, int right) {
        if (left == right) return nums[left];//the array has only one number
        int mid = (right - left) / 2 + left;
        int le = majority(nums, left, mid);
        int ri = majority(nums, mid + 1, right);
        if (le == ri) return le;
        if (count(nums.begin() + left, nums.begin() + right + 1, le) > count(nums.begin() + left, nums.begin() + right + 1, ri)) // decide which number is most in the array
            return le;
        else 
            return ri;
    }
};

-思路四:(Moore’s Voting Algorithm)这种算法是在Leetcode讨论区看到的一种非常简便的算法,这种算法不易被想到,但是又充满了智慧。
时间复杂度:O(n)

class Solution {
public:
    int majorityElement(vector<int>& nums) {
       int candidate = 0;
    int count = 1;
    for (int i = 1; i < nums.size(); i++) {
        if (nums[candidate] == nums[i]) count++;
        else count--;
        if (count == 0) {
            candidate = i;
            count = 1;
        }
    }
    return nums[candidate];
    }
};

在这种解法中每个数字都可以给自己投票,先以第一个数字为候选人,当后续数字与候选数字相同时,则这个候选数字票数加一,反之减一。当这个候选数字票数为零时,候选数字改变为使它发生改变对应的那个数字,同时这个新候选数字票数重置为一(这点很重要,否则会出现票数为负的情况)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值