class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
int count = 1;
for(int i = 1;i < nums.length;i++){
if(nums[i] == nums[i-1])
count++;
else
count = 1;
if(count > nums.length/2)
return nums[i];
}
return nums[0];
}
}
leetcode169. 多数元素
最新推荐文章于 2024-09-21 14:48:47 发布