【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
解法一:
排序,然后比较相邻元素是否相同。时间复杂度O(nlogn),空间复杂度O(1)
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.size() <= )
return false;
sort(nums.begin(), nums.end());
for(int i = ; i < nums.size(); i ++)
{
if(nums[i-] == nums[i])
return true;
}
return false;
}
};

解法二:
使用unordered_map记录已经出现的元素。时间复杂度O(n),空间复杂度O(n)
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.size() <= )
return false;
unordered_map<int, bool> m;
for(int i = ; i < nums.size(); i ++)
{
if(m[nums[i]] == true)
return true;
m[nums[i]] = true;
}
return false;
}
};

【LeetCode】217. Contains Duplicate (2 solutions)的更多相关文章
- 【LeetCode】217. Contains Duplicate 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计词频 使用set 排序 日期 [LeetCo ...
- 【LeetCode】217. Contains Duplicate
题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- 【一天一道LeetCode】#217. Contains Duplicate
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】609. Find Duplicate File in System 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 使用Java语言开发微信公众平台(三)——被关注回复与关键词回复
在上一篇文章中,我们实现了文本消息的接收与响应.可以在用户发送任何内容的时候,回复一段固定的文字.本章节中,我们将对上一章节的代码进行适当的完善,同时实现[被关注回复与关键词回复]功能. 一.微信可提 ...
- 【转】以太网帧、IP报文格式
原文:https://www.cnblogs.com/yongren1zu/p/6274460.html https://blog.csdn.net/gufachongyang02/article/d ...
- 【转】Linux基础与Linux下C语言编程基础
原文:https://www.cnblogs.com/huyufeng/p/4841232.html ------------------------------------------------- ...
- [转]使用 ssh -R 穿透局域网访问内部服务器主机,反向代理 无人值守化
原文: https://www.cnblogs.com/phpdragon/p/5314650.html ----------------------------------------------- ...
- Authentication and Authorization in ASP.NET Web API
You've created a web API, but now you want to control access to it. In this series of articles, we ...
- VC++导出具有命名空间的函数
问题现象 原因分析 解决的方法 1 问题现象 导出具有命名空间的函数和类.源码例如以下: 头文件MiniMFC.h namespace MiniMFC { __declspec(dllexport) ...
- 从C# 2.0新特性到C# 3.5新特性
一.C# 2.0 新特性: 1.泛型 List<MyObject> obj_list=new List(); obj_list.Add(new MyObject()); 2.部分类(par ...
- Linux中进程与线程及CPU使用率查询
一.进程查询: ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' 说明:PCPU是Cpu使用率,8核最多是800. 或者 ps -aux 二.线 ...
- An Objective-C Error
Incompatible integer to pointer conversion assigning to 'NSInteger *' (aka 'long *') from 'int' 主要是因 ...
- JAVA正确的四舍五入方法
在JDK版本为1.8的情况运行下面的代码,会发现很神奇的情况(见运行结果). 看如下代码: package com.longge.mytest; import java.math.BigDecimal ...