【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 repeated N times.
Return the element repeated N
times.
Example 1:
Input: [1,2,3,3]
Output: 3
Example 2:
Input: [2,1,2,5,3,2]
Output: 2
Example 3:
Input: [5,1,5,2,5,3,5,4]
Output: 5
Note:
- 4 <= A.length <= 10000
- 0 <= A[i] < 10000
- A.length is even
题目大意
一个数组有2N个数字,其中有N+1个不同的数字。在这里边恰好有一个数字重复了N次,找出这个重复了N次的数字是什么。
解题方法
字典
只要是和次数有关的题目,可以直接使用字典解决。这个题直接统计每个数字出现的次数,然后把次数等于N的返回即可。
python代码如下:
class Solution(object):
def repeatedNTimes(self, A):
"""
:type A: List[int]
:rtype: int
"""
N = len(A) / 2
count = collections.Counter(A)
for k, v in count.items():
if v == N:
return k
return 0
C++代码如下:
class Solution {
public:
int repeatedNTimes(vector<int>& A) {
const int N = A.size() / 2;
unordered_map<int, int> m;
for (int a : A) {
m[a] ++;
}
for (auto x : m) {
if (x.second == N) {
return x.first;
}
}
return 0;
}
};
日期
2018 年 12 月 23 日 —— 周赛成绩新高
【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)的更多相关文章
- LeetCode 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 re ...
- 116th LeetCode Weekly Contest 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】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 【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】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- LeetCode 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】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 r ...
- 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 ...
随机推荐
- 2020终于解决Chrome浏览器“崩溃啦”的问题!
Google的chrome莫名其妙突然所有页面都显示"喔唷 崩溃啦",各种插件在右下角弹出报错!这个问题我之前遇到过一次,后来通过改快捷方式的名字解决了.可是这次,隔离回来上班,打 ...
- Linux—yum安装python-pip
centos下安装pip时失败: [root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, ...
- ansible-playbook 编译安装nginx
mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,default,meta} -pv └── nginx ├── ...
- MapReduce06 MapReduce工作机制
目录 5 MapReduce工作机制(重点) 5.1 MapTask工作机制 5.2 ReduceTask工作机制 5.3 ReduceTask并行度决定机制 手动设置ReduceTask数量 测试R ...
- 一个神奇的JS混淆,JSFuck!
JSFuck,整体由6个字符[, ], (, ), !, +组成,但却是可以正常运行的JS代码,JSFuck程序可以在任何Web浏览器或引擎中运行解释JavaScript! 看一段代码,源代码为:do ...
- Hadoop fs.copyToLocalFile错误
fs.copyToLocalFile(new Path("/study1/1.txt"), new Path("C:/Users/Administrator/Deskto ...
- RocketMQ集群搭建方式
各角色介绍 Producer:消息的发送者:举例:发信者 Consumer:消息接收者:举例:收信者 Broker:暂存和传输消息:举例:邮局 NameServer:管理Broker:举例:各个邮局的 ...
- 【并发编程】Java并发编程-看懂AQS的前世今生
在我们可以深入学习AbstractQueuedSynchronizer(AQS)之前,必须具备了volatile.CAS和模板方法设计模式的知识,本文主要想从AQS的产生背景.设计和结构.源代码实现及 ...
- ORACLE dba_objects
dba_objects OWNER 对象所有者 OBJECT_NAME 对象名称 SUBOBJECT_NAME 子对象名称 OBJECT_ID 对象id DATA_OBJECT_ID 包含该对象的se ...
- Virtual Destructor
Deleting a derived class object using a pointer to a base class that has a non-virtual destructor re ...