一. 题目描写叙述

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.

二. 题目分析

题目的大意是,给定一个整数数组,推断数组中是否包括反复的元素。若数组中随意一个数字出现了至少两次,函数返回true;否则,返回false

这里提供两个方法:

  1. 先对数组进行排序。然后遍历数组,若出现两个相邻元素的值同样时。表明有反复元素。返回true。反之返回false。
  2. 使用map来实现。遍历数组。每訪问一个元素。看其是否在map中出现。如已出现过。则存在反复元素,返回true;如没有,则将元素增加到map中。

三. 演示样例代码

// 方法一
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int n = nums.size();
if (n < 2) return false;
sort(nums.begin(), nums.end());
for (int i = 1; i < n; ++i)
if (nums[i] == nums[i - 1]) return true;
return false;
}
};
// 方法二
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int, int> numsMap;
for (int i = 0; i < nums.size(); ++i) {
if(numsMap.count(nums[i])){
return true;
}
numsMap.insert(pair<int, int>(nums[i], i));
}
return false;
}
};

四. 小结

相关的题目有:Contains Duplicate II 和 Contains Duplicate III。

leetcode笔记:Contains Duplicate的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  10. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

随机推荐

  1. verilog behavioral modeling ---Block statements

    block statements : 1. sequential block  : begin-end block 2.parallel block       :  fork - join bloc ...

  2. 什么是php?php的优缺点有哪些?与其它编程语言的优缺点?

    身为一个PHP开发者,有必要了解一下PHP的缺点,知道每种语言的优点和缺点,才能知道某种语言在什么场景下适合使用,在什么场景下不适合使用. 这个问题我曾经面试的时候遇到过,我之前没总结过,第一问大部分 ...

  3. Python中单元测试出错了,会怎么样?

    在上一篇中,我们看到了单元测试正常通过时的情况,如果我们边修改一下要测试的函数,然后再运行单元测试,会出现什么情况. 原say_hello_name.py def hello_name(name): ...

  4. linux python 安装 pip出现 No module named 'setuptools'

    1.下载pip wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#m ...

  5. 牛腩新闻发布系统(三):CSS盒子模型及其基本内容

    导读: 这些天一直在做牛腩的网页,比如什么首页.出错页.新闻内容页等.在学习的不断推进中,一些刚开始理解的不是很好的东西,也逐渐的深刻了起来.下面,就对这一段时间的学习,做一个总结.主要总结内容有:盒 ...

  6. Java中NIO、BIO、AIO相关概念及应用场景

    1.同步阻塞IO(JAVA BIO):同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时,服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通 ...

  7. AXMLPrinter2.jar反编译xml文件

    apk里的AndroidManifest.xml 为二进制文件,可通过AXMLPrinter2.jar包反编译出来 cmd命令行运行一下命令: java -jar AXMLPrinter2.jar A ...

  8. gitweb 搭建教程

    1. 前言 git 是一个版本控制工具,类似svn. 本文内容主要涉及git仓库通过浏览器访问(用web的方式去查看git提交历史记录,tag,branch等信息),即gitweb. 效果图: 在这里 ...

  9. R语言入门--画图(一)--ggplot2

    先写一些需要用到的知识点,比如包.函数 dplyr 很好用的包 经常与ggplot2连用 mutate:用于对数据框的列进行重新处理,或者用处理的结果添加新列 数据清洗: 1.na.omit()   ...

  10. js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]

    <script> /* 数学对象:Math */ with (document) { write('<br>-3.5的绝对值:'+Math.abs(-3.5)); write( ...