Contains Duplicate
2015-11-29 22:12
489 查看
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.
给定一个整形数组,从中找出是否存在重复元素。可以使用map来实现,遍历数组,每访问一个元素,看其是否在map中出现,如已出现过,则存在重复元素,如没有,则将元素加入到map中。
其他思路:
2、排序法。现将数组排序,然后扫描一次数组,若出现相邻的两个数相同,则包含重复元素,否则没有。此方法时间复杂度为O(nlogn),空间复杂度为O(1)【取决于排序方法】,不过用leetcode跑出来比1方法要快些,因为1方法建立set需要花费一些时间,也有测试用例的问题。
#include<iostream>
#include<map>
using namespace std;
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6,7, 8 };
int len = sizeof(a) / sizeof(a[0]);
map<int, int> m;
bool temp=false;
for (int i = 0; i < len; i++)
{
if (m.find(a[i]) == m.end())
m[a[i]]=i;
else
{
temp = true;
break;
}
}
cout << temp;
system("pause");
return 0;
}
twice in the array, and it should return false if every element is distinct.
给定一个整形数组,从中找出是否存在重复元素。可以使用map来实现,遍历数组,每访问一个元素,看其是否在map中出现,如已出现过,则存在重复元素,如没有,则将元素加入到map中。
class Solution { public: bool containsDuplicate(vector<int>& nums) { map<int, int> temp; for (int i = 0; i<nums.size(); i++) { if(temp.count(nums[i])!=0) { return true; } temp[nums[i]]=i; } return false; } };
其他思路:
public class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> appearedNum = new HashSet<Integer>(); for(int i = 0; i < nums.length; i++){ if(!appearedNum.contains(nums[i])){ appearedNum.add(nums[i]); } else return true; } return false; } }
class Solution { public: bool containsDuplicate(vector<int>& nums) { map<int, int> int_map; for (int i = 0; i<nums.size(); i++) { if(int_map.count(nums[i])){ return true; } int_map.insert(pair<int, int>(nums[i], i)); } return false; } };
</pre><p></p><p style="font-family:'Microsoft YaHei'; padding-top:0px; padding-bottom:0px; font-size:14px; color:rgb(63,63,63); line-height:30px">1、哈希法。用一个set记录所有已经出现过的数字,若出现冲突,则包含重复元素,若不冲突,则不包含重复元素。此方法的时间复杂度为O(n),空间复杂度为O(n)</p><p style="font-family:'Microsoft YaHei'; padding-top:0px; padding-bottom:0px; font-size:14px; color:rgb(63,63,63); line-height:30px"></p><pre code_snippet_id="674894" snippet_file_name="blog_20150525_1_2671361" name="code" class="cpp" style="font-family: 'Microsoft YaHei'; padding: 5px; background-color: rgb(246, 246, 246); border: 1px dotted rgb(170, 170, 170); color: rgb(63, 63, 63); line-height: 30px;"><pre name="code" class="cpp">class Solution { public: bool containsDuplicate(vector<int>& nums) { set<int> s; int len=nums.size(); for(int i=0; i<len; i++){ if(s.find(nums[i])==s.end()){ s.insert(nums[i]); }else{ return true; } } return false; } };
2、排序法。现将数组排序,然后扫描一次数组,若出现相邻的两个数相同,则包含重复元素,否则没有。此方法时间复杂度为O(nlogn),空间复杂度为O(1)【取决于排序方法】,不过用leetcode跑出来比1方法要快些,因为1方法建立set需要花费一些时间,也有测试用例的问题。
<pre name="code" class="cpp">class Solution { public: bool containsDuplicate(vector<int>& nums) { std::sort(nums.begin(), nums.end()); int len=nums.size(); for(int i=1; i<len; i++){ if(nums[i-1]==nums[i]){ return true; } } return false; } };
#include<iostream>
#include<map>
using namespace std;
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6,7, 8 };
int len = sizeof(a) / sizeof(a[0]);
map<int, int> m;
bool temp=false;
for (int i = 0; i < len; i++)
{
if (m.find(a[i]) == m.end())
m[a[i]]=i;
else
{
temp = true;
break;
}
}
cout << temp;
system("pause");
return 0;
}
相关文章推荐
- 1090. Highest Price in Supply Chain
- opatch报错OPatch failed with error code 2
- 1079. Total Sales of Supply Chain (25)
- VS2012 error C2664: “std::make_pair”:无法将左值绑定到右值引用
- How much training data do you need?
- BZOJ4152: [AMPPZ2014]The Captain
- junit.framework.AssertionFailedError: No tests found in JUnit.test at junit junit.framework.Asserti
- Project Euler 90:Cube digit pairs 立方体数字对
- HDU 1839 Delay Constrained Maximum Capacity Path(二分+SPFA)
- 逻辑卷管理PV/VG/LV RAID
- 归并排序与逆序对问题---(解题报告)POJ1804---Brainman
- 大话存储4——RAID磁盘阵列
- 《leetCode》:Climbing Stairs
- mailchimp教程 如何创建EDM电子邮箱营销模版
- error LNK1123: failure during conversion to COFF:问题解决
- vim背景色molokai配置
- leetcode Factorial Trailing Zeroes
- Paint apI介绍
- MAT1.5工具分析AIX6.1虚拟机堆转储文件
- 如何 “解决” WPF中空域问题(Airspace issuse)