题意:

  如果所给序列的元素不是唯一的,则返回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 (判断重复元素)的更多相关文章

  1. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  2. LeetCode 217:存在重复元素 Contains Duplicate

    题目: 给定一个整数数组,判断是否存在重复元素. Given an array of integers, find if the array contains any duplicates. 如果任何 ...

  3. 力扣(LeetCode) 217. 存在重复元素

    给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true ...

  4. 【LeetCode】217.存在重复元素

    217. 存在重复元素 知识点:数组:Set: 题目描述 给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 fals ...

  5. LeetCode Contains Duplicate II (判断重复元素)

    题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...

  6. [LeetCode] Contains Duplicate 包含重复值

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  7. 力扣(LeetCode)219. 存在重复元素 II

    给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示例 1: 输入: nums = ...

  8. 217. Contains Duplicate数组重复元素 123

    [抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...

  9. [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 ...

随机推荐

  1. 【BZOJ】【3052】【WC2013】糖果公园

    树分块 老早(大约一个月以前?)就听说这道神题了……orz rausen 一直拖到现在才做……发现还是不会呢= = 只好也去Orz了Hzwer和zky http://hzwer.com/5250.ht ...

  2. idea从vcs引入maven项目报错

    一.问题 用idea从cvs上check out的maven项目,打开后,发现依赖的jar包都有红色下划线.检查本地的maven库中有对应的包,那就是依赖有问题,idea没有在本地找到对应的包. 二. ...

  3. C#枚举注释实例

    public enum 枚举名称    {        /// <summary>        /// 注释描述1        /// </summary>        ...

  4. PrintQueue

    PrintQueueCollection printQueues = null; var printServer = new PrintServer(); printQueues = printSer ...

  5. PAT-乙级-1049. 数列的片段和(20)

    1049. 数列的片段和(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 给定一个正数数列,我们可以从中截 ...

  6. Using command-line Subversion to access project source files

    Help index About source code version control with Software Configuration Management (Subversion) Usi ...

  7. mac上eclipse上配置hadoop

    在mac上安装了eclipse之后,配置hadoop其实跟在linux上配置差不多,只是mac上得eclipse和界面和linux上得有点不同. 一:安装eclipse eclipse得安装比较简单, ...

  8. 前台将勾选的多个属性放到一个value里面,是一个字符串,传到后台

    jq function changeStreet(a){ var valk=$(a).html(); $(a).parents(".select_box").children(&q ...

  9. 用JUnit4进行单元测试

    转载:http://tonl.iteye.com/blog/1948869 参考: http://thihy.iteye.com/blog/1771826 http://developer.51cto ...

  10. kmalloc/kfree,vmalloc/vfree函数用法和区别

    http://blog.csdn.net/tigerjibo/article/details/6412881 kmalloc/kfree,vmalloc/vfree函数用法和区别 1.kmalloc ...