LeetCode(217)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.
分析
判断所给整数序列中有无重复元素。
AC代码
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.empty())
return false;
unordered_set<int> us;
int len = nums.size();
for (int i = 0; i < len; ++i)
{
if (us.count(nums[i]) != 0)
return true;
else
us.insert(nums[i]);
}
return false;
}
};
LeetCode(217)Contains Duplicate的更多相关文章
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- 爬虫(GET)——handler处理器和自定义opener
工具:python3 解释:urlopen()不支持代理.cookie等其他的http/https高级功能,所以需要handler处理器创建特定功能的处理器对象,urllib.request.buli ...
- PartTime_一些网站
1. http://www.sxsoft.com/ 貌似 搜搜"破解",无符合条件的结果 http://www.taskcity.com/ "智城",貌似 符合 ...
- LindAgile~缓存拦截器支持类的虚方法了
写它的原因 之前写过一个缓存拦截器,主要在方法上添加CachingAspect特性之后,它的返回值就可以被缓存下来,下次访问时直接从缓存中返回结果,而它有一个前提,就是你的方法需要是一个接口方法,缓存 ...
- spring boot Filter过滤器的简单使用
springboot使用Filter过滤器有两种方式: 一种是实现Filter接口然后通过@Component注解向项目加入过滤器 另一种是通过配置类来配置过滤器 @Component public ...
- JVM类加载机制二
类加载器与双亲委派模型 类加载器 类加载的操作不是有虚拟机完成的,而是由类加载器完成的,这样可以让程序定义决定加载哪个类. 类加载器的分类: 从虚拟机的角度有两种加载器,一种是启动类加载器Bootst ...
- 带你零基础入门redis【二】
本篇文章介绍redis如何设置开机自启动以及如何在java中应用 一.设置redis开机自启 1.修改redis配置 [root@VM_6_102_centos ~]# vim /usr/local/ ...
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 .NET 4.5 installed Visual Studio 2012 Release Preview
Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt' after ...
- Typedef 用法
typedef声明有助于创建平台无关类型,甚至能隐藏复杂和难以理解的语法. 不管怎样,使用 typedef 能为代码带来意想不到的好处,通过本文你可以学习用typedef避免缺欠,从而使代码更健壮. ...
- Android中当item数量超过一定大小RecyclerView高度固定
Android中当item数量超过一定大小时,将RecyclerView高度固定 方法1 直接通过LayoutParams来设定相应高度 ViewGroup.LayoutParams lp = rv. ...
- 【Python图像特征的音乐序列生成】数据集制作的一些tricks
关于数据集的制作,我决定去掉很多不必要的东西,比如和弦,于是我选择了melody部分的旋律. 有了midi文件,我现在要abc序列,所以我要通过midi2abc转换一下文件. 批处理程序效果如下: 文 ...