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;
    }
};
相关推荐
秋雨梧桐叶落莳3 分钟前
iOS——Masonry约束内容整理
开发语言·学习·macos·ios·objective-c·cocoa
tankeven4 分钟前
贪心算法(Greedy Algorithm)详解:从理论到C++实践
c++·算法
Hesionberger4 分钟前
LeetCode72.编辑距离(多维动态规划)
java·开发语言·c++·python·算法
lwf0061646 分钟前
逻辑回归学习笔记-梯度下降求解回归方程
算法·机器学习·逻辑回归
郝学胜-神的一滴6 分钟前
从底层看透Linux高性能服务器:epoll自定义封装与超时清理实战
linux·服务器·c++·网络协议·tcp/ip·unix
人道领域12 分钟前
【LeetCode刷题日记】1047:双栈法与双指针法巧妙消除相邻重复字符
java·算法·leetcode·职场和发展
Via_Neo12 分钟前
Bash Game
开发语言·bash
切糕师学AI12 分钟前
布隆过滤器(Bloom Filter)技术详解
数学·算法
礼拜天没时间.17 分钟前
力扣热题100实战 | 第33期:搜索旋转排序数组——二分查找的变体艺术
算法·leetcode·职场和发展·旋转数组·搜索旋转排序数组
Jenlybein31 分钟前
用 uv 替代 conda,速度飙升(从 0 到 1 开始使用 uv)
后端·python·算法