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;
    }
};
相关推荐
兰令水2 分钟前
topcode【随机算法题】【2026.5.14打卡-java版本】
java·算法·leetcode
雪度娃娃2 分钟前
结构型设计模式——代理模式
java·c++·设计模式·系统安全·代理模式
故事和你914 分钟前
洛谷-【图论2-1】树2
开发语言·数据结构·c++·算法·动态规划·图论
MicroTech202510 分钟前
变分量子算法再升级:MLGO微算法科技滤波变分量子本征求解器推动量子计算落地
科技·算法·量子计算
折哥的程序人生 · 物流技术专研15 分钟前
Java面试85题图解版 · 全系列总目录
java·开发语言·后端·面试·职场和发展
小辉同志18 分钟前
72. 编辑距离
leetcode·多维动态规划
gf132111120 分钟前
飞书长连接_事件订阅(接收消息,审批任务状态变更)
开发语言·python·飞书
gihigo199821 分钟前
竞争性自适应重加权算法(CARS)
算法
木易 士心23 分钟前
Java 跳出多层循环
java·开发语言·后端
kyle~26 分钟前
C++---段错误(SIGSEGV)
linux·运维·c++·机器人