Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

217. Contains Duplicate #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from 217-Contains-Duplicate
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ continually updating 😃.
* [200. Number of Islands](src/0200_number_of_island/number_of_island.go)   *`dfs;`*  *`bfs`*
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
* [217. Contains Duplicate](src/0217_contains_duplicate/contains_duplicate.go)   *`map`*
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)   *`sliding window`*
* [303. Range Sum Query - Immutable](src/0303_range_sum_query/rsqim.go)
Expand Down
28 changes: 28 additions & 0 deletions src/0217_contains_duplicate/contains_duplicate.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
217. Contains Duplicate
https://leetcode.com/problems/contains-duplicate/

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.
*/
// time: 2019年01月07日

package cd

// using HashTable
// time complexity: O(n)
// space complexity: O(n)
func containsDuplicate(nums []int) bool {
record := make(map[int]int)

for _, num := range nums {
if _, ok := record[num]; !ok {
record[num] = 1
} else {
return true
}
}
return false
}
17 changes: 17 additions & 0 deletions src/0217_contains_duplicate/contains_duplicate_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cd

import "testing"

func TestContainsDuplicate(t *testing.T) {
testCases := [][]int{
{1, 2, 3, 1},
{1, 2, 3, 4},
}
expected := []bool{true, false}

for index, nums := range testCases {
if res := containsDuplicate(nums); res != expected[index] {
t.Errorf("expected %t, got %t", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
|0206|[206. Reverse Linked List](0206_reverse_linked_list/reverse_linked_list.go)|Easy|*`linked list`*|
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
|0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*|
|0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
Expand Down

AltStyle によって変換されたページ (->オリジナル) /