【LeetCode】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/2 的数。

C++ 中map 对于这种统计次数的问题来说特别方便,而 Python 的话当然用字典。

代码:

C++

class Solution{
public:
    int majorityElement(vector<int> &num) {
        vector<int>::iterator it = num.begin();
        int n = num.size();
        map<int,int> count;
        for(;it != num.end();it++)
        {
            // 这里为什么不用判断键是否存在?因为不存在时初始化为 0
            if(++count[*it] > n/2)
            {
                return *it;
            }
        }
    }
};

Python:

class Solution:
    # @param num, a list of integers
    # @return an integer
    def majorityElement(self, num):
        count = dict([])
        flag = len(num)/2
        for item in num:
            if item in count:
                count[item] = count[item] + 1
            else:
                count[item] = 1

            if count[item] > flag:
                return item
时间: 2024-10-21 04:43:57

【LeetCode】Majority Element的相关文章

【leetcode】Majority Element (easy)(*^__^*)

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. 思路: 找主要元素,用major记录主要字母,n记录ma

【10_169】Majority Element

今天遇到的题都挺难的,不容易有会做的. 下面是代码,等明天看看Discuss里面有没有简单的方法~ Majority Element My Submissions Question Total Accepted: 79833 Total Submissions: 208075 Difficulty: Easy Given an array of size n, find the majority element. The majority element is the element that

【Leetcode】Major Element in JAVA

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. 题目不难,但是我这个方法太贱了,我做了一个O(n^2)的

【LeetCode】Remove Element

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 解法一:使用vector记录每个elem的位置,介于这些位置之间的元素就可以前移相应

【leetcode】Remove Element (easy)

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路: s记录下一个判断位置, e记录结束位置,把前面的待排除元素与后面要保留的元素互换. int removeE

【leetcode】1287. Element Appearing More Than 25% In Sorted Array

题目如下: Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Example 1: Input: arr = [1,2,2,6,6,6,6,7,10] Output: 6 Constraints: 1 <= arr.length <=

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】位运算 bit manipulation(共32题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [78]Subsets [136]Single Number [137]Single Number II [169]Majority Element [187]Repeated DNA Sequences [190]Reverse Bits [191]Number of 1 Bits [201]Bitwise AND of Numbers Range [231]Pow

【LeetCode 229】Majority Element 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. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方