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;
    }
};
相关推荐
额额额对了3 分钟前
Linux C 多线程编程基础:从内存模型到生命周期管理
linux·开发语言·算法
尼古拉斯-托尔斯泰-赵四17 分钟前
Go 语言,你需要了解的一些规则
开发语言·后端·golang
INGNIGHT37 分钟前
1584.连接所有点的最小费用(最小生成树&并查集union find)
c++·leetcode
wabs66637 分钟前
关于二叉树【力扣144.二叉树的前序遍历的思考】
数据结构·c++·算法·leetcode·二叉树
郝学胜-神的一滴1 小时前
C++20 高级编程 004:从初始化、内存到const系列关键字
开发语言·算法·编程·软件构建·c++20
雪芽蓝域zzs1 小时前
Vue3 + JS + pnpm 创建项目
开发语言·javascript·ecmascript
一晌小贪欢1 小时前
Python办公16:PDF 强力缝合——将几十个 PDF 合并为单个文档并添加页码
开发语言·python·excel·数据可视化·python办公
名剑走天下1 小时前
Kotlin_LiveData
开发语言·kotlin
kyle~1 小时前
点云配准--- 迭代最近点 ICP 求解
线性代数·算法·机器学习