Skip to content

Commit a690298

Browse files
committed
feat: commit solution 217
1 parent 7e7ad5c commit a690298

File tree

6 files changed

+103
-3
lines changed

6 files changed

+103
-3
lines changed

_sidebar.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
- [83.删除排序链表中的重复元素 ✅](solution/1-99/0083.remove-duplicates-from-sorted-list/)
2323
- [88.合并两个有序数组 ✅](solution/1-99/0088.merge-sorted-array/)
2424
- [125.验证回文串 ✅](solution/100-199/0125.valid-palindrome/)
25+
- [217.存在重复元素 ✅](solution/200-299/0217.contains-duplicate/)
26+
- [219.存在重复元素 II ✅](solution/200-299/0219.contains-duplicate-ii/)
27+
2528

2629

2730

index-tags.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
3131
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
3232
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
33-
33+
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
34+
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
3435

3536
#### **哈希表**
3637

3738
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
3839
| --- | --- | --- | --- | --- |
3940
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
41+
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
42+
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
4043

4144
#### ****
4245

index-type.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> |
7070
| [205](https://leetcode-cn.com/problems/isomorphic-strings) | [同构字符串](/solution/200-299/0205.isomorphic-strings/) | `哈希表` | <font color=green>简单</font> |
7171
| [206](https://leetcode-cn.com/problems/reverse-linked-list) | [反转链表](/solution/200-299/0206.reverse-linked-list/) | `链表` | <font color=green>简单</font> |
72-
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> |
73-
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> |
72+
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
73+
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
7474
| [225](https://leetcode-cn.com/problems/implement-stack-using-queues) | [用队列实现栈](/solution/200-299/0225.implement-stack-using-queues/) | ``,`设计` | <font color=green>简单</font> |
7575
| [226](https://leetcode-cn.com/problems/invert-binary-tree) | [翻转二叉树](/solution/200-299/0226.invert-binary-tree/) | `` | <font color=green>简单</font> |
7676
| [231](https://leetcode-cn.com/problems/power-of-two) | [2的幂](/solution/200-299/0231.power-of-two/) | `位运算`,`数学` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [217.存在重复元素](https://leetcode-cn.com/problems/contains-duplicate/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个整数数组,判断是否存在重复元素。</p>
7+
8+
<p>如果任意一值在数组中出现至少两次,函数返回 <code>true</code> 。如果数组中每个元素都不相同,则返回 <code>false</code> 。</p>
9+
10+
<p>&nbsp;</p>
11+
12+
<p><strong>示例 1:</strong></p>
13+
14+
<pre><strong>输入:</strong> [1,2,3,1]
15+
<strong>输出:</strong> true</pre>
16+
17+
<p><strong>示例 2:</strong></p>
18+
19+
<pre><strong>输入: </strong>[1,2,3,4]
20+
<strong>输出:</strong> false</pre>
21+
22+
<p><strong>示例&nbsp;3:</strong></p>
23+
24+
<pre><strong>输入: </strong>[1,1,1,3,3,4,3,2,4,2]
25+
<strong>输出:</strong> true</pre>
26+
</div>
27+
28+
### 解题思路
29+
30+
1. 利用hash表来实现
31+
32+
### 代码实现
33+
34+
<!-- tabs:start -->
35+
36+
#### **Golang**
37+
```go
38+
func containsDuplicate(nums []int) bool {
39+
var numMap = make(map[int]int, 0)
40+
for k, v := range nums {
41+
if _, ok := numMap[v]; ok {
42+
return true
43+
}
44+
numMap[v] = k
45+
}
46+
return false
47+
}
48+
```
49+
50+
51+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=217 lang=golang
5+
*
6+
* [217] 存在重复元素
7+
*/
8+
9+
// @lc code=start
10+
func containsDuplicate(nums []int) bool {
11+
var numMap = make(map[int]int, 0)
12+
for k, v := range nums {
13+
if _, ok := numMap[v]; ok {
14+
return true
15+
}
16+
numMap[v] = k
17+
}
18+
return false
19+
}
20+
21+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPlusOne(t *testing.T) {
8+
var nums []int
9+
var ret bool
10+
11+
nums = []int{1, 2, 3, 1}
12+
ret = true
13+
if ret != containsDuplicate(nums) {
14+
t.Fatalf("case fails: %v\n", ret)
15+
}
16+
17+
nums = []int{1, 2, 3, 4}
18+
ret = false
19+
if ret != containsDuplicate(nums) {
20+
t.Fatalf("case fails: %v\n", ret)
21+
}
22+
}

0 commit comments

Comments
 (0)