Leetcode 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.

也可使用set,hash

def contains_duplicate(nums)
    nums != nums.uniq
end

数不太大的时候也可使用二进制操作方法,但在这题并不适用

bool containsDuplicate(int* nums, int numsSize) {
    int i = 0;
    int checker = 0;
    for (i=0;i<numsSize;i++){
        if ((checker & (1 << nums[i])) > 0) return true;
        checker |= (1 << nums[i]);
    }
    return false;
}

 

posted @ 2015-06-30 11:49  lilixu  阅读(135)  评论(0编辑  收藏  举报