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 ...
随机推荐
- [SoapUI]获取Project,Test Suite,Test Case各个级别参数的值
String testResultPath = testRunner.testCase.testSuite.project.getPropertyValue( "testResultPath ...
- Greeplum 系列(七) 权限管理
Greeplum 系列(七) 权限管理 一.角色管理 Role 分为用户(User)和组(Group),用户有 login 权限,组用来管理用户,一般不会有 login 权限.初始化 gp 时创建了一 ...
- 职责链模式c#(处理车)
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace 职责链模式{ ...
- Path Creation and Path Painting
[Path Creation and Path Painting] Path creation and path painting are separate tasks. First you crea ...
- 一条java开发工程师的升级路线,从初级到无语言障碍
看了一篇文章,讲述的是如何进行后端开发升级,现在分享下,我的总结,感谢写文章的作者大大,觉得他很会坚持,虽然一直在骂人,但是,我觉得人最大的敌人就是懒惰,所以骂得好 现在写下我的总结,希望对有志者有帮 ...
- 使用phpStorm编辑器进行PHP代码的xdebug调试
首先需要安装Xdebug,如果没有安装可以查看PHP断点调试工具Xdebug的安装这篇文章.phpStorm是开发者经常用的一款编辑器,当然也支持Xdebug调试,下面说一下配置步骤. phpStor ...
- linux上chrome、vlc等程序root不能运行的解决办法
which vlc 或者 whereis vlc 输入/geteuid,输入i进入输入模式,将geteuid改成getppid,然后ESC,输入wq,保存退出,这样程序root用户就可以运行了. ch ...
- Computer
Computer 1. Ctrl+N .根据惯例,“Control”.“Shift” 以及 “Alternate” 按键将以 Ctrl.Shift 以及 Alt 来表示,需要特别指出的是,其中第一个按 ...
- 【转发】在SQL Server中通过字段值查询存储该字段的表
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved. -- Purpose: To search all colu ...
- Java Socket实现基于TCP和UDP多线程通信
一.通过Socket实现TCP编程 1.1 TCP编程 TCP协议是面向连接,可靠的,有序的,以字节流的方式发送数据.基于TCP协议实现网络通信的类有客户端的Socket类和服务器端的ServerSo ...