Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Have you met this question in a real interview?
 
思路:
1,哈希法:用一个set记录所有出现过的数字,若有冲突,则含有重复元素;若没有冲突,则不含有重复元素。此方法时间复杂度为O(n),空间复杂度为O(n)
 class Solution
{
public:
bool containsDuplicate(vector<int> &nums)
{
set<int> S;
for(int i=; i<nums.size(); i++)
{
if(S.find(nums[i]) == S.end())
S.insert(nums[i]);
else
return true;
}
return false;
}
};
2,排序法:先将该数组排序,然后从i=1开始,num[i-1]与nun[i]两两比较, 若存在相等的情况,则含有重复元素,否则不含有重复元素,此方法时间复杂度为O(nlogn), 空间复杂度为O(1)。
 class Solution
{
public:
bool containsDuplicate(vector<int> &nums)
{
sort(nums.begin(), nums.end());
for(int i=; i<nums.size(); i++)
if(nums[i-] == nums[i])
return true; return false;
}
};

[leetcode] Contains Duplicate的更多相关文章

  1. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  2. [LeetCode] Contains Duplicate III 包含重复值之三

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  3. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

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

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

  5. [LeetCode] Delete Duplicate Emails 删除重复邮箱

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  6. LeetCode Remove Duplicate Letters

    原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...

  7. [LeetCode] Find Duplicate Subtrees 寻找重复树

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  8. [LeetCode] Find Duplicate File in System 在系统中寻找重复文件

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  9. LeetCode Find Duplicate File in System

    原题链接在这里:https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目: Given a list of ...

  10. [Leetcode] Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

随机推荐

  1. Netty5 + Protobuf 使用

    1. 安装开发环境 1.1 Netty环境 这里我使用Netty5.0.0版本 到这里下载即可http://netty.io/ 下载netty-all-5.0.0.Alpha2.jar 这个jar包简 ...

  2. 解决mysql“Access denied for user 'root'@'localhost'”

    解决mysql“Access denied for user 'root'@'localhost'” 分类: linux 2011-01-14 00:23 147547人阅读 评论(3) 收藏 举报 ...

  3. 快乐的JS正则表达式(三)

    ?的用途. 小任务:匹配一段网址如var str = "http://www.123.com/";注意http也可以是https var str = "http://i. ...

  4. iOS-App的启动页设置

    一. 要求 1. 把一张图片设置成为启动页面. 二. 准备工作 1. 把准备的适配的(@1x,@2x,@3x)三张图片拖进工程中. 2. 打开LaunchScreen.storyboard. 在页面上 ...

  5. [Python]爬虫v0.1

    #coding:utf-8 import urllib ###### #爬虫v0.1 利用urlib2 和 字符串内建函数 ###### # 获取网页内容 def getHtml(url): page ...

  6. Direct3D11学习:(六)渲染管线

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 这篇文章主要讲解渲染管线(rendering pipeline)的理论知识. 渲染管线是指:在给定一个3D场景的 ...

  7. HQueue:基于HBase的消息队列

    HQueue:基于HBase的消息队列   凌柏   ​1. HQueue简介 HQueue是一淘搜索网页抓取离线系统团队基于HBase开发的一套分布式.持久化消息队列.它利用HTable存储消息数据 ...

  8. Entity FrameWork 延迟加载本质(二)

    1.对于外键实体而言,EF会在用到这个外键属性的时候,才会去查对应的表.这就是按需加载了... 2.按需加载的缺点:每次调用外键实体的时候,都会去查询数据库(EF有小优化:相同的外键实体只查一次) I ...

  9. JS对象的创建与使用

    本文内容:     1.介绍对象的两种类型:     2.创建对象并添加成员:     3.访问对象属性:     4.利用for循环枚举对象的属性类型:     5.利用关键字delete删除对象成 ...

  10. div模拟实现select下拉框

    <!DOCTYPE html> <html> <head> <title></title> <style type="tex ...