leetcode: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.

分析:先把数组排个序,然后遍历排序后的数组,查看相邻元素是否有重复,时间复杂度O(nlogn)。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {

    if( (nums.size() == 0) || (nums.size() == 1) ) return false;

    std::sort(std::begin(nums), std::end(nums));      //sort()是c++、java里对数组的元素进行排序的方法,包含于头文件algorithm。

    for(int i = 0; i < nums.size()-1; i++)
       if(nums[i] == nums[i+1]) return true;

    return false;
}
}; 

其他解法:(集和多集的区别是:set支持唯一键值,set中的值都是特定的,而且只出现一次;而multiset中可以出现副本键,同一值可以出现多次。)

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
       set<int> s(nums.begin(), nums.end());
       if (nums.size() == s.size()) return false;
       else return true;
    }
};

或者:

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

 或:sort the vector then traverse to find whether there are same value element continuesly:

class Solution {
public:
       bool containsDuplicate(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    if (nums.size() == 0){
        return false;
    }
    vector<int>::iterator it = nums.begin();
    int temp = *it;
    it++;
    for (; it != nums.end(); it++){
        if (*it == temp){
            return true;
        }
        temp = *it;
    }

    return false;
 }
};

或: step 1 Sort the vector
step2 use erase to remove the duplicate and compare the size of the vector

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        int pre = nums.size();

        sort(nums.begin(), nums.end());
        nums.erase(unique(nums.begin(), nums.end()), nums.end());

        int post = nums.size();

        return (post == pre) ? false : true;
        return false;
    }
};

或:use hash map

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int, int> hash;
        vector<int>::iterator it = nums.begin();

        for (; it != nums.end(); it++){
            if (hash.find(*it) != hash.end()){
                return true;
            }
            hash[*it] = 1;
        }

        return false;
    }
};

  

  

  

 

时间: 2024-10-06 20:00:39

leetcode: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

leetcode笔记: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. 二. 题目分析 题目的大意是,给定一个整数数组,推断数组中

leetcode笔记:Contains Duplicate III

一. 题目描述 Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 二. 题目分析 题目大意是,给定一个整数数组,判断其中是否存

LeetCode:Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

leetcode:Pascal&amp;#39;s Triangle

一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数的和值 须要注意的是,当行数为0时输出[[1]] 结果为一个二维数组,所以不难想到解决方式. 每层保存前一行的指针,然后当前行数据依据上一行来得到,每一个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1). 算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上仅仅须要二维数

LeetCode: Triangle 题解

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

Android问题集锦之四十八:Error:duplicate files during packaging of APK

端午小长假最后一天,大雨.只好在家里鼓捣自己喜欢的代码了. 在将dom4j.jar也加入fastjson测试程序中,编译出错,如下: Error:duplicate files during packaging of APK /home/linc/workspace/lab/FastjsonTestor/app/build/outputs/apk/app-debug-unaligned.apk Path in archive: META-INF/LICENSE.txt Origin 1: /ho