leetcode 217 Contains Duplicate 数组中是否有反复的数字
Contains Duplicate
Total Accepted: 26477
Total Submissions: 73478 My Submissions
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)
{
set<int> result;
set<int>::iterator itor ;
for(int i = 0;i< nums.size();++i)
{
itor = result.find(nums[i]) ;
if(itor != result.end())
{
return true;
}
else
{
result.insert(nums[i]);
}
}
return false;
}
};
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
return nums.size() > set<int>(nums.begin(), nums.end()).size();
}
};
python 的版本号:
class Solution:
def containsDuplicate(self, nums):
return len(nums) > len(set(nums))
c++ 的hash版本号:同类的hash code是同样的,这是一个很重要的编程思想
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> hashset;
for (int i = 0; i < nums.size(); ++i) {
if (hashset.find(nums[i]) != hashset.end()) {
return true;
}
else {
hashset.insert(nums[i]);
}
}
return false;
}
};
c++排序版本号:
+2 votes
942 views
class Solution {
public:
bool containsDuplicate(vector<int>& nums)
{
int size=nums.size();
sort(nums.begin(),nums.end());
nums.erase(unique(nums.begin(),nums.end()),nums.end());
return (size!=nums.size());
}
}; +4 votes
Your running time is 28ms, if not use unique, it will be 24ms:
class Solution {
public:
bool containsDuplicate(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size(); ++i)
if (nums[i] == nums[i - 1])
return true;
return false;
}
};
leetcode 217 Contains Duplicate 数组中是否有反复的数字的更多相关文章
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II
[题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- LeetCode 80. 删除排序数组中的重复项 II
LeetCode 80. 删除排序数组中的重复项 II
- 前端与算法 leetcode 26. 删除排序数组中的重复项
目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...
- [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 每天一道面试题LeetCode 80--删除排序数组中的重复项 II(python实现)
LeetCode 80--删除排序数组中的重复项 II 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输 ...
- 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字
题目:给定一个长度为N的数组,其中每个元素的取值范围都是1到N.判断数组中是否有重复的数字.(原数组不必保留) 方法1.对数组进行排序(快速,堆),然后比较相邻的元素是否相同.时间复杂度为O(nlog ...
随机推荐
- Typora——自定义设置
Typora提供自定义设置,在偏好设置里面,有一个主题文件夹,如果对界面的样式进行设定,可以添加一个css文件,命名规范是 github.user.css,下面代码会对h1~h4进行自动序列化 bod ...
- QS之shell script
1 Invoke Mdoelsim In order to open Modelsim automatically, it is better to use a shell script to inv ...
- Java 基础入门随笔(10) JavaSE版——单例设计模式
设计模式:对问题行之有效的解决方式.其实它是一种思想. 1.单例设计模式. 解决的问题:就是可以保证一个类在内存中的对象唯一性.(单个实例) 使用单例设计模式需求:必须对于多个程序使用同一个配置信息对 ...
- Java 基础入门随笔(9) JavaSE版——文档注释
上节中写了一些static变量以及静态的方法的定义使用以及与非静态的差别,这节补充下: 如果在一个类中所有方法都为静态的,且无成员变量,这时候需要对对应的类进行限制该类无法创建对象,具体操作如下: p ...
- 【windows】自动化测试持续集成(CI)环境部署
1. 环境准备 1.1 我的环境 1.Win10 64位 2.JDK 1.8.0_121 3.Tomcat 7.0.92 4. Jenkins 2.24 5.SVN-Server 3.8.1 1.2 ...
- Microsoft SQL Server Transact-SQL
Microsoft SQL Server Transact-SQL 1.SQL 1.1数据定义语言(DDL) create 创建数据库或数据库对象:alter 修改数据库或数据库对象:drop 删除数 ...
- 07Oracle Database 数据表
Oracle Database 数据表 DDL 数据定义语言 - 建立数据库对象 create /alter/ drop/ truncate 创建表 Create table table_name( ...
- luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
Code: #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char ...
- Linux常用命令——目录处理命令
1.建立目录:mkdir mkdir -p [目录名] -p 递归创建 命令英文原意:make directories 实例: [root@localhost ~]# ls anaconda-ks.c ...
- JavaScript ES6 数组新方法 学习随笔
JavaScript ES6 数组新方法 学习随笔 新建数组 var arr = [1, 2, 2, 3, 4] includes 方法 includes 查找数组有无该参数 有返回true var ...