【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 ...
随机推荐
- 毕业设计之zabbix之nginx状态监控
监控脚本: [root@webone.quan.bbs ~]$vim /usr/local/zabbix/script/ngx_status.sh #!/bin/bash##************* ...
- Python——MacBook Pro中安装pip
1.系统已有python2和python3,如何检查MacBook Pro系统是否安装的有pip? 看到terminal的提示没有,有提示pip的,下面的提示,说明pip安装了. 要查看pip3是否安 ...
- R绘图布局包 customLayout
今天介绍一个R画图布局的包,地址如下: https://github.com/zzawadz/customLayout https://www.customlayout.zstat.pl/index. ...
- Notepad++—英文版卡框架翻译
用到了,就积累到这里,不急一时,慢慢沉淀. 一.File 二.Edit 三.Search 四.View视图 Always on top #总在最前 Toggle full screen mode ...
- javaWeb - 3 — JSP (技术已淘汰)— 更新完毕
1.jsp 在servlet中说过java中前端到后台有两条路线嘛 后台 <------ ajax.json <--------- 前端 后台 <------ jsp( EL.JST ...
- abandon, aboard, abolish
abandon Abandon is a 2002 American psychological thriller drama film [惊悚片] ... Waiting for Handler o ...
- Tomcat中的Server.xml配置详解
Tomcat中的Server.xml配置详解 Tomcat Server的结构图如下: 该文件描述了如何启动Tomcat Server <Server> <Listener /> ...
- JavaIO——System对IO的支持、序列化
1.系统类对IO的支持 在我们学习PriteWriter.PrintStream里面的方法print.println的时候是否观察到其与我们之前一直使用的系统输出很相似呢?其实我们使用的系统输出就是采 ...
- pop回指定控制器
//OCNSArray *array = [NSMutableArray new]; array = self.navigationController.viewControllers; //1.返回 ...
- iOS-调用系统的短信和发送邮件功能,实现短信分享和邮件分享
一.邮件分享 1.iOS系统自带邮件设置邮箱(此处以QQ邮箱为例)(http://jingyan.baidu.com/album/6181c3e084cb7d152ef153b5.html?picin ...