Skip to content

Commit 7653cb0

Browse files
authored
217 solved. (#54)
1 parent a2c7603 commit 7653cb0

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ continually updating 😃.
2626
* [200. Number of Islands](src/0200_number_of_island/number_of_island.go)   *`dfs;`*  *`bfs`*
2727
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
2828
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
29+
* [217. Contains Duplicate](src/0217_contains_duplicate/contains_duplicate.go)   *`map`*
2930
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
3031
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)   *`sliding window`*
3132
* [303. Range Sum Query - Immutable](src/0303_range_sum_query/rsqim.go)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
217. Contains Duplicate
3+
https://leetcode.com/problems/contains-duplicate/
4+
5+
Given an array of integers, find if the array contains any duplicates.
6+
7+
Your function should return true if any value appears at least twice in the array,
8+
and it should return false if every element is distinct.
9+
*/
10+
// time: 2019-01-07
11+
12+
package cd
13+
14+
// using HashTable
15+
// time complexity: O(n)
16+
// space complexity: O(n)
17+
func containsDuplicate(nums []int) bool {
18+
record := make(map[int]int)
19+
20+
for _, num := range nums {
21+
if _, ok := record[num]; !ok {
22+
record[num] = 1
23+
} else {
24+
return true
25+
}
26+
}
27+
return false
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cd
2+
3+
import "testing"
4+
5+
func TestContainsDuplicate(t *testing.T) {
6+
testCases := [][]int{
7+
{1, 2, 3, 1},
8+
{1, 2, 3, 4},
9+
}
10+
expected := []bool{true, false}
11+
12+
for index, nums := range testCases {
13+
if res := containsDuplicate(nums); res != expected[index] {
14+
t.Errorf("expected %t, got %t", expected[index], res)
15+
}
16+
}
17+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
|0206|[206. Reverse Linked List](0206_reverse_linked_list/reverse_linked_list.go)|Easy|*`linked list`*|
6969
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
7070
|0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*|
71+
|0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|
7172
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
7273
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
7374
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|

0 commit comments

Comments
 (0)