【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, ...
随机推荐
- git删除远程主机没有的tag
可以先删除所有本地tag,然后再拉取远程上的tag git tag -l | xargs git tag -d git fetch --tags 其他方法以及查询tag的命令请见:Remove loc ...
- Rest客户端
public class RestClient { public string EndPoint { get; set; } //请求的url地址 public HttpVerb Method { g ...
- linux ssh 免密码登录的配置过程
# ssh-keygen -t rsa -C "自定义描述" -f ~/.ssh/自定义生成的rsa文件 # cd ./.ssh # touch config # 粘贴 Host ...
- http协议 put、delete请求asp.net mvc应用,报404错误
http协议 put.delete请求asp.net mvc应用,报404错误 更改web.config,在<modules>节点中设置 runAllManagedModulesForAl ...
- "类工厂模式"改写SqlHelper
看到标题您一定很疑惑,23种经典设计模式什么时候多了一个"类工厂模式",稍等,请听我慢慢道来. 实践是检验真理的唯一途径.最近用了"类工厂模式"改写了我公司的S ...
- [javaSE] 集合工具类(Collections-sort)
java为我们提供了一个集合的工具类,方便我们对集合进行操作,里面的方法都是静态方法. Collections.sort()方法,参数:List<T>集合对象,这个对象带着泛型,是为了保证 ...
- 多线程-lock锁
package 多线程.lock锁; import java.util.concurrent.locks.ReentrantLock; /*. * * //同步代码块 * * */ public cl ...
- SpringFramework中重定向
需求: 需要在两个@Controller之间跳转,实现重定向 解决: @PostMapping("/files/{path1}") public String upload(... ...
- lumen配置日志daily模式
默认的日志保存模式是single 也就是单文件模式 要想改成每日的daily模式可以在bootstrap/app.php下添加: /* * 配置日志文件为每日 */ $app->configur ...
- SSH注解方式与XML配置方式对照表
一.Hibernate 1.一对多注解 2.单表注解 二.Struts2 Struts2注解 三.Spring 1.IOC注解 2.AOP注解