LeetCode 217 Contains Duplicate
Problem:
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.
Summary:
判断数组中是否出现重复值。
Analysis:
1. 给数组排序后遍历,判断相邻两值是否相等。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int len = nums.size();
sort(nums.begin(), nums.end());
for (int i = ; i < len; i++) {
if (nums[i] == nums[i - ]) {
return true;
}
}
return false;
}
};
2. Hash表建立数组中数字和出现次数的映射,判断是否大于1。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int len = nums.size();
unordered_map<int, int> m;
for (int i = ; i < len; i++) {
if(++m[nums[i]] > ) {
return true;
}
}
return false;
}
};
LeetCode 217 Contains Duplicate的更多相关文章
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- LN : leetcode 217 Contains Duplicate
lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- leetcode 217 Contains Duplicate 数组中是否有反复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- Java for LeetCode 217 Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- (easy)LeetCode 217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- ngCordova插件安装使用
为什么ngCordova ngCordova是在Cordova Api基础上封装的一系列开源的AngularJs服务和扩展,让开发者可以方便的在HybridApp开发中调用设备能力,即可以在Angul ...
- Java Sax解析
一. Java Sax解析是按照xml文件的顺序一步一步的来解析,在解析xml文件之前,我们要先了解xml文件的节点的种类,一种是ElementNode,一种是TextNode.如下面的这段boo ...
- POJ 2411 Mondriaan's Dream
状压DP Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9938 Accepted: 575 ...
- Mac Pro 安装 最新版的 SVN 1.9.4
系统自带的 SVN 版本为 1.7.2 $ svn --version svn, version 1.7.22 (r1694152) 有点老,安装下最新版本 brew install svn 由于老版 ...
- java中堆和栈的区别
从宏观上来讲,栈内存:存储基本数据类型.堆内存:存储实际的对象内容.说明白点就是new出来的东西. int a = 3; int b = 3; a = 4; 编译器首先会处理int a = 3;将a进 ...
- .net实现微信公众账号接口开发
说起微信公众帐号,大家都不会陌生,使用这个平台能给网站或系统增加一个新亮点,直接进入正题吧,在使用之前一定要仔细阅读官方API文档. API文档地址:http://mp.weixin.qq.com/w ...
- 批处理快速更改ip地址
在各种网络中切换,windows更换ip地址步骤: 进入控制面板--网络和internet--网络和共享中心--理性适配器设置--然后找到网卡--进入属性--然后internet 协议--更改ip信 ...
- Win7平台下React-Native开发之Android项目打包发布流程
一.bundle文件 React-Native开发步骤中,有一个步骤是使用命令 react-native start 去启动一个基于Node.js的服务,名字为packager.这个packager的 ...
- linux下安装使用libuuid(uuid-generate)
linux下安装使用libuuid(uuid-generate) linux下安装使用libuuid(uuid-generate) UUID简介 安装libuuid库 编写一个程序试一下 代码 编译运 ...
- 转:sql之left join、right join、inner join的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...