[日常刷题]leetcode D37

本文详细解析了LeetCode上的两道题目,475.Heaters和476.NumberComplement。对于Heaters问题,通过计算每个房子到最近加热器的距离来确定最小加热半径;对于NumberComplement问题,介绍了两种解决方案,一种是通过二进制字符串转换和遍历求值,另一种是利用位运算快速得出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

475. Heaters

Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.

Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.

So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.

Note:

  1. Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
  2. Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
  3. As long as a house is in the heaters’ warm radius range, it can be warmed.
  4. All the heaters follow your radius standard and the warm radius will the same.
    Example 1:
Input: [1,2,3],[2]
Output: 1
Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.

Example 2:

Input: [1,2,3,4],[1,4]
Output: 1
Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.

Solution in C++:

关键点:

  • 遍历目标选择

思路:

  • 这个题做了一个小时,由于遍历对象错误而做不下去,去做下一题了,然后后来回来看了人家的思路,发现自己的应该也是对了,就是没有简介,分类讨论的情况搞的太多了,然后遍历对象也选错了,选择了heaters。
  • 这边主要的思路就是计算所有房子和离自己最近的heater的距离,然后再取最大值
int findRadius(vector<int>& houses, vector<int>& heaters) {
        size_t hosize = houses.size();
        size_t hesize = heaters.size();
        if (hosize == 0)
            return 0;
        
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        
        int i = 0, r = 0;
        for(auto house : houses){
            while(i+1 < hesize && house >= ceil((heaters[i+1] + heaters[i])/2.0))
                ++i;
            r = max(r, abs(heaters[i] - house));
        }
        
        return r;
    }

476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

Solution in C++:

关键点:

  • 位运算

思路:

  • 我的方法主要是暴力,先把num转换为对应的二进制,然后遍历字符串求值,只不过将其中的1当作0,0当作1.
  • 这边主要介绍一下我在discuss里面看到的一个比较简介的解决方法。这里主要是获取一个和num一样位数的全1数,因为 0 ^ 1 = 1; 1^1 = 0刚好符合题意要求,所以当t > num时停止,则t-1是为与num相同位数的全1序列。

方法一:暴力

string tobinary(int num){
        string result;
        
        while(num){
            result = to_string(num % 2) + result;
            num /= 2;
        }
        return result;
    }
    
    int findComplement(int num) {
        int re = 0;
        if (num == 0)
            return 1;
        else{
            string result = tobinary(num);
            size_t size = result.size();
            for(int i = 0; i < size; ++i){
                int tmp = (result[i] - '0') ^ 1;
                re += tmp * pow(2, size - 1 - i);
            }
        }
        
        return re;
    }

方法二:位运算

int findComplement(int num) {
        long long t=1;
        while(t<=num)
            t=t<<1;
        return (t-1)^num;
    }

小结

哎,今天也是挫败感不低啊,不知道是不是最近事情变多了,然后也有点怀疑这样不复习数据结构和算法的刷题策略是不是有错误,但是又觉得刷了这么久了不想轻易就放弃。状态太不好了。。。

知识点

  • 分情况
  • 位运算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值