题目链接:LeetCode 217—Contains Duplicate
题目大意:即判断一个数组中是否有重复元素
采用一个Set容器即可轻松解决。
实现代码:
public class Problem217 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set =new HashSet<Integer>();
for(int i:nums){
if(set.contains(i)) return true;
else set.add(i);
}
return false;
}
}

本文介绍如何使用HashSet容器解决LeetCode217题目——判断数组中是否存在重复元素的问题。通过遍历数组并将元素存入HashSet,若发现已存在元素则返回true,否则返回false。

被折叠的 条评论
为什么被折叠?



