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.

给定一个整形数组,从中找出是否存在重复元素。可以使用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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值