[LintCode] Majority Number(以时间复杂度O(n)求主元素)

一个数据序列的主元素,是指序列中出现次数超过序列长度一半的元素。

法1(期望时间复杂度为O(n)):

由于主元素出现次数超过序列长度的一半,因此,主元素一定是中位数。可以利用递归划分求中位数的方法,期望时间复杂度为O(n)。

法2:

显然,如果一个序列存在主元素,那么我们去掉序列中不同的两个数,剩下序列的主元素和原序列的主元素相同。

具体算法操作:记录两个量,当前元素x,计数cnt。初始化cnt为0;然后遍历序列,若cnt为0,则将x设为当前元素并将cnt置为1,否则,若当前元素和x相同,那么cnt++,若当前元素和x不同,那么cnt--;遍历结束以后,x即为主元素。

代码实现如下:

 1 class Solution {
 2 public:
 3     /**
 4      * @param nums: A list of integers
 5      * @return: The majority number
 6      */
 7     int majorityNumber(vector<int> v) {
 8         // write your code here
 9         int x, cnt = 0;
10         for(int i=0; i!=v.size(); ++i)
11         {
12             if(cnt==0)    x = v[i], cnt = 1;
13             else    v[i]==x?++cnt:--cnt;
14         }
15         return x;
16     }
17 };
时间: 2024-10-22 04:28:32

[LintCode] Majority Number(以时间复杂度O(n)求主元素)的相关文章

[LintCode] Majority Number 求众数

Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Notice You may assume that the array is non-empty and the majority number always exist in the array. Have you met this questio

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra

Lintcode: Majority Number 解题报告

Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example For [1, 1, 1, 1, 2, 2, 2], return 1 Challenge O(

[LintCode] Median(期望时间复杂度O(n)求中位数)

1 class Solution { 2 public: 3 /** 4 * @param nums: A list of integers. 5 * @return: An integer denotes the middle number of the array. 6 */ 7 void swap(vector<int> &v, int i, int j) 8 { 9 int tmp = v[i]; 10 v[i] = v[j]; 11 v[j] = tmp; 12 } 13 i

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. 求主元素:包含n个元素的数组中,如果一个元素的出现次数大

LeetCode OJ:Majority Element II(主元素II)

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 求主元素,这次的是大于sz/3就算是主元素,可以分为两轮查找,第一轮先查找元素数目较多的两个元素(可能不属于主元素),第二次再遍历来查找上面两个元素是否符合条件,代码如下: 1 class Solutio

lintcode 主元素:majority number III主元素III

题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 给出数组 [3,1,2,3,2,3,3,4,4,4] ,和 k = 3,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O(k) 解题 上一题刚介绍过所用的方法,但是这个确实很复杂的 先利用HashMap实现 public class Solution { /** * @param nums: A list of integers * @param k

【Lintcode】046.Majority Number

题目: Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Notice You may assume that the array is non-empty and the majority number always exist in the array. Have you met this que

Majority Number II

题目描述 链接地址 解法 算法解释 题目描述 Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Example Given [1, 2, 1, 2, 1, 3, 3], return 1. Note There is only one majority number in the array. Challenge O(n