LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array
原题链接在这里:https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/
题目:
Given an array nums
sorted in non-decreasing order, and a number target
, return True
if and only if target
is a majority element.
A majority element is an element that appears more than N/2
times in an array of length N
.
Example 1:
Input: nums = [2,4,5,5,5,5,5,6,6], target = 5
Output: true
Explanation:
The value 5 appears 5 times and the length of the array is 9.
Thus, 5 is a majority element because 5 > 9/2 is true.
Example 2:
Input: nums = [10,100,101,101], target = 101
Output: false
Explanation:
The value 101 appears 2 times and the length of the array is 4.
Thus, 101 is not a majority element because 2 > 4/2 is false.
Note:
1 <= nums.length <= 1000
1 <= nums[i] <= 10^9
1 <= target <= 10^9
题解:
Use binary search to find the first occurance of target. Make sure that first ovvurance index + n / 2 also points to target.
Time Complexity: O(logn).
Space: O(1).
AC Java:
class Solution {
public boolean isMajorityElement(int[] nums, int target) {
if(nums == null || nums.length == 0){
return false;
} int n = nums.length;
int l = 0;
int r = n - 1;
while(l < r){
int mid = l + (r - l) / 2;
if(nums[mid] < target){
l = mid + 1;
}else{
r = mid;
}
} return l + n / 2 < n && nums[l + n / 2] == target;
}
}
LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array的更多相关文章
- 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...
- 【LEETCODE】35、169题, Majority Element
package y2019.Algorithm.array; import java.util.HashMap; import java.util.Map; /** * @ProjectName: c ...
- leetcode解题报告(21):Majority Element
描述 Given an array of size n, find the majority element. The majority element is the element that app ...
- C#LeetCode刷题之#169-求众数(Majority Element)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4048 访问. 给定一个大小为 n 的数组,找到其中的众数.众数是 ...
- [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 【leetcode】540. Single Element in a Sorted Array
题目如下: 解题思路:题目要求时间复杂度是O(logN),可以尝试使用二分查找法.首先数组是有序的,而且仅有一个元素出现一次,其余均为两次.我们可以先找到数组最中间的元素,记为mid.如果mid和mi ...
- LeetCode - 540. Single Element in a Sorted Array
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- LeetCode——Single Element in a Sorted Array
Question Given a sorted array consisting of only integers where every element appears twice except f ...
随机推荐
- 动手学深度学习6-认识Fashion_MNIST图像数据集
获取数据集 读取小批量样本 小结 本节将使用torchvision包,它是服务于pytorch深度学习框架的,主要用来构建计算机视觉模型. torchvision主要由以下几个部分构成: torchv ...
- Java连载17-赋值类运算符&字符串的连接运算符
一.赋值运算符 1.赋值类运算符包括两种: (1)基本赋值运算符:= (2)扩展的赋值运算符: += -= *= /= &= 赋值类的运算符优先级:先执行等号右边的表 ...
- 明解C语言 入门篇 第七章答案
练习7-1 #include <stdio.h> int main() { int n; printf(,, ); //此行显示结果为 4 3 6 因为1的字节就是为4,而-1的字节也是4 ...
- Dictionary不可以迭代修改值
var buffer = new List<string>(showDict.Keys); foreach (var key in buffer) { if (showDict[key] ...
- Linux 安装Redis4.0.8【yum安装】
.下载yum源 yum install epel-release2.安装redisyum install redis3.启动redis # 启动redis service redis start # ...
- 关于预装操作系统的ThinkPad的分区建议
Think的个人电脑产品大部分预装有正版操作系统,当前新产品出厂时默认都是一个大分区“C”和一个恢复分区“Q”,很多用户都会要求客服人员提供分区服务,在这里我简单说一下关于分区的几点注意事项望各位参考 ...
- K8s 学习者绝对不能错过的最全知识图谱(内含 58个知识点链接)
作者 | 平名 阿里服务端开发技术专家 导读:Kubernetes 作为云原生时代的“操作系统”,熟悉和使用它是每名用户的必备技能.本篇文章概述了容器服务 Kubernetes 的知识图谱,部分内容参 ...
- Java : JavaWeb和Tomcat相关
部署:1.直接把项目移动到webapps文件夹下, 用文件夹名访问(如果ROOT文件夹可以直接访问)2.也可以把war包放到webapps文件夹下, tomcat自动解压,但是删除war包必须要停止t ...
- 【BZOJ5104】Fib数列(BSGS,二次剩余)
[BZOJ5104]Fib数列(BSGS,二次剩余) 题面 BZOJ 题解 首先求出斐波那契数列的通项: 令\(A=\frac{1+\sqrt 5}{2},B=\frac{1-\sqrt 5}{2}\ ...
- Linux查看进程启动时间和已持续时间
ps -eo pid,lstart,etime,cmd | grep zzlogic