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.

代码:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章