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. 【Trie+DP】BZOJ1212-[HNOI2004]L语言

    [题目大意]给出字典和文章,求出文章能够被理解的最长前缀. [思路] 1A……!先用文章建立一棵Trie树,然后对于文章进行DP.f[i]表示文章中长度为i的前缀能否被理解,如果f[i]能理解,顺着下 ...

  2. Atom | 报错 Cannot load the system dictionary for zh-CN的解决办法

    文章目录 问题描述 推荐阅读 查找问题所在 解决方案 (二选一) 问题描述 最近这款优秀的编辑器 atom,报错 Cannot load the system dictionary for zh-CN ...

  3. VHDL硬件描述语言实现数字钟

    --VHDL上机的一个作业,程序太长实验报告册上写不下了.于是就在博客上留一份吧.LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOG ...

  4. Android Studio 下使用git -- 个人,本地版本控制

    第一步:下载安装git 下载地址 : https://git-scm.com/downloads 第二步:Android Studio 下配置git路径. 配置之后,Test弹出如下成功的提示即可. ...

  5. Oracle查询库中记录数大于2千万的所有表

    Oracle查询库中记录数大于2千万的所有表 假如当前用户拥有select any table权限,则可以使用下列sql语句: select table_name, num_rows from dba ...

  6. C# log4net打不出日志 (IIS项目)

    配置文件都配了,引用也引用了,调用也是对的,网上找博客也找不到,疯掉了. 后来腆着脸问了一个前辈,他告诉我的,添完就好了. 给项目的这个文件,添加这行代码: [assembly: log4net.Co ...

  7. linux下GPRS模块的应用程序

    ---------------------------------------------------------------------------------------------------- ...

  8. 基于3D Vision眼镜的OSG立体显示 【转】

    http://blog.csdn.net/qq_20038925/article/details/50510565 OSG 立体显示 3D Vision眼镜:所实现的是被动立体. 1.本人最近在做os ...

  9. maven 打包不全(xml,properties文件没打进包)解决方案

    在pom.xml的build标签中加入以下代码即可 <build> <resources>            <resource>               ...

  10. Quartz与Spring的整合使用

    之前说到过Quartz的基本使用(猛戳这里看文章).在实际使用中,我们一般会将定时任务交由spring容器来管理.所以今天我们来说说Quartz与spring的整合. 咱们还是依照Quartz的三大元 ...