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

Commit 1214c29

Browse files
authored
219 solved. (#55)
1 parent 7653cb0 commit 1214c29

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ continually updating 😃.
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`*
2929
* [217. Contains Duplicate](src/0217_contains_duplicate/contains_duplicate.go)   *`map`*
30+
* [219. Contains Duplicate II](src/0219_contains_duplicate_2/contains_duplicate_2.go)   *`map`*
3031
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
3132
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)   *`sliding window`*
3233
* [303. Range Sum Query - Immutable](src/0303_range_sum_query/rsqim.go)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
219. Contains Duplicate II
3+
https://leetcode.com/problems/contains-duplicate-ii/
4+
5+
Given an array of integers and an integer k,
6+
find out whether there are two distinct indices i and j in the array
7+
such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
8+
*/
9+
// time: 2019年01月07日
10+
11+
package cond
12+
13+
// using hash table
14+
// time complexity: O(n)
15+
// space complexity: O(n)
16+
func containsNearbyDuplicate(nums []int, k int) bool {
17+
record := make(map[int]int, len(nums))
18+
for i, v := range nums {
19+
if j, ok := record[v]; ok {
20+
if i-j <= k {
21+
return true
22+
}
23+
}
24+
record[v] = i
25+
}
26+
return false
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cond
2+
3+
import "testing"
4+
5+
func TestContainsNearbyDuplicate(t *testing.T) {
6+
testCases := [][]int{
7+
{1, 2, 3, 1},
8+
{1, 0, 1, 1},
9+
{1, 2, 3, 1, 2, 3},
10+
}
11+
ks := []int{3, 1, 2}
12+
expected := []bool{true, true, false}
13+
14+
for index, nums := range testCases {
15+
if res := containsNearbyDuplicate(nums, ks[index]); res != expected[index] {
16+
t.Errorf("expected %t, got %t", expected[index], res)
17+
}
18+
}
19+
}

‎src/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
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`*|
7171
|0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|
72+
|0219|[219. Contains Duplicate II](0219_contains_duplicate_2/contains_duplicate_2.go)|Easy|*`map`*|
7273
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
7374
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
7475
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|

0 commit comments

Comments
(0)

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