leetcode解题报告(18):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) {
if(nums.size() == 0)return false;
sort(nums.begin(),nums.end());
for(int i = 0; i != nums.size() - 1; ++i){
if(nums[i] == nums[i + 1])return true;
}
return false;
}
};
讨论区发现了一行代码写出来的:
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
return nums.size() > set<int>(nums.begin(),nums.end()).size();
}
};
leetcode解题报告(18):Contains Duplicate的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetcode解题报告(19):Contains Duplicate II
描述 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum
1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
随机推荐
- Gitlab服务不能启动postgresql
源博文:http://www.zxmseed.com/blog/911081 1.查看启动的服务 -sh-4.1$ gitlab-ctl status warning: gitlab-workhors ...
- OAuth 2.0 简介
是什么: 授权框架/授权标准/授权协议(授权指的是授予你能做什么的权力) 干什么: 授权 使第三方应用程序或客户端获得对HTTP服务上用户帐户信息的有限访问权限(例如 Google,GitHub ) ...
- VBA Exit Do语句
当想要根据特定标准退出Do循环时,可使用Exit Do语句. 它可以同时用于Do...While和Do...Until直到循环. 当Exit Do被执行时,控制器在Do循环之后立即跳转到下一个语句. ...
- JavaScript常用节点类型
一.常用节点类型: nodeType:节点类型 nodeName:节点名称 nodeValue:节点值 1.查看节点类型(控制台操作): 获取元素:var p = document.getElemen ...
- Jmeter学习笔记(六)——使用badboy录制脚本
1.下载安装 可以去badboy官网下载地址:http://www.badboy.com.au,如果官网打不开也可以去网上搜索下载. 下载之后点击BadboyInstaller-2.2.5.exe普通 ...
- 微信小程序开发demo
自己写的小程序,欢迎下载 https://gitee.com/lijunchengit/chengZiShengHuoBang
- 理解JVM之java内存模型
java虚拟机规范中试图定义一种java内存模型(JMM)来屏蔽掉各种硬件和操作系统内存访问差异,以实现让java程序在各种平台都能打到一致的内存访问效果.所以java内存模型的主要目标是定义程序中各 ...
- sql sever2008 R2 检测到索引可能已损坏。请运行 DBCC CHECKDB。
1.设置成单用户状态 USE MASTER ALTER DATABASE DBNAME SET SINGLE_USER; GO --DBNAME为修复的数据库名 2.执行修复语句,检查和修复数据库及索 ...
- Python requests.post嵌套多层json参数调用接口
#coding:utf-8 import requests,json #第一行注解的#coding:utf-8表示可以支持中文,不然代码里面有中文会报错 url = "http://xxx& ...
- linux运维之路配置网络
前言裸机上装操作系统,想和物理机通信需要设置IP 开机以后: 第一步:setup命令 ——> NetWork configguation ---->Device configurat ...