[LeetCode]10. 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.

解法1:暴力破解,时间复杂度O(n^2)。从头至尾扫描数组,将当前值与数组其他值一一进行比较,若有相同,则返回true;否则直到数组末尾仍没找到重复值,则返回false。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        int n = nums.size();
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                if(nums[i] == nums[j])
                    return true;
            }
        }
        return false;
    }
};

解法2:先将数组按升序排序,然后比较相邻的两个元素是否有相等的即可。因为一般交换排序最快可以达到O(nlogn),最后扫描一趟耗时O(n),因此时间复杂度为O(nlogn)。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        int n = nums.size();
        sort(nums.begin(), nums.end());

        for(int i = 0; i < n - 1; i++)
        {
            if(nums[i] == nums[i + 1])
                return true;
        }
        return false;
    }
};

解法3:由解法2引申想到空间换时间的方法,桶排序或者计数排序等都可以使得时间复杂度降到O(n),因此整个算法时间复杂度为O(n)。但是此法有个缺点:输入数组的元素范围需在一定范围内,并且已知这个范围。一个好的方法即是使用STL的map,也可以自己实现Hash表和相应算法。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        int n = nums.size();
        map<int, int> mii;
        for(int i = 0; i < n; i++)
        {
            int cnt = ++mii[nums[i]];
            if(cnt > 1)
                return true;
        }

        return false;
    }
};
时间: 2024-07-29 16:53:07

[LeetCode]10. Contains Duplicate重复检测的相关文章

LeetCode:Contains Duplicate - 判断数组内是否有重复元素

1.题目名称 Contains Duplicate(判断数组内是否有重复元素) 2.题目地址 https://leetcode.com/problems/contains-duplicate/ 3.题目内容 英文: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

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nu

微软Dynamics 2015 数据管理 之 重复检测 功能

重复检测的规则有很多. 本人把所有的规则编辑出来,大家可以看看. 规则名称 状态描述 基本记录类型 匹配记录类型 Accounts with the same e-mail address 已发布 客户 客户 Contacts with the same e-mail address 已发布 联系人 联系人 Contacts with the same first name and last name 已发布 联系人 联系人 Leads with the same e-mail address

MAC地址记录与重复检测系统

一.通信模块如WiFi.Zigbee都会有唯一的MAC地址,这些模块在出厂前需要一套系统来确保唯一性. 此套MAC地址记录与重复检测系统已经经过KK级的出货验证,难有漏网之鱼. 二.系统设计思路: 客户端程序读取模块MAC地址,然后去pass数据库中寻找是否已经存在,如果不存在,则将此MAC存到pass数据库中,显示PASS,如果已经存在,证明已经生产过了,有重复,则将此MAC存到repeat数据库中. 三.系统实施步骤: 1,搭建一台windows2003服务器,外接交换机,通过网线与产线上几

Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entir

LeetCode 219. Contains Duplicate II (包含重复项之二)

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 题目标签:Array, Hash Table 题目给了我们一个nums array 和一个 k, 让

LeetCode 217. 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. 题目标签:Array, Hash Table 题目给了我们一个nums arr

leetcode 217 Contains Duplicate 数组中是否有重复的数字

 Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions 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 shoul

LeetCode 217 Contains Duplicate(包含重复数字)(Vector、hash)

翻译 给定一个整型数字数组,找出这个数组是否包含任何重复内容. 如果任何值出现了至少两次,那么返回真(true), 如果每个值都是互不相同的,那么返回假(false). 原文 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 s