【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: 6Constraints:
1 <= arr.length <= 10^40 <= 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的更多相关文章
- 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 ...
- 【LeetCode】421. Maximum XOR of Two Numbers in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 依次遍历每一位 前缀树 日期 题目地址:https://lee ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- 【leetcode】Majority Element (easy)(*^__^*)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【leetcode】Remove Element
题目概述: Given an array and a value, remove all instances of that value in place and return the new len ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【leetcode】421. Maximum XOR of Two Numbers in an Array
题目如下: 解题思路:本题的难点在于O(n)的复杂度.为了减少比较的次数,我们可以采用字典树保存输入数组中所有元素的二进制的字符串.接下来就是找出每个元素的异或的最大值,把需要找最大值的元素转成二进制 ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
随机推荐
- Oracle导入/导出某个用户下的数据库
导出 exp用户名/密码@数据库实例owner=用户名file=文件存储路径 例如:exp MM/123456@ORCL owner=MM file=F\abcd.dmp 导入 imp用户名/密码@数 ...
- *** 没有规则可以创建目标“test”。 停止。
在编译Linux模块时出现这个问题,在仔细检查了Makefile没有错误后,重名了了该源程序和Makefile所在文件夹的名字,与源程序名字一致,然后问题就消失了!它们的关联体现在哪啊!?
- markdown中使用缩进
在markdown中直接敲空格是不生效的. 使用html标签来实现 一个空格大小的表示: 两个空格的大小表示: 不换行空格: 别忘记分号 参考了大神的文章: markdown空格缩进以及HTML ...
- 10-Perl 循环
1.Perl 循环一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推.有的时候,可能需要多次执行同一块代码.编程语言提供了更为复杂执行路径的多种控制结构.循环语句允许 ...
- nnginx配置代理服务器
因为有些服务有ip白名单的限制,部署多节点后ip很容易就不够用了,所以可以将这些服务部署到其中的一些机器上, 并且部署代理服务器,然后其余机器以代理的方式访问服务.开始是以tinyproxy作为代理服 ...
- windows安装npm教程
1.在使用之前,先类掌握3个东西,明白它们是用来干什么的: npm: nodejs 下的包管理器. webpack: 它主要用途是通过CommonJS 的语法把所有浏览器端需要发布的静态资源作相应的 ...
- centos 配置mysql主从复制
mysql+centos7+主从复制 MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...
- 13 Django之中间件
一.什么是中间件 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不好会影 ...
- JS ES5
常用 严格模式 use strict 必须使用var声明变量 禁止自定义函数this指向window 'use strict' funcion Person(name){ this.name = na ...
- vue源码分析
1.new Vue的过程 1)首先Vue是一个类,初始化时已经添加很多 initMixin(Vue) 给原型添加Vue.prototype._init stateMixin( ...