HTMLify
major_element2.c
Views: 1 | Author: cody
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 | #include <stdio.h> #include <stdlib.h> #include <limits.h> int* majorityElement(int* nums, int numsSize, int* returnSize); int main() { int nums[] = {1, 1, 1, 2, 2, 3, 3}; int numsSize = sizeof(nums) / sizeof(nums[0]); int i,returnSize = 0; int* result = majorityElement(nums, numsSize, &returnSize); printf("Majority Elements: "); for (i = 0; i < returnSize; i++) { printf("%d ", result[i]); } printf("\n"); free(result); return 0; } int* majorityElement(int* nums, int numsSize, int* returnSize) { int* result = (int*)malloc(numsSize * sizeof(int)); int el1 = INT_MIN, el2 = INT_MIN, i, k = 0, check = 0; int count1 = 0, count2 = 0; int counts1, counts2; for (i = 0; i < numsSize; i++) { if (count1 == 0 && nums[i] != el2) { el1 = nums[i]; count1++; } else if (count2 == 0 && nums[i] != el1) { el2 = nums[i]; count2++; } else if (nums[i] == el1) { count1++; } else if (nums[i] == el2) { count2++; } else { count1--; count2--; } } counts1 = 0; counts2 = 0; for (i = 0; i < numsSize; i++) { if (el1 == nums[i]) { counts1++; } else if (el2 == nums[i]) { counts2++; } } if (counts1 > (numsSize / 3) && counts2 > (numsSize / 3)) { check++; result[k] = el1; k++; check++; result[k] = el2; k++; } else if (counts1 > (numsSize / 3)) { check++; result[k] = el1; k++; } else if (counts2 > (numsSize / 3)) { check++; result[k] = el2; k++; } *returnSize = check; return result; } |