LeetCode-Algorithms-[Easy]1150. 检查一个数是否在数组中占绝大多数
程序员文章站
2024-03-15 22:52:15
...
public boolean isMajorityElement(int[] nums, int target) {
int n = nums.length;
int i = 0, count = 0;
while (i < n && nums[i] != target) {
i++;
}
while (i < n && nums[i] == target) {
count++;
i++;
}
if (count > n / 2) {
return true;
}
return false;
}