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 ...
随机推荐
- snowflake ID生成器
背景 Snowflake 是 Twitter 内部的一个 ID 生算法, 可以通过一些简单的规则保证在大规模分布式情况下生成唯一的 ID 号码. 其组成为: 第一个 bit 为未使用的符号位. 第二部 ...
- thinkphp中怎么使用phpmailer发送邮件
phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎么集成phpmailer到thinkphp框架了,有需要了解的朋友可参考. phpmailer发送邮件功能很强大,今天真 ...
- 浙大版《C语言程序设计(第3版)》题目集 --总结
浙大版<C语言程序设计(第3版)>题目集 此篇博客意义为总结pta上浙大版<C语言程序设计(第3版)>题目集所做题目的错误点,心得体会. 1.练习2-10 计算分段函数[1] ...
- Vue.js 源码分析(十) 基础篇 ref属性详解
ref 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素:如果用在子组件上,引用就指向组件实例,例如: ...
- go ---MQTT client
Paho GO Client 语言 GO 协议 EPL AND EDL 官网地址 http://www.eclipse.org/paho/ API类型 Asynchronous 描述 Paho ...
- php 获取一张图片所有点的颜色值,感觉不错转载学习
片段一 //similar_text($numStr, $val, $pre); //计算两个字符串的相似度 //print_r($pre); $imgPath = 'time.jpg'; $size ...
- gentelella 开源后台使用记录
前言 gentelella是一款开源后台,github地址是:https://github.com/ColorlibHQ/gentelella 使用 表单验证 parsley 验证 在form.htm ...
- git commit 统计
git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; lo ...
- 初探Tomcat的架构设计
Tomcat 作为 servlet 容器实现,它是基于 Java 语言开发的轻量级应用服务器.因为 Tomcat 作为应用服务器,它有着完全开源,轻量,性能稳定,部署成本低等优点,所以它成为目前 Ja ...
- python高级编程——入门语法(二)
闭包 概念:外函数outer定义一个变量a,又定义一个内函数inner,而这个内函数inner访问了外函数outer的变量a,如果需要改变外函数outer的变量a的值,则需要声明 nonlocal a ...