首页 > 其他 > 详细

Leetcode 217 Contains Duplicate

时间:2015-06-30 12:14:59      阅读:79      评论:0      收藏: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.

也可使用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;
}

 

Leetcode 217 Contains Duplicate

原文:http://www.cnblogs.com/lilixu/p/4609982.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!