comments | difficulty | edit_url | rating | source | tags | ||
---|---|---|---|---|---|---|---|
true |
简单 |
1249 |
第 6 场双周赛 Q1 |
|
给出一个按 非递减 顺序排列的数组 nums
,和一个目标数值 target
。假如数组 nums
中绝大多数元素的数值都等于 target
,则返回 True
,否则请返回 False
。
所谓占绝大多数,是指在长度为 N
的数组中出现必须 超过 N/2
次。
示例 1:
输入:nums = [2,4,5,5,5,5,5,6,6], target = 5 输出:true 解释: 数字 5 出现了 5 次,而数组的长度为 9。 所以,5 在数组中占绝大多数,因为 5 次 > 9/2。
示例 2:
输入:nums = [10,100,101,101], target = 101 输出:false 解释: 数字 101 出现了 2 次,而数组的长度是 4。 所以,101 不是 数组占绝大多数的元素,因为 2 次 = 4/2。
提示:
1 <= nums.length <= 1000
1 <= nums[i] <= 10^9
1 <= target <= 10^9
我们注意到,数组
时间复杂度
class Solution:
def isMajorityElement(self, nums: List[int], target: int) -> bool:
left = bisect_left(nums, target)
right = bisect_right(nums, target)
return right - left > len(nums) // 2
class Solution {
public boolean isMajorityElement(int[] nums, int target) {
int left = search(nums, target);
int right = search(nums, target + 1);
return right - left > nums.length / 2;
}
private int search(int[] nums, int x) {
int left = 0, right = nums.length;
while (left < right) {
int mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
class Solution {
public:
bool isMajorityElement(vector<int>& nums, int target) {
auto left = lower_bound(nums.begin(), nums.end(), target);
auto right = upper_bound(nums.begin(), nums.end(), target);
return right - left > nums.size() / 2;
}
};
func isMajorityElement(nums []int, target int) bool {
left := sort.SearchInts(nums, target)
right := sort.SearchInts(nums, target+1)
return right-left > len(nums)/2
}
function isMajorityElement(nums: number[], target: number): boolean {
const search = (x: number) => {
let left = 0;
let right = nums.length;
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
const left = search(target);
const right = search(target + 1);
return right - left > nums.length >> 1;
}
方法一中,我们使用了两次二分查找,分别找到数组
时间复杂度
class Solution:
def isMajorityElement(self, nums: List[int], target: int) -> bool:
left = bisect_left(nums, target)
right = left + len(nums) // 2
return right < len(nums) and nums[right] == target
class Solution {
public boolean isMajorityElement(int[] nums, int target) {
int n = nums.length;
int left = search(nums, target);
int right = left + n / 2;
return right < n && nums[right] == target;
}
private int search(int[] nums, int x) {
int left = 0, right = nums.length;
while (left < right) {
int mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
class Solution {
public:
bool isMajorityElement(vector<int>& nums, int target) {
int n = nums.size();
int left = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
int right = left + n / 2;
return right < n && nums[right] == target;
}
};
func isMajorityElement(nums []int, target int) bool {
n := len(nums)
left := sort.SearchInts(nums, target)
right := left + n/2
return right < n && nums[right] == target
}
function isMajorityElement(nums: number[], target: number): boolean {
const search = (x: number) => {
let left = 0;
let right = n;
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
const n = nums.length;
const left = search(target);
const right = left + (n >> 1);
return right < n && nums[right] === target;
}