题目如下:

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

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

  1. 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-so ...

  2. 【LeetCode】421. Maximum XOR of Two Numbers in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 依次遍历每一位 前缀树 日期 题目地址:https://lee ...

  3. 【leetcode】Majority Element

    题目概述: Given an array of size n, find the majority element. The majority element is the element that ...

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

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. 【leetcode】Remove Element

    题目概述: Given an array and a value, remove all instances of that value in place and return the new len ...

  6. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  7. 【leetcode】421. Maximum XOR of Two Numbers in an Array

    题目如下: 解题思路:本题的难点在于O(n)的复杂度.为了减少比较的次数,我们可以采用字典树保存输入数组中所有元素的二进制的字符串.接下来就是找出每个元素的异或的最大值,把需要找最大值的元素转成二进制 ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

随机推荐

  1. Linux 防火墙设置常用指令

    查看防火墙状态命令: service firewalld status systemctl status firewalld 结果: 其中:   enabled:开机启动(开机不启动是disabled ...

  2. SpringBoot起飞系列-自定义starter(十)

    一.前言 到现在,我们可以看出来,如果我们想用一些功能,基本上都是通过添加spring-boot-starter的方式来使用的,因为各种各样的功能都被封装成了starter,然后把相关服务注入到容器中 ...

  3. JS的精确简单的加减乘除

    简单的写法: <script> function decNum(a){/*获取小数位数*/ var r=0; a=a.toString(); if(a.indexOf(".&qu ...

  4. spring jpa 学习笔记(一) 之集成

    一.pom 配置 <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apa ...

  5. 26-Perl 包和模块

    1.Perl 包和模块Perl 中每个包有一个单独的符号表,定义语法为:package mypack;此语句定义一个名为 mypack 的包,在此后定义的所有变量和子程序的名字都存贮在该包关联的符号表 ...

  6. 23-Perl 面向对象

    1.Perl 面向对象Perl 中有两种不同地面向对象编程的实现:一是基于匿名哈希表的方式,每个对象实例的实质就是一个指向匿名哈希表的引用.在这个匿名哈希表中,存储来所有的实例属性.二是基于数组的方式 ...

  7. Django: ORM 数据库设置和读写分离

    一.Django的数据库配置 (一)修改settings.py文件关于数据库的配置: Django默认使用sqlite: # Django默认的数据库库,SQLit配置 DATABASES = { ' ...

  8. 服务端相关知识学习(一)之什么是zookeeper

    什么是zookeeper zookeeper是分布式协调服务,可以在分布式系统中共享配置.协调锁资源.提供命名服务那分布式协调服务又是个什么东西呢?首先我们来看“协调”是什么意思.在一个并发的环境里, ...

  9. 【Git的基本操作九】ssh免密登录

    SSH免密登录 1. 进入用户家目录 cd ~ 2. 删除原有的 .ssh 目录 rm -r .ssh 3. 运行命令生成 .ssh 目录 ssh-keygen -t rsa -C github或gi ...

  10. svn+jenkins自动部署

    需求:项目经理想要将原型图修改完后直接发布 前置条件: 已经有了svn服务器,并正常使用 已经有了jenkins服务器,之前搭建的gitlab+jenkins, 如需搭建jenkins,参考 http ...