码迷,mamicode.com
首页 > 其他好文 > 详细

find the majority element

时间:2014-12-24 13:25:12      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

    1. Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
      1. If the counter is 0, we set the current candidate to x and the counter to 1.
      2. If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.
      After one pass, the current candidate is the majority element. Runtime complexity = O(n).

find the majority element

标签:

原文地址:http://www.cnblogs.com/miaoz/p/4182147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!