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.

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:

 1 class Solution {
 2     public boolean isMajorityElement(int[] nums, int target) {
 3         if(nums == null || nums.length == 0){
 4             return false;
 5         }
 6
 7         int n = nums.length;
 8         int l = 0;
 9         int r = n - 1;
10         while(l < r){
11             int mid = l + (r - l) / 2;
12             if(nums[mid] < target){
13                 l = mid + 1;
14             }else{
15                 r = mid;
16             }
17         }
18
19         return l + n / 2 < n && nums[l + n / 2] == target;
20     }
21 }

类似Majority Element.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12076239.html

时间: 2024-11-01 21:09:38

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

【LeetCode从零单排】No.169 Majority Element(hashmap用法)

题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits:Special thanks to

【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)

leetcode上有四道关于移除有序序列中重复数字的题目,其中两道为数组结构,两道为链表结构,分别为: (1)Remove Duplicates from sorted array I:移除一个有序数组中的重复数字,并且返回新数组的大小. (2)Remove Duplicates from sorted array II:移除一个有序数组中的重复数字,并且返回新数组的大小,和上道题目不同的是每个数字可以最多重复两次出现. (3)Remove Duplicates from sorted list

540. Single Element in a Sorted Array(LeetCode)

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

LeetCode.540.Single Element in a Sorted Array

恩, 沙比提 1 class Solution(object): 2 def singleNonDuplicate(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 xorsum = 0 8 for i in nums: 9 xorsum = xorsum ^ i 10 return xorsum 11

leetcode 6. 在有序数组旋转后搜索 Search in Rotated Sorted Array

Search in Rotated Sorted Array 难度:Hard Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherw

【leetcode 哈希表】Majority Element

[leetcode 哈希表]Majority Element @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is no

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

Leetcode[154]-Find Minimum in Rotated Sorted Array II

Link: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is

169.Majority Element (法1排序法2多数投票)

Given an array of size n, find the majority element. Themajority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element alwaysexist in the array. Credits: Special thanks to @ts