Movatterモバイル変換


[0]ホーム

URL:


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

Commit4a90f37

Browse files
authored
Merge pull request6boris#54 from hiepndd/develop
Add solution problem 0217
2 parents73119a0 +e973cb5 commit4a90f37

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

‎src/0217.Contains-Duplicate/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[217. Contains Duplicate][title]
2+
3+
##Description
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, and it should return false if every element is distinct.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: [1,2,3,1]
13+
Output: true
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: [1,2,3,4]
20+
Output: false
21+
```
22+
23+
**Example 3:**
24+
25+
```
26+
Input: [1,1,1,3,3,4,3,2,4,2]
27+
Output: true
28+
```
29+
30+
[title]:https://leetcode.com/problems/contains-duplicate/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Solution
2+
3+
funccontainsDuplicate(nums []int)bool {
4+
disct:=make(map[int]bool,len(nums))
5+
6+
for_,number:=rangenums {
7+
ifdisct[number] {
8+
returntrue
9+
}else {
10+
disct[number]=true
11+
}
12+
}
13+
returnfalse
14+
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
funcTestConTainsDuplicate(t*testing.T) {
9+
cases:= []struct {
10+
namestring
11+
inputs []int
12+
expectsbool
13+
}{
14+
{"Test case 1", []int{1,2,3,1},true},
15+
{"Test case 2", []int{1,2,3,4},false},
16+
{"Test case 3", []int{1,1,1,3,3,4,3,2,4,2},true},
17+
}
18+
19+
for_,testcase:=rangecases {
20+
t.Run(testcase.name,func(t*testing.T) {
21+
result:=containsDuplicate(testcase.inputs)
22+
if!reflect.DeepEqual(result,testcase.expects) {
23+
t.Fatalf("expected: %v, but got: %v, with input: %v ",testcase.expects,result,testcase.inputs)
24+
}
25+
26+
})
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp