217. Contains Duplicate

Java

public class Solution {
    public boolean containsDuplicate(int[] nums) {
    Arrays.sort(nums);
    for(int i=0;i

Javascript

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function(nums) {
    nums.sort(function(a,b){return a-b});
    for(var i=0;i

最优解,用了hash,时间空间复杂度O(n)

Java

public class Solution {
    public  boolean containsDuplicate(int[] nums) {
         Set set = new HashSet();
         for(int i : nums)
             if(!set.add(i))// if there is same
                 return true; 
         return false;
     }
}

你可能感兴趣的:(217. Contains Duplicate)