[日常刷题]leetcode D36

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} 231.

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率让我怀疑自己。

知识点

  • 方向数组
  • 进制表示 等价转换能力
Python 是一种流行的高级编程语言,因其简洁易读的语法和广泛的应用领域而受到开发者喜爱。LeetCode 是一个在线编程平台,专门用于算法和技术面试的准备,提供了大量的编程目,包括数据结构、算法、系统设计等,常用于提升程序员的编程能力和解决实际问的能力。 在 Python 中刷 LeetCode 目通常涉及以下步骤: 1. **注册账户**:首先在 LeetCode 官网 (https://leetcode.com/) 注册一个账号,这样你可以跟踪你的进度和提交的代码。 2. **选择语言**:登录后,在个人主页设置中选择 Python 作为主要编程语言。 3. **学习和理解目**:阅读目描述,确保你理解问的要求和预期输出。目通常附有输入示例和预期输出,可以帮助你初始化思考。 4. **编写代码**:使用 Python 编写解决方案,LeetCode 提供了一个在线编辑器,支持实时预览和运行结果。 5. **测试和调试**:使用给出的测试用例来测试你的代码,确保它能够正确地处理各种边界条件和特殊情况。 6. **提交答案**:当代码完成后,点击 "Submit" 提交你的解法。LeetCode 会自动运行所有测试用例并显示结果。 7. **学习他人的代码**:如果遇到困难,可以查看社区中的其他优秀解法,学习他人的思路和技术。 8. **反复练习**:刷题不是一次性的事情,通过反复练习和优化,逐渐提高解速度和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值