【leetcode】1287. Element Appearing More Than 25% In Sorted Array

题目如下:

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time.

Return that integer.

Example 1:

Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6

Constraints:

  • 1 <= arr.length <= 10^4
  • 0 <= arr[i] <= 10^5

解题思路:最直接的方法是统计每个元素出现的次数。

代码如下:

class Solution(object):
    def findSpecialInteger(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        dic = {}
        res = 0
        for i in arr:
            dic[i] = dic.setdefault(i,0) + 1
            if dic[i] > len(arr)/4:
                res = i
                break
        return res

原文地址:https://www.cnblogs.com/seyjs/p/12041875.html

时间: 2024-11-08 03:34:01

【leetcode】1287. Element Appearing More Than 25% In Sorted Array的相关文章

1287. Element Appearing More Than 25% In Sorted Array

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Example 1: Input: arr = [1,2,2,6,6,6,6,7,10] Output: 6 Constraints: 1 <= arr.length <= 10^4

LeetCode 5126. 有序数组中出现次数超过25%的元素 Element Appearing More Than 25% In Sorted Array

地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/element-appearing-more-than-25-in-sorted-array/ 目描述给你一个非递减的 有序 整数数组,已知这个数组中恰好有一个整数,它的出现次数超过数组元素总数的 25%.请你找到并返回这个整数 示例: 输入:arr = [1,2,2,6,6,6,6,7,10] 输出:6 提示: 1 <= arr.length <= 10^4 0 &

【leetcode】Majority Element (easy)(*^__^*)

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. 思路: 找主要元素,用major记录主要字母,n记录ma

【LeetCode】Majority Element

题意: 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. 思路: 在数组中找出出现次数大于 n/2 的数.

【LeetCode】Remove Element

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 解法一:使用vector记录每个elem的位置,介于这些位置之间的元素就可以前移相应

【Leetcode】Major Element in JAVA

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. 题目不难,但是我这个方法太贱了,我做了一个O(n^2)的

【leetcode】Remove Element (easy)

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路: s记录下一个判断位置, e记录结束位置,把前面的待排除元素与后面要保留的元素互换. int removeE

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【LeetCode】Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A =