Given an array of integers, find if the array contains any duplicates. 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.
先排序,然后判断。Accept。
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1])

该博客探讨了LeetCode第217题的解决方案,即检查一个整数数组是否包含重复元素。博主尝试通过排序和使用集合数据结构来解决,但遇到了超时问题。
订阅专栏 解锁全文
174

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



