原题链接在这里: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.

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. 1 <= nums.length <= 1000
  2. 1 <= nums[i] <= 10^9
  3. 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;
}
}

类似Majority Element.

LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array的更多相关文章

  1. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  2. 【LEETCODE】35、169题, Majority Element

    package y2019.Algorithm.array; import java.util.HashMap; import java.util.Map; /** * @ProjectName: c ...

  3. leetcode解题报告(21):Majority Element

    描述 Given an array of size n, find the majority element. The majority element is the element that app ...

  4. C#LeetCode刷题之#169-求众数(Majority Element)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4048 访问. 给定一个大小为 n 的数组,找到其中的众数.众数是 ...

  5. [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素

    Given a sorted array consisting of only integers where every element appears twice except for one el ...

  6. 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...

  7. 【leetcode】540. Single Element in a Sorted Array

    题目如下: 解题思路:题目要求时间复杂度是O(logN),可以尝试使用二分查找法.首先数组是有序的,而且仅有一个元素出现一次,其余均为两次.我们可以先找到数组最中间的元素,记为mid.如果mid和mi ...

  8. 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 ...

  9. LeetCode——Single Element in a Sorted Array

    Question Given a sorted array consisting of only integers where every element appears twice except f ...

随机推荐

  1. vue 学习记录

    模板:通常是指html模板 组件component的概念: 在 Vue 里,一个组件本质上是一个拥有预定义选项的一个 Vue 实例, 将组件看作自定义的HTML元素.使用组件的前提是创建并注册组件 v ...

  2. Unity C# File类 本地数据保存和游戏存档

    进行本地数据存档和载入在游戏开发中非常常见,几乎任何一款游戏都需要这样的功能. 命名空间: using System.IO; 主要用于引入File类以处理各类文件操作. using System.Ru ...

  3. HTTP之缓存命中

    缓存命中和缓存未命中 ========================摘自<HTTP权威指南>============================== 1.缓存命中和缓存未命中 可以用 ...

  4. mvn手动上传jar到本地仓库

    mvn install:install-file -Dfile=G:\elastic-project\workspace\out\artifacts\xxl_job_core_jar\xxl-job- ...

  5. Music模块

    micro:bit中,MicroPython提供一个Music模块,提供播放音乐的方法,但值得注意的是,控制板上并没有蜂鸣器,所以要外接蜂鸣器,才能听到效果 ,一般接到pin0端口,和gnd地,rob ...

  6. Cookie,Session,Token and Oauth

    Cookie 服务器端生成,发送给客户端,保存用户信息.下一次请求同一网站时会把该cookie发送给服务器. 应用:登录表单自动填充,同样 随着交互式Web应用的兴起,像在线购物网站,需要登录的网站等 ...

  7. Fiddler抓包工具怎么设置HTTPS抓包

    最近在学习使用 Fiddler 抓包工具时遇到一个问题,Fiddler 默认只对 HTTP 协议进行抓包,如果出现下图提示,则需要进行相应设置才可以抓包HTTPS 具体步骤 ①:Tools-Optio ...

  8. WPF ResourceDictionary XAML资源 c#代码 获取与遍历

    使用C#代码来获取XAML资源,除去正常的FindResource.而且是能查询到资源的对象. 说实话还是很麻烦的. 比如说我现在有一堆静态资源放在xaml的资源中,我想通过绑定的方式来获取. 好比是 ...

  9. mysqlbinlog恢复误删数据

    概述 代码bug,在处理上传出现异常时执行了DELETE FROM t_resource WHERE resource_id = ? OR parent_id = ?因为OR条件导致用户的上传的所有数 ...

  10. .Net IOC框架入门之——Unity

    一.概述 IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection). 作用:将各层的对象以松耦合的方式组织在一 ...