[日常刷题]leetcode D37

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/83098060

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;
    }

小结

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

知识点

  • 分情况
  • 位运算

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83098060