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.
解法一:
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】的更多相关文章
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 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 ...
- 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 ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [BZOJ2226]LCMSum
转化一下,$\sum\limits_{i=1}^n[i,n]=n\sum\limits_{i=1}^n\dfrac i{(i,n)}$ 枚举$d=(i,n)$,上式变为$n\sum\limits_{d ...
- 【AC自动机】【动态规划】hdu2296 Ring
题解:http://www.cnblogs.com/swm8023/archive/2012/08/08/2627535.html 要输出路径,价值最大优先,价值相同的取长度较小者,仍相同取字典序较小 ...
- 【费用流】bzoj3280 小R的烦恼
类似bzoj1221 http://www.cnblogs.com/autsky-jadek/p/4174087.html 只不过大学有多个,所以我们另开一个节点汇总所有'S->大学'的边,然后 ...
- [CF627D]Preorder Test
题目大意: 一个$n(n\le2\times10^5)$个结点的树,每个结点有一个权值$w_i$.可以任选一点为根,并选择一些结点交换其子结点的顺序,使得该树DFS序上第$m$个结点的权值最大.求最大 ...
- Linux下使用xargs得到字符串作为参数进行输出、awk得到字符串作为参数进行输出
背景: cat test.txt 实例: 1.xargs cat test.txt | xargs -i echo {} cat test.txt | xargs -I {} echo {} 大小写i ...
- Oracle TRCA 工具 说明 10046
本篇文章主要介绍了"Oracle TRCA 工具 说明 ",主要涉及到Oracle TRCA 工具 说明 方面的内容,对于Oracle TRCA 工具 说明 感兴趣的同学可以参考一 ...
- 科研不是比赛,而是一种对未知和完美的自我追求——跟邢波(Eric Xing)面对面聊科研
编者按:6月26日,2014年国际机器学习大会(ICML)在北京国际会议中心完美落幕.作为机器学习领域两大顶尖年会之一,这是 ICML大会30多年来首次来到中国和远东,在国内的机器学习界震动不小.身为 ...
- centos selinux学习记录
SELinux 全称 Security Enhanced Linux (安全强化 Linux),是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明 ...
- SpingMVC实现集合参数(Could not instantiate bean class [java.util.List])
需求,要求批量新增或者修改一个List,在springMVC中是不支持下面代码的写法: @RequestMapping(value = "/update", method = Re ...
- builder pattern
design patterns 结合书本和这个网站的这个系列的文章来看: https://www.tutorialspoint.com/design_pattern/builder_pattern.h ...