【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/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
随机推荐
- 使用 jQuery 实现当前页面高亮显示的通栏导航条
index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- CSIC_716_20191224【python基础结课作业--仿优酷练习】
需 求:********管理员界面******** 1 注册 2 登录 3 上传视频 4 删除视频 5 发布公告 ********普通用户界面******** 1 注册 2 登录 3 冲会员 4 查看 ...
- 如何查看Ubuntu版本
有时候需要查看一下系统安装的Ubuntu的版本,最简单的方式是输入lsb_release -a. whatis lsb_release输出:print distribution-specific in ...
- php number_format()函数 语法
php number_format()函数 语法 number_format()函数怎么用? php number_format()函数表示通过千位分组来格式化数字,语法是number_format( ...
- Spring框架-经典的案例和demo,一些可以直接用于生产,使用atomikos来处理多数据源的一致性事务等
Spring Examples Demo website:http://www.ityouknow.com/ 对Spring框架的学习,包括一些经典的案例和demo,一些可以直接用于生产. sprin ...
- centos7.5部署ELk
第1章 环境规划 1.1 ELK介绍 ELK是ElasticSerach.Logstash.Kibana三款产品名称的首字母集合,用于日志的搜集和搜索. Elasticsearc ...
- JMeter 阶梯式加压测试插件 Stepping Thread Group
在日常性能测试过程中,有时需要对被测对象不断的增加压力,直至达到某个值后,并持续运行一段时间.这里将借助jmeter插件模拟这种情况. 本文介绍在jmeter中,使用插件Stepping Thread ...
- 项目质量管理—七种基本质量工具
出处:PMBOK(第五版) P236 1.因果图,又称鱼骨图或石川图 用来追溯问题来源,回推到可行动的根本原因.(找根本原因) 2.流程图,也称过程图 用来显示在一个或多个输入转化成一个或多个输出的过 ...
- oracle服务端导出/导入方式expdp/impdp
1. expdp导出步骤 1.1 用sys管理员登录sqlplus [root@hxjk_test_mysql_redis_file oracle]# sqlplus SQL*Plus: Releas ...
- js匿名函数测试
js匿名函数测试 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...