【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 question in a real interview?

Yes

Example

Given [1, 1, 1, 1, 2, 2, 2], return 1

题解:

  摩尔投票法

Solution 1 ()

class Solution {
public:
    int majorityNumber(vector<int> nums) {
        int cnt = 0;
        int res = nums[0];
        for(auto n : nums) {
            if (n == res) {
                cnt++;
                continue;
            } else {
                if(--cnt <= 0) {
                    cnt = 1;
                    res = n;
                }
            }
        }
        return res;
    }
};
时间: 2024-08-08 01:28:55

【Lintcode】046.Majority Number的相关文章

【HDU1394】Minimum Inversion Number(线段树)

大意:n次操作原串查询逆序数,求出所有串中最小的逆序数. 求逆序数属于线段树的统计问题,建立空树,每次进行插点时进行一次query操作即可.n次操作可以套用结论:如果是0到n的排列,那么如果把第一个数放到最后,对于这个数列,逆序数是减少a[i],而增加n-1-a[i]. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int maxn = 5e3 + 10; 6 int sumv[

【转】oracle数据库NUMBER数据类型

原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定precision的话,Oracle将使用38作为精度.b)  如果scale大于零,表示数字精确到小数点右边的位数:scale默认设置为0:如果scale小于零,Oracle将把该数字取舍到小数点左边的指定位数.c)  Precision的取值范围为[1---38]:Scale的取值范围为[-84---1

【POJ2699】The Maximum Number of Strong Kings

Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a tournament T, the score of x is the number of playe

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

题目地址: https://oj.leetcode.com/problems/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

【Lintcode】153.Combination Sum II

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Example Given candidate set [10,1,6,7,2,1,5] 

【Lintcode】062.Search in Rotated Sorted Array

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

【Lintcode】003.Digit Counts

题目: Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we have FIVE 1's (1, 10, 11, 12) 题解: Solution 1 () class Solution { public: int digitCounts(int k, int n) { if (n < 0)

【Lintcode】135.Combination Sum

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Example Given candidate set [2,3,6,7]