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 <= numsi <= 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;
    }
};
相关推荐
无名猿11 分钟前
shared_ptr 完全指南:引用计数、控制块与开销
c++·性能优化·内存管理·标准库·现代c++
Sirens.33 分钟前
Java并发锁详解:六类锁策略与 synchronized 底层原理
java·前端·算法
和裕1 小时前
全纸结构重型纸箱能否满足 1 吨以上设备出口熏蒸豁免要求?通关合规性全解析
大数据·运维·网络·人工智能·算法
tangguofeng1 小时前
不带头结点的链队操作集(不含InitFlag)(C语言版)
算法
weixin_440730502 小时前
函数return、参数、参数组、递归函数、高阶函数等小结
开发语言·python
sugarzhangnotes2 小时前
【无标题】
开发语言·人工智能·python
geovindu2 小时前
rust: Composite Pattern
开发语言·后端·设计模式·rust·组合模式
xailorliu3 小时前
SLIC_超像素算法
c++·opencv·目标检测·机器学习·计算机视觉
苏打豆3 小时前
CRISP源码阅读——基于ROS2力矩反馈控制器与速度加速度零空间
c++·线性代数·算法·矩阵·机器人