Leetcode - Contains Duplicate

Paste_Image.png

My code:

import java.util.HashSet;

public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if (nums == null || nums.length == 0)
            return false;
        HashSet<Integer> h = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (h.contains(nums[i]))
                return true;
            else
                h.add(nums[i]);
        }
        return false;
    }
}

My test result:

就用一个hash table搞定了。 easy

**
总结:还是多关注自己的事,别太为了别人的事而大动肝火,不值得。
简历要开始制作了!!!
**
Anyway, Good luck, Richardo!

public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if (nums == null || nums.length == 0)
        return false;
        HashSet<Integer> helper = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (helper.contains(nums[i]))
                return true;
            else
                helper.add(nums[i]);
        }
        return false;
    }
}

代码应该差不多,就是一个HashSet 解决问题。

Anyway, Good luck, Richardo!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容