leetcode-Contains Duplicate

来源:互联网 发布:手机检测软件 编辑:程序博客网 时间:2024/06/05 15:04

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.


用哈希表,如果该元素已经存在于哈希表,说明重复。

public class Solution {    public boolean containsDuplicate(int[] nums) {        boolean flag=false;    HashMap<Integer, Integer> hm=new HashMap<Integer, Integer>();    for(int i=0;i<nums.length;i++)    {    if(!hm.containsKey(nums[i]))    hm.put(nums[i], 1);    else    {    flag=true;    break;    }    }    return flag;    }}



0 0
原创粉丝点击