LeetCode OJ: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.
判断数组里面是否有重复的,很简单:
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int sz = nums.size();
if (sz <= ) return true;
sort(nums.begin(), nums.end());
for (int i = ; i < sz; ++i){
if (nums[i] == nums[i - ])
return false;
}
return true;
}
};
java版本,用HashSet来做的,这样的效率应该比上面的要高上不少,代码如下:
public class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> s = new HashSet<Integer>();
for(int i = 0; i< nums.length; ++i){
if(!s.contains(nums[i])){
s.add(nums[i]);
}else
return true;
}
return false;
}
}
c++的Hash代码也写一遍试试:
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int sz = nums.size();
unordered_set<int> s;
for(int i = 0; i < sz; ++i){
if(s.find(nums[i]) == s.end()){
s.insert(nums[i]);
}else
return true;
}
return false;
}
};
发现实际上比java的runtime还是要慢上不少,大概用了java三倍的时间,我也不知道怎么回事。
LeetCode OJ:Contains Duplicate(是否包含重复)的更多相关文章
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)
题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Jav ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- [LeetCode]652. Find Duplicate Subtrees找到重复树
核心思想是:序列化树 序列化后,用String可以唯一的代表一棵树,其实就是前序遍历改造一下(空节点用符号表示): 一边序列化,一边用哈希表记录有没有重复的,如果有就添加,注意不能重复添加. 重点就是 ...
- LeetCode Find the Duplicate Number 找重复出现的数(技巧)
题意: 有一个含有n+1个元素的数组,元素值是在1-n之间的整数,请找出其中出现超过1次的数.(保证仅有1个出现次数是超过1的数) 思路: 方法一:O(nlogn).根据鸽笼原理及题意,每次如果< ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- 动态生成ABAP程序-资料
参考程序: Tcode ABAPdocu--> BC - ABAP Programming--> The ABAP Programming Language--> Special T ...
- 4.3 使用STM32控制MC20进行GPRS通讯
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- Centos6.6安装mysql记录
一.环境介绍: 系统:Cerntos6.6 Mysql版本:mysql-5.6.34 二.安装操作: 1.卸载旧版本: rpm -qa |grep mysql mysql-server-5.1.73- ...
- spring mvc 自动扫描注解失效原因
关于spring自动扫描,在控制层,采用注解配置@Controller,项目能够成功启动,且无任何报错.但是 在进行页面跳转时,并未进行相应的拦截,整个界面只能在默认界面 ,跳转报404,由于楼主初次 ...
- PAT 天梯赛 L1-020. 帅到没朋友 【STL】
题目链接 https://www.patest.cn/contests/gplt/L1-020 思路 对于每个 K >= 2 的朋友圈,里面的所有 ID 都用 MAP 标记一下 对于每个 K = ...
- sublime text C++
几乎每一门编程语言都是从"Hello, world!"学起的, 刚学编程的时候感觉有点枯燥, 对它不够重视. 可是到后来慢慢发现, 几乎我学到的每一个知识点, 在最开始都是经过 h ...
- qt的登录设置(转)
1.下面添加代码来实现使用用户名和密码登录,这里只是简单将用户名和密码设置为了固定的字符串,如果以后学习了数据库,还可以通过读取数据库来获取用户名和密码.到logindialog.cpp文件中将登录按 ...
- matplotlib模块之子图画法
一般化的子图布局 首先要创建各个子图的坐标轴,传入一个四元列表参数:[x,y,width,height],用来表示这个子图坐标轴原点的x坐标.y坐标,以及宽和高.值得注意的是,这四个值的取值范围都是[ ...
- Go 字符串相关-标准库
标准库中有四个包对字符串处理尤为重要: bytes strings strconv unicode strings包提供了许多如字符串的查询.替换.比较.截断.拆分和合并等功能. bytes包也提供了 ...
- 【bzoj2118&洛谷P2371】墨墨的等式(最短路神仙题)
题目传送门:bzoj2118 洛谷P2371 这道题看了题解后才会的..果然是国家集训队的神仙题,思维独特. 首先若方程$ \sum_{i=1}^{n}a_ix_i=k $有非负整数解,那么显然对于每 ...