【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/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
随机推荐
- webpack (1)
node_modules/.bin/webpack 用webpack 打包 因为我们没有全局安装webpack 所以要用到这条命令 创建html npm install --save-dev html ...
- centos 升级python
1.下载python3.6.1的包 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz 2.解压 tar -zxvf Pytho ...
- word--->pdf资料转载..
https://blog.csdn.net/dsn727455218/article/details/80667927
- C语言之——__attribute__
__attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关 . __ ...
- [BOOKS]Big Data: Principles and best practices of scalable realtime data systems
- shell 提取文件的某行,并在行尾添加字符
sed -n '100,200p' file.txt #提取文件file.txt第100行到第200行的内容 sed 's/$/test/' file.txt #在行尾添加字符串test,$代表行尾
- Ubuntu下Arm-Linux-GCC交叉编译环境的搭建
1.下载arm-linux-gcc-3.4.1.tar.bz2到临时的目录下. 2.解压 arm-linux-gcc-3.4.1.tar.bz2 #tar -jxvf arm-linux-gcc-3. ...
- 学习.net的步骤
第一步 学习HTML与CSS 这并不需要去学一大堆的诸如Dreamweaver,Firework之类的各种网页设计工具,关键是理解HTML网页嵌套的block结构与CSS的box模型.许多ASP.NE ...
- 【Java架构:基础技术】一篇文章搞掂:Spring Boot 官方文档解读
本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文内容大部分是翻译和总结官方文档,可以到https://docs.spring.io/spring-boot/docs查看(此地 ...
- 国际C 语言乱码大赛(IOCCC )
你也许听说过“国际C 语言乱码大赛(IOCCC )”,能获奖的人毫无疑问是世界顶级C 程序员.这是他们利用C 语言的特点极限挖掘的结果.下面这个例子就是网上广为流传的 一个经典作品:// 原始代码如下 ...