【Leetcode】【Easy】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.
解题:
题目很简单,判断数组中是否含有重复数字,有重复则返回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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
随机推荐
- Jquery动画操作的stop()函数
今天做一个点击动画时,遇到了当快速连续点击时,动画效果会乱,并不是我们想要达到的效果. 查询了一下,确认是动画累积的原因.网上搜了一下,发现jquery 的stop()函数刚好能解决. stop(cl ...
- Idea查看代码相关技巧
(1)查看类中的属性与方法快捷键 Alt+7 (2)查看方法被调用 在方法上右键find usages (3)查看方法说明 Ctrl+Q
- WindowsServer2008安装IIS相关服务
控制面板->程序->打开或关闭Windows功能 添加角色,选择IIS服务器,选择以下角色服务,如果添加过就选择添加角色服务 如果出现500错误,这个跟程序没有多大关系,可以试一下以下操作 ...
- IDEA的学习总结
IntelliJ IDEA是一款非常优秀的JAVA编辑器,初学都可会对其中的一些做法感到很别扭,刚开始用的时候我也感到很不习惯,在参考了网上一些文章后在这里把我的一些经验写出来,希望初学者能快速适应它 ...
- 什么是SharePoint?
在聊SharePoint开发之前,有必要说下什么是SharePoint. 在我工作的过程中,经常遇到客户对SharePoint不太了解的情况.有客户说,SharePoint太烂了,DropBox能做到 ...
- mysql-5.6.31安装(单实例 Linux)
安装版本:mysql-5.6.31 安装环境:Linux RHEL6.5.x86 安装要求:单实例,端口为默认:3306 (1) 要求安装Mysql数据库版本号及包名为:my ...
- 移动端使用的WebKit私有属性(转)
<!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>无标 ...
- 搭建Eclipse和MyEclipse的开发环境
主要步骤: 下载并配置Eclipse 建立并运行一个简单的javaSE项目 下载并破解MyEclipse 整合Eclipse和MyEclipse 开发环境和Tomcat结合 关于这个配置也可以参考:h ...
- java使用lock实现一个简单的生产者和消费者模式
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public ...
- 【SSH网上商城项目实战18】过滤器实现购物登录功能的判断
转自:https://blog.csdn.net/eson_15/article/details/51425010 上一节我们做完了购物车的基本操作,但是有个问题是:当用户点击结算时,我们应该做一个登 ...