zoukankan      html  css  js  c++  java
  • 217. Contains Duplicate【easy】

    217. Contains Duplicate【easy】

    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 class Solution {
     2 public:
     3     bool containsDuplicate(vector<int>& nums) {
     4         if (nums.empty()) {
     5             return false;
     6         }
     7         
     8         unordered_map<int, int> my_map;
     9         for (int i = 0; i < nums.size(); ++i) {
    10             if (++my_map[nums[i]] > 1) {
    11                 return true;
    12             }
    13         }
    14         
    15         return false;
    16     }
    17 };

    解法二:

    1 class Solution {
    2 public:
    3     bool containsDuplicate(vector<int>& nums) {
    4         return nums.size() > set<int>(nums.begin(), nums.end()).size();        
    5     }
    6 };

    参考@chammika 的代码

    解法三:

     1 public boolean containsDuplicate(int[] nums) {
     2     for(int i = 0; i < nums.length; i++) {
     3         for(int j = i + 1; j < nums.length; j++) {
     4             if(nums[i] == nums[j]) {
     5                 return true;
     6             }
     7         }
     8      }
     9     return false;
    10 }

    Time complexity: O(N^2), memory: O(1)

    The naive approach would be to run a iteration for each element and see whether a duplicate value can be found: this results in O(N^2) time complexity.

    解法四:

    1 public boolean containsDuplicate(int[] nums) {
    2     Arrays.sort(nums);
    3     for(int ind = 1; ind < nums.length; ind++) {
    4         if(nums[ind] == nums[ind - 1]) {
    5             return true;
    6         }
    7     }
    8     return false;
    9 }

    Time complexity: O(N lg N), memory: O(1) - not counting the memory used by sort

    Since it is trivial task to find duplicates in sorted array, we can sort it as the first step of the algorithm and then search for consecutive duplicates.

    解法五:

     1 public boolean containsDuplicate(int[] nums) {
     2 
     3     final Set<Integer> distinct = new HashSet<Integer>();
     4     for(int num : nums) {
     5         if(distinct.contains(num)) {
     6             return true;
     7         }
     8         distinct.add(num);
     9     }
    10     return false;
    11 }

    Time complexity: O(N), memory: O(N)

    Finally we can used a well known data structure hash table that will help us to identify whether an element has been previously encountered in the array.

    解法三、四、五均参考@jmnarloch 的代码

  • 相关阅读:
    EXTJS4.2——0.简介
    什么是JDBC?
    Applet和普通的Java应用程序有什么区别?
    final关键字、finally代码块和finalize()方法有什么区别?
    throw和throws有什么区别
    Java中的两种异常类型是什么?他们有什么区别?
    Java堆的结构是什么样子的?什么是堆中的永久代(Perm Gen space)?
    flash插件
    算法复杂度分析中的符号(大Θ符号、大Ο符号...)
    Enumeration接口和Iterator接口的区别有哪些?
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7623413.html
Copyright © 2011-2022 走看看