-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityElement.c
More file actions
88 lines (74 loc) · 1.53 KB
/
MajorityElement.c
File metadata and controls
88 lines (74 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
问题描述:
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.
Example 1:
Input: [3,2,3]
Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2
解题思路:
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
void swap(int* x,int* y){
int temp=*x;
*x=*y;
*y=temp;
}
void quickSort(int* arr,int low,int high){
int x=low;
int y=high;
x++;
while(x<=y){
if(arr[x]>arr[low] && arr[y]<arr[low]){
swap(&arr[x],&arr[y]);
x++;
y--;
}
if(arr[x]<=arr[low]){
x++;
}
if(arr[y]>=arr[low]){
y--;
}
}
swap(&arr[low],&arr[y]);
if(y-1>low){
quickSort(arr,low,y-1);
}
if(high>x){
quickSort(arr,x,high);
}
}
int majorityElement(int* nums, int numsSize){
quickSort(nums,0,numsSize-1);
return nums[numsSize/2];
}
int majorityElement(int* nums, int numsSize){
int current;
int count=0;
for(int i=1;i<numsSize;i++){
if(count=0){
current=nums[i];
count++;
}else{
if(nums[i]==current){
count++;
}else{
count--;
}
}
}
return current;
}
int main(){
int m;
for(int i=0;i<10;i++){
printf("the n = %d : %d \n",i,m);
}
return 0;
}