Home > Back-end >  Leetcode 169
Leetcode 169

Time:09-23

Topic: https://leetcode-cn.com/problems/find-majority-element-lcci/


Method one: the main way for: to calculate in each element of an array number and put these into a new array inside; Then iterate through the array to find the maximum value, and record the subscript (subscript of an array of new array and the original one to one correspondence), the maximum value comparing with general array length, if greater than by recording the subscript indices array element, get the target element, but it will start to miss the array element is only one case, so need to be done in the first array element number, relatively large amount of calculation of the problem need to be optimized
The code is as follows;
The class Solution {
Public:
Int majorityElement (vector & Nums) {
int i=0;
Int result=0;
Int hang=(int) nums. The size () & gt;> 1;
Vector sum;
If (nums. The size ()==1) return nums [0].
The else
{
While (i{
Int num nums=[I];
int len=0;
For (int j=0; J & lt; Nums. The size (); J++) {
If (nums==[j] num) {
len++;

}
}
The sum. The push_back (len);
I++;
}
Int temp=0;
for (int i=0; i
If (temp & lt; The sum [I]) {
Temp=sum [I];
Result=I;
}

}
If (temp> Hang) return nums. [result];
The else return - 1;
}
}
};
Method 2: first to ascending sort an array, such elements equal row together, from 0
To traverse, if the current position of the element and its location half step length of the array elements are equal, it is the element we are looking for, traverse to the end of the half the length of the array,
The class Solution {
Public:
Int majorityElement (vector & Nums) {
Sort (nums. The begin (), nums. The end ());//order
Int len=nums. The size ();
for (int i=0; i
If (nums==nums [I] [I + len/2])
Return nums [I];

}
return -1;


}
};
Method three: Moore's law: the algorithm to solve the problem is how to in any number of candidates (votes disorder), won the most votes are selected, the common algorithm is to scan again, to statistics of the votes for each candidate, when the number of candidate fixed, the common algorithm's time complexity is: O (n) O (n) O (n), when the number of candidates do not timing, counting votes may perform a long time, may need to run a O (n ^ 2) time when votes and orderly, can easily make up themselves, and then check if the number of digits in the more than half of the votes, this paper in view of the chaotic and uncertain situation candidates, Moore voting algorithm is proposed, algorithm of most times is the double of votes (to n), can be in
O (n) O (n) O (n) time choose won the most votes, space overhead for the O (1) O (1) O (1),
Thinking: 1, the count=1; Num nums=[0]. Says from the vote,
2. Through the array, if the number appearing next the same as the num, count + 1, if different, the count minus 1,
3 if the count=0; Indicates the current traversal to different pairs of Numbers can add up to cancel each other out, greater than n/2 digital number can appear in the back, then this element starts from now, makes the count=1
int check;
int count=0;
For (int num nums) {
If (count==0)
{
Check=num;
Count=1;
}
The else {
If (num==check) {
count++;
}
The else count -;
}
}

count=0;
For (int num nums) {
If (num==check) {
count++;
}
}
If (count & lt;=nums. The size ()/2) {
return -1;
}
The else return check;

CodePudding user response:

Set the pile method, meet the o (n)
  • Related