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.

解法一:

 class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.empty()) {
return false;
} unordered_map<int, int> my_map;
for (int i = ; i < nums.size(); ++i) {
if (++my_map[nums[i]] > ) {
return true;
}
} return false;
}
};

解法二:

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

参考@chammika 的代码

解法三:

 public boolean containsDuplicate(int[] nums) {
for(int i = 0; i < nums.length; i++) {
for(int j = i + 1; j < nums.length; j++) {
if(nums[i] == nums[j]) {
return true;
}
}
}
return false;
}

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.

解法四:

 public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for(int ind = 1; ind < nums.length; ind++) {
if(nums[ind] == nums[ind - 1]) {
return true;
}
}
return false;
}

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.

解法五:

 public boolean containsDuplicate(int[] nums) {

     final Set<Integer> distinct = new HashSet<Integer>();
for(int num : nums) {
if(distinct.contains(num)) {
return true;
}
distinct.add(num);
}
return false;
}

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 的代码

217. Contains Duplicate【easy】的更多相关文章

  1. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  2. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  3. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  4. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  5. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  6. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  7. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  8. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  9. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

随机推荐

  1. 【匈牙利算法】BZOJ1059-[ZJOI2007]矩阵游戏

    [题目大意] 给出一个局部染色的矩阵,问能否通过交换行或者列使得最后又一条对角线全部被染色过? [思路] 无论如何交换,同一行的格子依然在同一行,同一列的格子依然在同一列.所以只需找出n个行号列号均不 ...

  2. Problem I: 零起点学算法30——输出四位完全平方数

    #include<stdio.h> int main() { int a,b,c,d,s,i; ;i<;i++){ s=i*i; a=s/; b=s%/; c=s%/; d=s%; ...

  3. Java高级架构师(一)第05节:TortoiseGit的本地使用

  4. URL 和URI的区别

    1.统一资源标志符URI就是在某一规则下能把一个资源独一无二地标识出来. URL:(Uniform/Universal Resource Locator 的缩写,统一资源定位符). URI:(Unif ...

  5. NHibernate官方文档中文版--只读实体类型(Read-only entities)

    重点 NHIbernate处理只读对象的方式可能和你在别处看到的不同.不正确的使用方式可能造成不可预料的结果. 当一个实体是只读的时候: NHIbernate不会对实体的简单属性和单向关联数据检查数据 ...

  6. Spring-data-jpa 之Specification in的用法

    1.一个简单的 Predicate 的示例 构建简单的Predicate示例: Predicate p1=cb.like(root.get(“name”).as(String.class), “%”+ ...

  7. WebLogic MBean Monitor

    weblogic server提供了一个dashboard让我们对mbean进行图形化的展现和分析,地址是 http://localhost:7001/console/dashboard 但有时候总是 ...

  8. nginx+lua+redis

    git clone --branch master https://github.com/openresty/lua-resty-redis.git yum install openssl opens ...

  9. 手把手教你调试Entity Framework 6源码

    0  摘要 本文讲述在Visual Studio 2013(VS 2013)下调试Entity Framework 6(EF 6)源码的配置过程.原则上,VS 2012也适用. 之前打算编写<E ...

  10. iOS:地图笔记

    地图笔记 01. CLLocation -------------------------------------------------------- CLLocationManager 定位管理者 ...