每天一道leetcode之入门

Day7. Contains Duplicate(217)

2017-11-09  本文已影响0人  前端伊始

问题描述
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.
思路:刚开始就用了双层for循环,超时了

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function(nums) {
    nums.sort();
    for(var i = 0; i < nums.length-1; i++){
        if(nums[i] == nums[i+1]){
            return true;
        }
    }
    return false;
};
上一篇下一篇

猜你喜欢

热点阅读