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.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
要完成的函数:
bool containsDuplicate(vector<int>& nums)
说明:
1、给定一个vector,要求判断vector中包不包含重复元素。如果重复,那么返回true,如果全都是不重复的,那么返回false。
2、这道题我们可以用最笨的双重循环来做,也可以增加空间复杂度,建立set,用哈希的方法来判断有没有重复。
笔者也想过能不能用异或来做,最后觉得应该还是不太行。
最终选择了排序的方法,先快排整个vector,接着遍历一次整个vector,判断相邻元素有没有相同的,如果有就返回true,如果跑完一整个循环都没有,那么返回false。
代码十分简单,如下:
bool containsDuplicate(vector<int>& nums)
{
sort(nums.begin(),nums.end());//排序整个vector
int s1=nums.size();
for(int i=0;i<s1-1;i++)//从第一个元素开始,到倒数第二个元素结束
{
if(nums[i+1]==nums[i])//如果有相同元素
return true;
}
return false;//如果跑完全程都没有相同的
}
上述代码实测29ms,beats 99.14% of cpp submissions。
leetcode-217-Contains Duplicate(使用排序来判断整个数组有没有重复元素)的更多相关文章
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- LN : leetcode 217 Contains Duplicate
lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素
83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...
- 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- java8 stream初试,map排序,list去重,统计重复元素个数,获取map的key集合和value集合
//定义一个100元素的集合,包含A-Z List<String> list = new LinkedList<>(); for (int i =0;i<100;i++) ...
- LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素
一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...
随机推荐
- 存储过程返回update结果集和insert主键
update teacher set name ='111' where id in(286,289);print @@rowcount;--或select将查出,是@@rowcount,不是@row ...
- 把二叉搜索树转化成更大的树 · Convert BST to Greater Tree
[抄题]: 给定二叉搜索树(BST),将其转换为更大的树,使原始BST上每个节点的值都更改为在原始树中大于等于该节点值的节点值之和(包括该节点). Given a binary search Tree ...
- Qt Font
Font and How to use TTF字体基本知识及其在QT中的应用 Qt为程序添加外部字体 在使用qt 添加第三方字体的时候,在程序开始的时候,使用·QFontDatabse·的静态函数加载 ...
- Linux&&Mac 自动增加CSDN访问量
我心里面有两个小人. 一个叫愧疚,对CSDN这么一个分享知识的平台的愧疚,因为我正在做一件对不起CSDN的事情. 一个叫虚荣,对CSDN访问量的渴望过渡使得我踏出了这一步. 这一步,踏入了深渊.. 最 ...
- mybatis总结回顾
1.mybatis的介绍 轻量级数据持久层框架,替代hibernate 2.mybatis的入门 导包-->配置文件(类名.xml.SqlMapConfig.xml) 类名.xml:放映射.sq ...
- C语言字符编码处理
一.字符编码识别 1.简介 uchardet是一个开源的用于文本编码检测的C语言库,其功能模块是用C++实现的,通过一定数量的字符样本独立的分析出文本的编码,当前已经支持UTF-8/GB13080/B ...
- RabbitMQ的一些说明
下载安装包后,运行会提示你下再ErLang环境,根据提示下载安装就可以了 RabbitMQ 自己的插件包中带一 个WebUI的管理工具,在RabbitMQ安装目录(bin)下运行 rabbitmq-p ...
- Redis数据结构(五)
存储set (1)和List类型不同的是,Set集合中不允许出现重复的元素 (2)set可包含的最大元素数量是4294967295 存储set常用命令: (1)添加/删除元素 添加 sadd myli ...
- CodeForces - 589F —(二分+贪心)
A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...
- OpenSSL命令---crl2pkcs7
用途: 本命令根据CRL或证书来生成pkcs#7消息. 用法: openssl crl2pkcs7 [-inform PEM|DER ] [-outform PEM|DER ] [-in filena ...