[日常刷题]leetcode D36

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

458. Poor Pigs

There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket contains the poison within one hour.

Answer this question, and write an algorithm for the follow-up general case.

Follow-up:

If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x) you need to figure out the “poison” bucket within p minutes? There is exact one bucket with poison.

Solution in C++:

关键点:

  • 问题转换

思路:

  • 我自己昨天看了题没什么思路,就放今天做了,然后今天又是从后往前做的,以为自己能够找到点规律,但是没搞几步思路和对题意理解有问题,就放弃了,直接看了解析。叹为观止!!!两个解决方案最后殊途同归就是理解不一样,都很棒
    幂的方法
    log方法

log方法

int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        int state = floor(minutesToTest / minutesToDie) + 1;
        return ceil(log(buckets) / log(state));
    }

459. Repeated Substring Pattern

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.

Example 2:

Input: "aba"
Output: False

Example 3:

Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

Solution in C++:

关键点:

  • 顺序不可变

思路:

  • “ababab” true “ababba” false.这里的思路是采取n substring组成s,所以sub的长度一定是s.size的因子,就求出其所有因子,然后以sub的长度再遍历s,看是否都与sub相同;特殊情况size = 0 || 1排除
vector<int> divisor(int num){
        vector<int> divisors;
        for(int i = 1; i <= num/2; ++i)
            if (num % i == 0)
                divisors.push_back(i);
        return divisors;
    }
    
    bool repeatedSubstringPattern(string s) {
        size_t size = s.size();
        if (size == 0 || size == 1)
            return false;
        
        vector<int> divisors = divisor(size);
        for(auto d : divisors){
            string sub = s.substr(0, d);
            int index = d;
            while(index < size){
                if (s.substr(index, d) != sub)
                    break;
                index += d;
            }
            if (index >= size)
                return true;
        }
        return false;
    }

461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 2 31 2^{31} .

Example:

Input: x = 1, y = 4

Output: 2

Explanation:
1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

The above arrows point to positions where the corresponding bits are different.

Solution in C++:

关键点:

  • 不同位数

思路:

  • 按位扫描,先取出x y的对应位,然后用异或操作,不同即+1,相同就跳过,最后和即为所求
int hammingDistance(int x, int y) {
        long long bit = 1;
        int i = 0;
        int result = 0;
        while(i < 32){
            int tmpx = x & bit;
            int tmpy = y & bit;
            if (tmpx ^ tmpy)
                ++result;
            bit <<= 1;
            ++i;
        }
        return result;
    }

463. Island Perimeter

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.

Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).

The island doesn’t have “lakes” (water inside that isn’t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.
Example:

Input:
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

Output: 16

Explanation: The perimeter is the 16 yellow stripes in the image below:

在这里插入图片描述

Solution in C++:

关键点:

  • 方向数组

思路:

  • 看图很明显是与水相邻的边需要计入周长内,即四个方向扫描时需要将相邻为0的计入,遍历即可。
bool isWater(int x, int y, int size1, int size2, vector<vector<int>>& grid){
        // 边界判断
        if (x < 0 || x >= size1 || y < 0 || y >= size2 || grid[x][y] == 0)
            return true;
        else
            return false;
    }
    
    int islandPerimeter(vector<vector<int>>& grid) {
        size_t size1 = grid.size();
        size_t size2 = grid[0].size();
        int perimeter = 0;
        // 方向数组
        vector<vector<int>> dxy = {{1,0},     // 右
                                  {-1,0},    // 左
                                  {0,1},     // 上
                                  {0,-1}};   // 下
    
        for(int i = 0; i < size1; ++i){
            for(int j = 0; j < size2; ++j){
                if (grid[i][j] == 0)
                    continue;
                // 判断上下左右是否有island
                int dx = 0;
                while(dx < 4){
                    if (isWater(i+dxy[dx][0], j+dxy[dx][1], size1, size2, grid))
                        ++perimeter;
                    ++dx;
                }   
            }
        }
    return perimeter;
    }

小结

今天又是不小的打击,做出来的题目也没有感觉很有成就感(因为方法不好代码也写的不优美),主要有些题的思路确实没有,比如Poor Pig,虽然不知道easy难度并且AC率还这么高是为什么,难道都是不自己写直接看别人解答AC的吗,最近几天都因为AC率让我怀疑自己。

知识点

  • 方向数组
  • 进制表示 等价转换能力

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83067294
今日推荐