一. 题目描写叙述

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. cache支持single/increment/increment4三种方式传输

    1.cache bypass signle---data length 已知 increment ---data length 不知 用 last data address  结束数据传输 2.cac ...

  2. Hibernate 框架理解

    Hibernate框架简化了java应用程序与数据库交互的开发.Hibernate是一个开源,轻量级的ORM(对象关系映射)工具. ORM工具简化浏览数据的创建,数据处理和数据访问.它是将对象映射到数 ...

  3. oracle sequence的用法

    在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence     首先要有CREATE SEQUENCE或者CREATE ...

  4. Linux cp复制

    复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir2.怎样才能将dir1下所有文件复制到dir2下 ...

  5. 字符串格式化format很牛B

    python的format方法可谓相当强大,它可以接受不限个参数... 1.通过位置来格式化字符串,注意format传入的参数的位置要正确{0}对应第1个参数,{1}对应第2个参数... >&g ...

  6. xtu数据结构 D. Necklace

    D. Necklace Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class ...

  7. poj3180 The Cow Prom

    The Cow Prom Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2373   Accepted: 1402 Desc ...

  8. ubuntu问题解答集锦

    一.su root提示认证失败 su root提示认证失败 ubuntu root是默认禁用了,不答应用root登陆,所以先要设置root密码.   执行:sudo passwd root 接着输入密 ...

  9. 文艺平衡树(bzoj 3223)

    Description   您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2  ...

  10. formSubmit

    精简代码: <form name='form0001' method="post"> .... <li id="view"><a ...