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;
    }
};
相关推荐
凉云生烟2 小时前
机器学习 02- KNN算法
人工智能·算法·机器学习
wabs6662 小时前
关于动态规划【力扣583.两个字符串的删除操作的思考】
算法·leetcode·动态规划
cui_ruicheng2 小时前
Python从入门到实战(六):非序列容器
开发语言·python
可编程芯片开发2 小时前
空气流量和空气压力参数解耦系统simulink建模与仿真
算法
小小龙学IT2 小时前
C++ 并发编程深度解析:从内存模型到无锁数据结构
数据结构·c++
西西学代码2 小时前
Flutter---底部导航栏(2)
开发语言·javascript·flutter
legendary_1632 小时前
SINK芯片:Type-C统一供电时代的小家电核心方案
c语言·开发语言·人工智能·智能手机
momo2 小时前
JAVA基础知识
java·开发语言
_Doubletful3 小时前
妙用位运算:解构汉明距离至100%(提供分析与多解)
c语言·算法·leetcode