LeeCode-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(int* nums, int numsSize)
 2 {
 3     if(numsSize==1)
 4         return nums[0];
 5
 6
 7     int i,j,k;
 8     for(i=1;i<numsSize;i++)
 9     {
10         for(j=i-1;j>=0;j--)
11         {
12             if(nums[i]>=nums[j])
13                 break;
14         }
15
16         if(i!=j-1)
17         {
18             int temp=nums[i];
19             for(k=i-1;k>j;k--)
20             {
21                 nums[k+1]=nums[k];
22             }
23             nums[k+1]=temp;
24         }
25     }
26
27     int Time;
28     Time=numsSize/2;
29     int count=1;
30     for(i=0;i<numsSize-1;i++)
31     {
32         if(nums[i]==nums[i+1])
33         {
34             count++;
35             if(count>Time)
36             {
37                 return nums[i];
38             }
39         }
40         else
41         {
42             count=1;
43         }
44     }
45
46
47     return 0;
48 }
时间: 2024-10-26 22:17:44

LeeCode-Majority Element的相关文章

LeetCode(7): Majority Element

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]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. Credits: Special thanks to @

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

题目: 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. 题解:运用多数投票算法的思路来解:从头到尾遍历数

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. 哈希实现 通过map计数即可 1 class Solut

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 &quot;Majority Element II&quot;

Similar as "Majority Element". There are at most 2 such elements, for > floor(n/3), and every non-hit element will decrease count of EACH hit element. https://leetcode.com/discuss/42769/o-n-time-and-in-o-1-space-c-solution

LeetCode——Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element.但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):1.Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相

169.Majority Element (法1排序法2多数投票)

Given an array of size n, find the majority element. Themajority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element alwaysexist in the array. Credits: Special thanks to @ts

【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