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、不使用多余空间,先对数组进行排序o(nlogn),在依次检查相邻元素是否有重复o(n),整体时间复杂度o(nlogn),空间复杂度o(1);

2、使用hash表判断数字是否重复,用空间换时间。时间复杂度o(n),空间复杂度o(n);

代码(hash):

 class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
map<int, bool> num_map;
map<int, bool>::iterator iter; for (int i = ; i < nums.size(); ++i) {
iter = num_map.find(nums[i]);
if (iter != num_map.end())
return true;
num_map[nums[i]] = true;
} return false;
}
};

【Leetcode】【Easy】Contains Duplicate的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  6. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  7. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  8. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  9. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  10. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

随机推荐

  1. php 使用 rabbitmq

    1,配置好rabbitmq 服务器 (参照 http://www.cnblogs.com/spicy/p/7017603.html)(我是linux) 2,新增了一个用户 并点击该用户 增加权限如下

  2. javac的访问者模式2

    (5)Printer /** * A combined type/symbol visitor for generating non-trivial(有意义的) localized string * ...

  3. Java GC 垃圾回收算法 内存分配

    垃圾回收(Garbage Collection, GC)是Java不同于c与c++的重要特性之一. 他帮助Java自动清空堆中不再使用的对象. 由于不需要手动释放内存,程序员在编程中也可以减少犯错的机 ...

  4. Windows Azure上的大数据服务: HDInsight的介绍

    这个视频介绍了目前非常流行的大数据处理框架Hadoop的Windows Azure上的实现:HDInsight,以及利用MapReduce来对大数据进行分析,利用Hive进行查询,利用客户端Power ...

  5. LINQ-from多from

    简: LINQ全称是Language  Integrated Query,中文“语言集成查询”.LINQ是一种查询技术,有LINQ toSQL.LINQ to Object. LINQ to ADO. ...

  6. DataGridView 获取当前单元格

    获取DataGridview控件中的当前单元格,是通过DataGridview的Rows属性和Column属性的索引来取得的,他们的索引都是从0开始的. Private void datagridvi ...

  7. Linux 服务器 MySql的安装和网站的发布

    Linux安装MySql,并配置能通过自己的电脑连接服务器的数据库 昨天安装的MySql,今天上午配置MySql能使用本机连接服务器数据库,服务器时DigitalOcean的,提供了很全面很专业的文档 ...

  8. 关于UI回调Invoker的实现(一)

    打算写一个DirectUI库,在写其中底层窗口的回调构造的时候遇到一个问题. Invoker是一个模板,因为closure的关系,它必须保存一个类对象的指针,和回调函数的地址.而函数调用的时候,就可以 ...

  9. C# 请求Https

    /// <summary> /// Get请求 /// </summary> /// <param name="Url"></param& ...

  10. Web Service与Apache CXF 框架

    一.WebService简介 为了支持跨网络的机器间相互操作交互而设计,用于开发分布式的互操作的应用程序组件. Web Service服务通常被定义为一组模块化的API,它们可以通过网络进行调用,来执 ...