217. 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.

题意:判断数组中是否有重复的元素,如果没有返回false,反之返回true.

解法:先对数组进行排序,然后比较排序之后数组相邻元素。

代码如下:

 class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int size = nums.size();
if(size == || size == )
{
return false;
}
sort(nums.begin(), nums.end());
for(int i = ; i < nums.size(); i++)
{
if(nums[i-] == nums[i])
{
return true;
}
}
return false;
}
};

leetcode 217的更多相关文章

  1. leetcode——217. 存在重复元素

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

  2. [LeetCode]-217.存在重复元素-简单

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

  3. 25. leetcode 217. Contains Duplicate

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  4. LeetCode 217. Contains Duplicate (包含重复项)

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

  5. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  6. LN : leetcode 217 Contains Duplicate

    lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...

  7. 2017-3-11 leetcode 217 219 228

    ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...

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

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

  9. Java实现 LeetCode 217 存在重复元素

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

随机推荐

  1. Python 类与作用域——一些测试

    /* 一 */ >>> class T (): a = 0 b = a >>> T.b 0 >>> del T /* 二 */ >>& ...

  2. 1.13 linux笔记

    free -m 看内存脚本 rpm -ivh 显示过程 rpm -U --upgraderpm -F --freshenrpm -e --eraserpm -e koren find / -name ...

  3. Java Concurrent之 AbstractQueuedSynchronizer

    ReentrantLock/CountDownLatch/Semaphore/FutureTask/ThreadPoolExecutor的源码中都会包含一个静态的内部类Sync,它继承了Abstrac ...

  4. C++引用详解

    引用的概念 引用:就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. 引用的声明方法:类型标识符 &引用名=目标变量名: 如下:定义引用ra,它是变量a的引用,即别名. i ...

  5. 从头开始db-oracle

    rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpmrpm -ivh http: ...

  6. netflix:Conductor微服务编排引擎

    项目地址: https://github.com/Netflix/conductor Conductor 是 Netflix 受需要运行全球流媒体业务流程的启发,构建的基于云的微服务编排引擎. Con ...

  7. 一条SQL查询多个统计结果

    例如以下情况,假如字段3是日期类型,按照小时分组统计字段1为空的个数,并对字段2大于5的值求和: SELECT SUM(CASE WHEN field1 IS NULL THEN 1 ELSE 0 E ...

  8. 处理大并发之五 使用libevent利器bufferevent

    转自:http://blog.csdn.net/feitianxuxue/article/details/9386843 处理大并发之五 使用libevent利器bufferevent 首先来翻译一段 ...

  9. 为docker配置固定ip

    docker默认使用bridge模式,通过网桥连接到宿主机,而容器内部的ip则从网桥所在的ip段取未用的ip.这样做一个不方便的地方在于容器内部的ip不是固定的,想要连接容器时只能通过映射到宿主机的端 ...

  10. Mac iTerm with Powerline

    1. 下载iTerm 地址: http://www.iterm2.com/ 完全可以取代Mac自带的终端了. 2. 之前我装过oh-my-zsh git clone git://github.com/ ...