Skip to content

Commit 794cf54

Browse files
committed
solve problem Contains Duplicate
1 parent ba6683e commit 794cf54

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ All solutions will be accepted!
4646
|231|[Power Of Two](https://leetcode-cn.com/problems/power-of-two/description/)|[java/py/js](./algorithms/PowerOfTwo)|Easy|
4747
|38|[Count And Say](https://leetcode-cn.com/problems/count-and-say/description/)|[java/py/js](./algorithms/CountAndSay)|Easy|
4848
|383|[Ransom Note](https://leetcode-cn.com/problems/ransom-note/description/)|[java/py/js](./algorithms/RansomNote)|Easy|
49+
|217|[Contains Duplicate](https://leetcode-cn.com/problems/contains-duplicate/description/)|[java/py/js](./algorithms/ContainsDuplicate)|Easy|
4950

5051
# Database
5152
|#|Title|Solution|Difficulty|
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Contains Duplicate
2+
This problem is easy to solve by hashmap
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
Map<Integer, Boolean> intMap = new HashMap<Integer, Boolean>();
4+
for (int num : nums) {
5+
if (intMap.get(num) == null) {
6+
intMap.put(num, true);
7+
} else {
8+
return true;
9+
}
10+
}
11+
return false;
12+
}
13+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
let intMap = {}
7+
for (let i = 0; i < nums.length; i++) {
8+
let num = nums[i]
9+
if (intMap[num] === undefined) {
10+
intMap[num] = true
11+
} else {
12+
return true
13+
}
14+
}
15+
return false
16+
};
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def containsDuplicate(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: bool
6+
"""
7+
int_map = {}
8+
for num in nums:
9+
if int_map.get(num) == None:
10+
int_map[num] = True
11+
else:
12+
return True
13+
return False

0 commit comments

Comments
 (0)