217:存在重复元素
This commit is contained in:
parent
4584dd083e
commit
25086a91b3
54
src/main/java/leetcode/editor/cn/ContainsDuplicate.java
Normal file
54
src/main/java/leetcode/editor/cn/ContainsDuplicate.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
//给定一个整数数组,判断是否存在重复元素。
|
||||||
|
//
|
||||||
|
// 如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: [1,2,3,1]
|
||||||
|
//输出: true
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: [1,2,3,4]
|
||||||
|
//输出: false
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: [1,1,1,3,3,4,3,2,4,2]
|
||||||
|
//输出: true
|
||||||
|
// Related Topics 数组 哈希表 排序
|
||||||
|
// 👍 407 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
//217:存在重复元素
|
||||||
|
class ContainsDuplicate{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ContainsDuplicate().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean containsDuplicate(int[] nums) {
|
||||||
|
Set<Integer> set = new HashSet<>();
|
||||||
|
for (int num : nums) {
|
||||||
|
if (set.contains(num)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
set.add(num);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
24
src/main/java/leetcode/editor/cn/ContainsDuplicate.md
Normal file
24
src/main/java/leetcode/editor/cn/ContainsDuplicate.md
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<p>给定一个整数数组,判断是否存在重复元素。</p>
|
||||||
|
|
||||||
|
<p>如果存在一值在数组中出现至少两次,函数返回 <code>true</code> 。如果数组中每个元素都不相同,则返回 <code>false</code> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong> [1,2,3,1]
|
||||||
|
<strong>输出:</strong> true</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入: </strong>[1,2,3,4]
|
||||||
|
<strong>输出:</strong> false</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入: </strong>[1,1,1,3,3,4,3,2,4,2]
|
||||||
|
<strong>输出:</strong> true</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>排序</li></div></div>\n<div><li>👍 407</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user