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;
    }
};
相关推荐
游乐码几秒前
c#迭代器
开发语言·c#
十五年专注C++开发6 分钟前
Qt程序设计涉及到的开发软件
开发语言·c++·qt
海盗12347 分钟前
C# OPC UA客户端开发实战
服务器·开发语言·c#
asdzx6713 分钟前
使用 C# 从 URL 下载 Word 文档
开发语言·c#·word
大萌神Nagato15 分钟前
python 包管理器uv
开发语言·python·uv
humcomm25 分钟前
AI 编程时代-全栈开发技术栈解析
开发语言·人工智能
人道领域37 分钟前
【黑马点评日记】社交平台用户关注功能全解析Feed流相关操作
java·开发语言·数据库·redis·python
地平线开发者40 分钟前
征程 6X 之 Memory corruption 问题分析方法
算法·自动驾驶
地平线开发者44 分钟前
Sparse4D:从 Dense BEV 到工程可落地的世界建模
算法·自动驾驶
xiaoshuaishuai81 小时前
C# DeepSeek V4 与 V3对比
开发语言·c#·量子计算