Skip to content

Commit 579cd4d

Browse files
majority element
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent 39f3486 commit 579cd4d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

169_majority_element/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test majority.c

169_majority_element/majority.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int majorityElement(int* nums, int numsSize)
5+
{
6+
int i, count = 1;
7+
int major = nums[0];
8+
for (i = 1; i < numsSize; i++) {
9+
if (count == 0) {
10+
major = nums[i];
11+
count++;
12+
} else if (major == nums[i]) {
13+
count++;
14+
} else {
15+
count--;
16+
}
17+
}
18+
19+
return major;
20+
}
21+
22+
int main(int argc, char **argv)
23+
{
24+
if (argc < 2) {
25+
fprintf(stderr, "Usage: ./test target n1 n2...\n");
26+
exit(-1);
27+
}
28+
29+
int i, count = argc - 1;
30+
int *nums = malloc(count * sizeof(int));
31+
for (i = 0; i < count; i++) {
32+
nums[i] = atoi(argv[i + 1]);
33+
}
34+
35+
printf("%d\n", majorityElement(nums, count));
36+
return 0;
37+
}

172_factorial_trailing_zeros/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test zeroes.c

172_factorial_trailing_zeros/zeroes.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int trailingZeroes(int n)
5+
{
6+
return n == 0 ? 0 : trailingZeroes(n / 5);
7+
}
8+
9+
int main(int argc, char **argv)
10+
{
11+
if (argc != 2) {
12+
fprintf(stderr, "Usage: ./test n\n");
13+
exit(-1);
14+
}
15+
16+
printf("%d\n", trailingZeroes(atoi(argv[1])));
17+
return 0;
18+
}

0 commit comments

Comments
 (0)