【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个嘛。因此,接着上一题的方法做,只不过这回要投两个票啦,而且最后还得检查这两个投票结果是不是真的满足都超过三分之一,因为这一题题目什么都没有保证,所以答案可能有0个、1个、2个。

C++:

 1 class Solution {
 2 public:
 3     vector<int> majorityElement(vector<int>& nums) {
 4         vector<int> ret;
 5
 6         int len = nums.size();
 7         if(len == 0)
 8             return ret;
 9
10         int m = 0, n = 0, cm = 0, cn = 0;
11         for(int i = 0; i < len; i++)
12         {
13             int val = nums[i];
14             if(m == val)
15                 cm++;
16             else if(n == val)
17                 cn++;
18             else if(cm == 0)
19             {
20                 m = val;
21                 cm = 1;
22             }
23             else if(cn == 0)
24             {
25                 n = val;
26                 cn = 1;
27             }
28             else
29             {
30                 cm--;
31                 cn--;
32             }
33         }
34
35         cm = cn = 0;
36
37         for(int i = 0; i < len; i++)
38         {
39             if(nums[i] == m)
40                 cm++;
41             else if(nums[i] == n)
42                 cn++;
43         }
44
45         if(cm * 3 > len)
46             ret.push_back(m);
47         if(cn * 3 > len)
48             ret.push_back(n);
49
50         return ret;
51     }
52 };

Python:

 1 class Solution:
 2     # @param {integer[]} nums
 3     # @return {integer[]}
 4     def majorityElement(self, nums):
 5         m, n, cm, cn = 0, 0, 0, 0
 6         ret = []
 7
 8         for val in nums:
 9             if m == val:
10                 cm = cm + 1
11             elif n == val:
12                 cn = cn + 1
13             elif cm == 0:
14                 m = val
15                 cm = 1
16             elif cn == 0:
17                 n = val
18                 cn = 1
19             else:
20                 cm = cm - 1
21                 cn = cn - 1
22
23         cm, cn = 0, 0
24
25         for val in nums:
26             if m == val:
27                 cm = cm + 1
28             elif n == val:
29                 cn = cn + 1
30
31         if cm * 3 > len(nums):
32             ret.append(m)
33         if cn * 3 > len(nums):
34             ret.append(n)
35
36         return ret
时间: 2024-10-25 17:51:15

【LeetCode 229】Majority Element II的相关文章

【LeetCode 169】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. 思路: 找到一个数组中出现次数超过一半的数.排序.哈希等

【LeetCode OJ】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. 1 int majorityElement(vec

【Leetcode 167】Two Sum II - Input array is sorted

问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, target=9Output: index1=1, index2=2 分析:在排序好的数组中进行查找,很容易想到用二分查找的思想.这里相当于是二分查找两个数,可以把最小值和最大值作为起点求和(sum). 若sum<target,则需要把较小元素也就是low处元素变大,此时不能直接把mid赋值给low,

【LeetCode OJ】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. 代码: class Solution { public: int removeElement(int A[]

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

【LeetCode 227】Basic Calculator II

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex

【LeetCode】229. Majority Element II

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. Hint: How many majority elements could it possibly have? Do you have a better hint

【leetcode 哈希表】Majority Element

[leetcode 哈希表]Majority Element @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 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 no

169. Majority Element &amp;&amp; 229. Majority Element II

169. 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. Hide T