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;
    }
};
相关推荐
电子云与长程纠缠7 分钟前
UE5制作六边形包裹球体效果
开发语言·python·ue5
砍材农夫13 分钟前
物联网 基于netty构建mqtt协议规范(遗嘱与保留消息)
java·开发语言·物联网·netty
DFT计算杂谈16 分钟前
KPROJ编译教程
java·前端·python·算法·conda
重生之我是Java开发战士27 分钟前
【笔试强训】Week5:空调遥控, kotor和气球,走迷宫,主持人调度II,体操队形,二叉树的最大路径和,排序子序列,消减整数
java·算法·动态规划
froginwe1130 分钟前
Python3 迭代器与生成器
开发语言
xiaoshuaishuai842 分钟前
C# 签名异常与Gas预估失败调试方案
开发语言·网络·tcp/ip·c#
xiaoshuaishuai843 分钟前
C# Gemini 辅助网络安全漏洞分析
开发语言·web安全·c#
念恒123061 小时前
Python(循环中断)
开发语言·python
社交怪人1 小时前
【数字对调】信息学奥赛一本通C语言解法(题号2070)
c语言·开发语言
hef2881 小时前
C语言中char指针与数组的区别及应用
c语言·开发语言