[LeetCode]--217. Contains Duplicate

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])
                return true;
        }
        return false;
    }

我想着用List或者Set装着,然后判断在不在里面。但是全都超时了。

public boolean containsDuplicate1(int[] nums) {
        Set<Integer> unique = new HashSet<Integer>();
        for (int x : nums) {
            if (unique.contains(x))
                return true;
            else
                unique.add(x);
        }
        return false;
    }

    public boolean containsDuplicate(int[] nums) {
        if (nums.length == 0)
            return false;
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (list.contains(nums[i]))
                return true;
            else
                list.add(nums[i]);
        }
        return false;
    }
时间: 2024-10-18 14:10:15

[LeetCode]--217. Contains Duplicate的相关文章

leetcode 217 Contains Duplicate 数组中是否有重复的数字

 Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions 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 shoul

[LeetCode]--219. Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 想法:这题比较简单,可以直接定义一个长度最大为k的滑动窗口,用一个set维护窗口内的数字判断是否出现重复,使用两个指针

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode] Find Duplicate Subtrees 寻找重复树

Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any oneof them. Two trees are duplicate if they have the same structure with same node values. Example 1:  1 / \ 2 3 / /

[LeetCode] Contains Duplicate(II,III)

Contains Duplicate 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. 解题思路 用一个set保存数组中的值,如

[LeetCode] Number of 1 Bits &amp;amp; Reverse Integer - 整数问题系列

目录:1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历]3.Reverse Integer - 翻转整数 [int边界问题]4.Excel Sheet Column Number - Excel字符串转整数 [简单]5.Power of Two & Happy Number - 计算各个位数字 [%10 /10] 一.Number of 1 Bits  题目概述:Write a function t

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

leetcode 5 Longest Palindromic Substring--最长回文字符串

问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的.比如"a" , "aaabbaaa" 之前

[LeetCode]82.Remove Duplicates from Sorted List II

[题目] Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-