C语言 | Leetcode C语言题解之第169题多数元素
题目:
题解:
int majorityElement(int* nums, int numsSize) {int ans = 0;for (int i = 0, cnts = 0; i < numsSize; i++) {if (nums[i] == ans) {cnts++;} else if (cnts == 0) {ans = nums[i];} else {cnts--;}}return ans;
}
题目:
题解:
int majorityElement(int* nums, int numsSize) {int ans = 0;for (int i = 0, cnts = 0; i < numsSize; i++) {if (nums[i] == ans) {cnts++;} else if (cnts == 0) {ans = nums[i];} else {cnts--;}}return ans;
}