【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 ...
随机推荐
- go搭建beego框架 go安装bee报错 github.com/derekparker/delve@v1.4.0: parsing go.mod:
go搭建beego框架 go安装bee报错 go使用beego框架的时候,需要安装bee,安装的时候遇到一些坑报错,这里跟大家分享一下,有助于快速安装bee,搭建好beego环境 1. 首先切换到go ...
- 最短剩余时间优先法则SRTN
- Google服务器架构图解简析
无疑是互联网时代最闪亮的明星.截止到今天为止,Google美国主站在Alexa排名已经连续3年第一,Alexa Top100中,各国的Google分站竟然霸占了超过20多个名额,不得不令人感叹Goog ...
- KEPServeEX 6与KepOPC中间件测试
KEPServeEX 6可以组态服务器端和客户端连接很多PLC以及具有OPC服务器的设备,以下使用KEPServeEX 6建立一个OPC UA服务器,然后使用KepOPC建立客户端来连接服务器做测试. ...
- C#时间选择
<script type="text/javascript" src="http://www.shicishu.com/down/WdatePicker.js&qu ...
- Shell学习(八)——dd命令
一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ...
- Virtual Destructor
Deleting a derived class object using a pointer to a base class that has a non-virtual destructor re ...
- ES安装简记
JDK # java -versionjava version "1.8.0_231"Java(TM) SE Runtime Environment (build 1.8.0_23 ...
- Spring Cloud服务离线
服务离线,即某服务不能对外提供服务了.服务离线的原因有两种:服务下架与服务下线.这两种方案都是基于Actuator监控器实现的. 服务下架:将注册到Eureka Server中的Eureka Clie ...
- Maven的聚合工程(多模块工程)
在开发2个以上模块的时候,每个模块都是一个 Maven Project.比如搜索平台,学习平台,考试平台.开发的时候可以自己管自己独立编译,测试,运行.但如果想要将他们整合起来,我们就需要一个聚合工程 ...