zoukankan      html  css  js  c++  java
  • 217 Contains Duplicate

    // 使用哈希方法,用set 记录所有出现过的数字,如果没有冲突,则包含重复的元素。
    // 事件复杂度为O(n),空间复杂度O(n)

    class Solution {
    public:
    bool containsDuplicate(vector<int>& nums) {
    set<int>s;
    int len = nums.size();
    for(int i=0;i<len;i++)
    {
    if(s.find(nums[i])==s.end()){
    s.insert(nums[i]);
    }else{
    return true;
    }
    }
    return false;
    }
    };

     想用位图来做,但是超过了空间calloc 返回位nil

    bool containsDuplicate(int* nums, int numsSize) {
        int n = 1;
        n = n<<(27);
       
        int *BitMap = (int *)calloc(n*sizeof(int));
        printf("BitMap = %p
    ",BitMap);
        for(int i=0;i<numsSize;i++)
        {
            // 找出数字在位图中的坐标。(Block,表示哪一块int,bit表示32位 int中的哪一位)
            int temp = *(nums+i);
            int Block = temp/32;
            int bit = temp%32;
            printf("Block = %d ,bit = %d
    ",Block,bit);
            //根据坐标查找位图
            int targetBlock = *(BitMap+Block);
            printf("targetBlock = %d
    ",targetBlock);
            int place = targetBlock &(1<<bit);
            if(place)
            {
                return false;
            }else
            {
                targetBlock = targetBlock |(1<<bit);
                *(BitMap+Block) = targetBlock;
            }
        }
        return true;
    }
  • 相关阅读:
    Java运行环境(win10)
    maven封装jar包遇到的问题
    eclipse安装STS遇到的问题
    Redis IO多路复用的理解
    操作系统文章推荐
    jdk1.8新特性
    Maven笔记
    博主推荐
    MySQL文章推荐
    多线程文章推荐
  • 原文地址:https://www.cnblogs.com/pencilCool/p/4671072.html
Copyright © 2011-2022 走看看