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;
    }
};
相关推荐
吃好睡好便好17 小时前
在Matlab中绘制圆锥三维曲面图
开发语言·人工智能·学习·算法·matlab·信息可视化
摇滚侠17 小时前
并发编程 Java 面试题 真正的 offer 偏方 Java 基础 Java 高级
java·开发语言
兰令水17 小时前
topcode【随机算法题】【2026.5.15打卡-java版本】
java·算法·leetcode
洛水水17 小时前
【力扣100题】44.完全平方数
算法·leetcode·职场和发展
橙淮17 小时前
哈希核心:高效映射与安全加密
算法·哈希算法
JAVA学习通19 小时前
北京明光云振铎数据科技Java面经
java·开发语言·科技
jerryinwuhan1 天前
基于各城市站点流量的复合功能比较
开发语言·php
浅念-1 天前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
Kiling_07041 天前
Java集合进阶:Set与Collections详解
算法·哈希算法
迈巴赫车主1 天前
Java基础:list、set、map一遍过
java·开发语言