题意:

  如果所给序列的元素不是唯一的,则返回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. .run文件安装

    比如realplay.run 安装方法如下 chmod +x realplay.run ./realplay.run 然后他就会执行安装了,在过程中可能会要求你输入yes或no 安装完后就可以用了

  2. POJ 1456 Supermarket(贪心+并查集优化)

    一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2  10 1   20 2   10 1    50+20 50 2  40 ...

  3. oracle实现自增列

    手动创建了一个表格,但是id字段无法实现自增,查看了一下网上的信息,没有找到满意的答案.一下是自己总结摸索的,仅供参考 第一步:手动创建表和列中的字段 (本例中,表明 T_VIDEO,第一个字段:ID ...

  4. SQO2008配置管理工具服务显示远程过程调用失败

    前两天,装了VS2012后,打开SQL2008配置管理工具,发现SQL服务名称里什么也没有,只有一个提示:(如图) 卸载了一个叫"Microsoft SQL Server 2012Local ...

  5. 欧拉工程第63题:Powerful digit counts

    题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...

  6. 深入浅出Mybatis-分页

    http://blog.csdn.net/hupanfeng/article/details/9265341 http://blog.csdn.net/isea533/article/details/ ...

  7. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

  8. linux 显示当前用户信息

    1.w命令 2.who命令 3.who am i 4. users

  9. 【最新】最流行的java后台框架 springmvc mybaits 集代码生成器 SSM SSH

        获取[下载地址]   QQ: 313596790   [免费支持更新] A 代码生成器(开发利器);全部是源码     增删改查的处理类,service层,mybatis的xml,SQL( m ...

  10. Intellij IDEA 快速创建Spring Web 项目

    相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...