【leetcode】961. N-Repeated Element in Size 2N Array
题目如下:
In a array
Aof size2N, there areN+1unique elements, and exactly one of these elements is repeated N times.Return the element repeated
Ntimes.Example 1:
Input: [1,2,3,3]
Output: 3Example 2:
Input: [2,1,2,5,3,2]
Output: 2Example 3:
Input: [5,1,5,2,5,3,5,4]
Output: 5Note:
4 <= A.length <= 100000 <= A[i] < 10000A.lengthis even
解题思路:送分题。因为题目没有要求不能用额外的内存,所以我的方法是用字典保存每个数字出现的次数,从而找到出现N的数字。
代码如下:
class Solution(object):
def repeatedNTimes(self, A):
"""
:type A: List[int]
:rtype: int
"""
dic = {}
res = 0
for i in A:
dic[i] = dic.setdefault(i,0) + 1
if dic[i] == len(A)/2:
res = i
break
return res
【leetcode】961. N-Repeated Element in Size 2N Array的更多相关文章
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 【Leetcode_easy】961. N-Repeated Element in Size 2N Array
problem 961. N-Repeated Element in Size 2N Array solution: class Solution { public: int repeatedNTim ...
- LeetCode--Sort Array By Parity && N-Repeated Element in Size 2N Array (Easy)
905. Sort Array By Parity (Easy)# Given an array A of non-negative integers, return an array consist ...
- 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- LC 961. N-Repeated Element in Size 2N Array【签到题】
In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
随机推荐
- Eigen ,MKL和 matlab 矩阵乘法速度比较
Eigen 矩阵乘法的速度 < MKL矩阵乘法的速度,MKL矩阵乘法的速度与matlab矩阵乘法的速度相差不大,但matlab GPU版本的矩阵乘法速度是CUP的两倍,在采用float数据类型 ...
- java基础学习笔记六(继承)
继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父 ...
- SEM推广引流效果的因素有哪些呢?
决定搜索引擎推广效果的基本就是流量,所引流过来的是有效流量还是无效流量,直接决定了推广的效果!那我们如何才能引流到最精准的流量把流量变现呢? 第一个就是关键词的匹配模式 "民营企业" ...
- python dir()详解
Python dir() 函数 Python 内置函数 描述 dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__ ...
- select into outfile的sql语句
SELECT INTO…OUTFILE语句把表数据导出到一个文本文件中,并用LOAD DATA …INFILE语句恢复数据.但是这种方法只能导出或导入数据的内容,不包括表的结构,如果 ...
- [CSP-S模拟测试]:计划(前缀和)
题目传送门(内部题32) 输入格式 第一行,三个正整数$n,m,q$.第二行,$n$个正整数$a_i$,保证$1\leqslant a_i\leqslant n$.接下来$q$行,每行两个正整数$k, ...
- Redis入门很简单之二【常见操作命令】
Redis入门很简单之二[常见操作命令] 博客分类: NoSQL/Redis/MongoDB redisnosql缓存 Redis提供了丰富的命令,允许我们连接客户端对其进行直接操作.这里简单介绍一 ...
- python基础-包和模块
Python基础-包与模块 写在前面 如非特别说明,下文均基于Python3 摘要 为重用以及更好的维护代码,Python使用了模块与包:一个Python文件就是一个模块,包是组织模块的特殊目录(包含 ...
- Ngrinder简单使用
文章目录 安装 试玩 性能测试 安装 https://github.com/naver/ngrinder/releases 下载对应版本,是一个war包,3.4以上支持jdk1.8 将war包放到to ...
- 【IO流】java中文件路径(相对路径、绝对路径)相关类及方法
原文链接:https://blog.csdn.net/Activity_Time/article/details/98034409 1. URL 菜鸟教程:Java URL处理 通常推荐对http等协 ...