LeetCode Contains Duplicate (判断重复元素)

题意:
如果所给序列的元素不是唯一的,则返回true,否则false。
思路:
哈希map解决。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int,int> mapp;
for(int i=; i<nums.size(); i++)
{
if(mapp[nums[i]]) return true;
else mapp[nums[i]]=;
}
return false;
}
};
AC代码
python3
直接排序,再比对相邻元素
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if nums==[]: return False
nums=sorted(nums)
i=1
while i<len(nums):
if nums[i-1]==nums[i]:
return True
i+=1
return False
AC代码
用set辅助
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(set(nums))<len(nums)
AC代码
LeetCode Contains Duplicate (判断重复元素)的更多相关文章
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode 217:存在重复元素 Contains Duplicate
题目: 给定一个整数数组,判断是否存在重复元素. Given an array of integers, find if the array contains any duplicates. 如果任何 ...
- 力扣(LeetCode) 217. 存在重复元素
给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true ...
- 【LeetCode】217.存在重复元素
217. 存在重复元素 知识点:数组:Set: 题目描述 给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 fals ...
- LeetCode Contains Duplicate II (判断重复元素)
题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...
- [LeetCode] Contains Duplicate 包含重复值
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 力扣(LeetCode)219. 存在重复元素 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示例 1: 输入: nums = ...
- 217. Contains Duplicate数组重复元素 123
[抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
随机推荐
- [转载]C# Double toString保留小数点方法
有时候double型数据需要toString(),但又想保留小数,当值为整数,比如3.00时tostring后会变为”3″,具体说明见下: 1 string str0 = i.ToString(&qu ...
- window.open被IE拦截的解决办法
由于在使用window.open时,在很多情况下,弹出的窗口会被浏览器阻止,但若是使用a链接target='_blank',则不会,基于这一特点,自己封装了一个open方法: function ope ...
- springMVC+ibatis数据持久化入门级学习例子
1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- Properties --- C++读配置信息的类(一)
http://blog.csdn.net/billow_zhang/article/details/4304980 在开发实践中,积累了一些通用的C++ 类库,在此写出来给大家分享.也希望能给出更好的 ...
- Elasticsearch 学习~
http://cloud.51cto.com/art/201505/476322.htmEs https://www.gitbook.com/book/asdgh000/mongodb-elastic ...
- POJ1258Agri-Net
http://poj.org/problem?id=1258 题意 : john当上了镇长,打算给每个农场都连接网络,需要用最小的成本连接其他所有农场,所以要找最少的纤维连在一起,任何两个农场之间的距 ...
- hdu 4187 Alphabet Soup
这题的主要就是找循环节数,这里用找字符串最小覆盖来实现,也就是n-next[n],证明在这http://blog.csdn.net/fjsd155/article/details/6866991 #i ...
- linux入门教程(二) 图形界面还是命令窗口
对于linux的应用,我想大多数都是用在服务器领域,对于服务器来讲真的没有必要跑一个图形界面.所以我们平时安装linux操作系统时往往是不安装图形界面的.说到这里也许你会有疑问,图形界面还能选择装或者 ...
- 李洪强iOS开发之【Objective-C】09-空指针和野指针
一.什么是空指针和野指针 1.空指针 1> 没有存储任何内存地址的指针就称为空指针(NULL指针) 2> 空指针就是被赋值为0的指针,在没有被具体初始化之前,其值为0. 下面两个都是空指针 ...
- React测试Mixin
1.test.jsx var randomNumberMixin = require("./randomNumberMixin.jsx"); describe("test ...