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;
    }
};
相关推荐
Wy_编程34 分钟前
go中的协程Goroutine
开发语言·golang
lzh2004091942 分钟前
手撕线程池:巩固Linux线程知识
linux·c++
Godspeed Zhao1 小时前
从零开始学AI14——最大似然估计与对数损失函数
算法·逻辑回归·最大似然
basketball6161 小时前
C++ 命名空间知识点总结:从入门到合理设计
开发语言·c++
WL_Aurora1 小时前
Java多线程详解(一)
java·开发语言
流年如夢1 小时前
排序算法详解
数据结构·算法·排序算法
会编程的土豆1 小时前
Go 语言中的 `new` 关键字(创建指针)
java·算法·golang
RSTJ_16251 小时前
PYTHON+AI LLM DAY FOURTY-EIGHT
开发语言·人工智能·python·深度学习
南宫萧幕1 小时前
HEV能量管理建模实战:从零搭建 Simulink 物理环境到 Python(DQN) 强化学习联合仿真调通
开发语言·python·算法·matlab·汽车·控制