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 ...
随机推荐
- Shell脚本——显示系统上的登录用户数
写一个脚本showlogged.sh,其用法格式为: showlogged.sh -v -c -h|--help 其中,-h选项只能单独使用,用于显示帮助信息:-c选项时,显示当前系统上登录的所有用户 ...
- mybatis解决字段名和实体属性不相同
两种方法: 1.在xml文件里面使用别名 2.使用resultMap标签
- luogu P2495 [SDOI2011]消耗战 |虚树+LCA+dp
题目描述 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知 ...
- 03、装饰模式(Decorator)
一.概念: 动态的给一个对象添加一些额外的职责,就增加的功能来说,装饰模式比生成子类更为灵活.[DP] 二.通俗的理解: 装饰模式是利用其中的方法为来对对象进行包装的,这样每个包装对象的事项就和如何使 ...
- WPF 精修篇 调用Win32Api
原文:WPF 精修篇 调用Win32Api 栗子是 调用WIn32API 让窗口最前 后台代码 [DllImport("user32.dll")] private static e ...
- C语言语法教程-链表
链表是一群结构体(称为结点)通过指针连起来.这种结构体类型,比较特殊,叫自引用结构体类型.它有一个指针指向和和结构体一样的类型,其余是数据成员. 头指针指向第一结点,尾指针一定要用空表示,这叫有头有尾 ...
- asp.net webapi 随笔
第一次写博客,文笔有限,记录下学习的过程 话不多说,直接开干 首先用vs2017建立一个空网站项目,然后只勾选api 项目建立后,如下结构 其中WebApiConfig类配置了路由相关信息 publi ...
- asp.net core流式上传大文件
asp.net core流式上传大文件 首先需要明确一点就是使用流式上传和使用IFormFile在效率上没有太大的差异,IFormFile的缺点主要是客户端上传过来的文件首先会缓存在服务器内存中,任何 ...
- Mark: 如何用Haskell写一个简单的编译器
作者:aaaron7 链接:https://www.zhihu.com/question/36756224/answer/88530013 如果是用 Haskell 的话,三篇文章足矣. prereq ...
- WPF布局介绍(1)
开局一张图,内容全靠...,本系列的文章, 主要针对刚入门.亦或是从 winform/bs转过来的开发人员快速入门的指南, 相对于其它一些文章中会详细的从项目如何建立到其实现的原理及组成部分, 本系列 ...