LeetCode217. Contains Duplicate

文章目录

一、题目

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Example 1:

Input: nums = [1,2,3,1]

Output: true

Example 2:

Input: nums = [1,2,3,4]

Output: false

Example 3:

Input: nums = [1,1,1,3,3,4,3,2,4,2]

Output: true

Constraints:

1 <= nums.length <= 105

-109 <= nums[i] <= 109

二、题解

cpp 复制代码
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int,int> map;
        for(auto x:nums){
            if(map[x] == 1) return true;
            map[x]++;
        }
        return false;
    }
};
相关推荐
不吃土豆的马铃薯12 小时前
网络 IO 核心(同步/异步)概念笔记
服务器·c语言·开发语言·网络·c++·笔记
张小凡vip12 小时前
python的__init__.py说明
开发语言·前端·python
努力努力再努力wz12 小时前
【Redis入门系列】:从 hashtable到 listpack:深入理解 Hash 底层编码、字段级过期、核心命令与缓存应用
开发语言·数据结构·数据库·c++·redis·算法·缓存
Zxc_12 小时前
模拟退火算法:从固体退火到Rastrigin与TSP,手写一个完整的退火求解器
算法
小黑随笔12 小时前
Python asyncio 模块学习总结:从“等着”到“切出去干点别的”
开发语言·python·学习
qq_2949405512 小时前
Python环境搭建
开发语言·python
zhaokuangkuang_12 小时前
Java学习
java·学习·算法
陈辛chenxin13 小时前
【数据挖掘01】相似度算法大全(万字讲解)
算法·数据挖掘·代理模式
XMYX-013 小时前
40 - Go HTTP 客户端:从 http.Get 到高性能连接池
开发语言·http·golang
Daydream.V13 小时前
C++ 入门全攻略:从基础语法到核心特性
java·开发语言·c++