半数的妙解
用了栈的思想
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int majorityElement(int* nums, int numsSize){
int stack = malloc(sizeof(int) * numsSize);
int top = -1;
int i ;
for(i = 0; i <numsSize; i++){
if(top == -1){
stack[++top] = nums[i];
}
else if(stack[top] == nums[i]){
stack[++top] = nums[i];
}
else{
top--;
}
}
return stack[0];
}
int main(){
int nums[] = {1,4,6,4,4};
printf("%d", majorityElement(nums,5));
return 0;
}
方法二:
candidate和account就可以。