一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

解题思路:

最开始想到的解法时利用STL的set,遍历数组,如果在set中找到则返回true,如果set中没有就添加到set中,如果最后都没有返回,那就返回false。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        int len = nums.size();
        if(len ==0) return false;
        set<int> st;
        for(int i = 0 ; i < len ; i++){
            auto iter = st.find(nums[i]);
            if(iter!=st.end()){
                return true;
            }
            else {
                st.insert(nums[i]);
            }
        }
        return false;
    }
};

还有一种解法时首先对数组进行排序,然后遍历数组,如果出现与上一个数相等就返回true,如果遍历完数组都没有返回,就返回false。

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

【一天一道LeetCode】#217. Contains Duplicate的更多相关文章

  1. 25. leetcode 217. Contains Duplicate

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

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

  3. LN : leetcode 217 Contains Duplicate

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

  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 (包含重复项)

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

  6. leetcode 217 Contains Duplicate 数组中是否有重复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  7. leetcode 217 Contains Duplicate 数组中是否有反复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  8. Java for LeetCode 217 Contains Duplicate

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

  9. LeetCode 217 Contains Duplicate

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

  10. (easy)LeetCode 217.Contains Duplicate

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

随机推荐

  1. 使用C# (.NET Core) 实现状态设计模式 (State Pattern)

    本文的概念性内容来自深入浅出设计模式一书 项目需求 这是一个糖果机的需求图. 它有四种状态, 分别是图中的四个圆圈: No Quarter: 无硬币 Has Quater 有硬币 Gumball So ...

  2. C语言程序设计第四次作业-选择结构

    (一)改错题 输出三角形的面积和周长,输入三角形的三条边a.b.c,如果能构成一个三角形,输出面积area和周长perimeter(保留2位小数):否则,输出"These sides do ...

  3. jquery传值与判断

    js判断是否包含字符串 var str="Hello world!" var s = str.indexOf("Hello") 存在则s>-1不存在则是s ...

  4. request.url 端口 错误

    今天遇到一个很奇怪的事情,用request.url.port来获取一个请求的端口,返回是80 ,很纳闷啊我的请求上面是http://www.XX.com:8088 啊,怎么会是80啊,太不可思议了! ...

  5. Mysql根据一个基库生成其他库与其不同的库升级脚本

    今天研究了一下不同数据库之间如何做同步.弄了一个升级工具类,希望以后还能有所帮助. public class UpgradeDataBase { public static void main(Str ...

  6. c++ 文件操作详解

    C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifstream: 读操作(输入)的文件类(由istream引申而来) fstre ...

  7. 存出和载入Docker镜像

    存出镜像 如果要导出镜像到本地文件,可以使用 docker save 命令. $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL ...

  8. OpenCV 2.x/3.x 随机初始化矩阵

    简介 在测试算法的时候,或者某些算法需要使用随机数,本文介绍如何使用OpenCV的随机数相关功能. 主要内容: 1. cv::RNG类 -- random number generator 2. cv ...

  9. 让你的代码量减少3倍!使用kotlin开发Android(一)

    让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程 本文同步自博主的私人博客:wing的地方酒馆 写在前面 使用kotlin开发android已经两周多了.得到的好处 ...

  10. 异步请求引发的Chrome死锁

    浏览器支持的并发异步请求数目是有限的,当需要的资源过多时候(远远大于并发数目),就需要自己管理XHR请求. 在实现自己的XHR的Manger时候,当请求数目达到2000多的时候,经常会遇到chrome ...