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 ...
随机推荐
- jquery封装的方法
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Mongodb命令行导入导出数据
第一步,找到mongodb安装目录第二步,从命令行进入mongodb安装目录下的bin目录第三步(1),导出数据命令(导出的文件有两种格式:json/csv,此处导出的是json文件,对于导出CSV文 ...
- C#进阶系列——WebApi异常处理解决方案
阅读目录 一.使用异常筛选器捕获所有异常 二.HttpResponseException自定义异常信息 三.返回HttpError 四.总结 正文 为什么说是实践?因为在http://www.asp. ...
- php 跳转页面
header('location:./example.php'); header('refresh:2;url=./example.php');
- css 省略号的写法
单行省略号 overflow: hidden; text-overflow:ellipsis; white-space: nowrap; width:500px; 多行省略号 overflow: hi ...
- Java QuickSelect
Java QuickSelect /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternationa ...
- 一、hystrix如何集成在openfeign中使用
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 HystrixInvocationHandler hystrix是开源的一个熔断组件,s ...
- 【转载】Sqlserver使用Group By进行分组并计算每个组的数量
在SQL语句查询中,Group By语句时常用来进行分组操作,有时候在分组的同时还需要计算出每个组的数量多少.在Sqlserver数据库中可以使用Group By加Count聚合函数来实现此功能,即通 ...
- 【坑】select2 模态框中下拉input无法focus
select2的组件bug 解决方案: 在bootstrap.js中修改: Modal.prototype.enforceFocus = function () { $(document) .off( ...
- C语言面试题目之指针和数组
说明:所有题目均摘录于网络以及我所见过的面试题目,欢迎补充! 无特殊说明情况下,下面所有题s目都是linux下的32位C程序. 先来几个简单的热热身. 1.计算以下sizeof的值. char str ...