wy的leetcode刷题记录_Day39

这篇博客记录了作者在LeetCode上解决的三道题目,包括判断字符串的两半是否相似以及两种类型的二叉树深度计算。通过DFS和BFS展示了如何找到二叉树的最大深度和最小深度。对于字符串问题,重点在于统计元音的数量。在二叉树问题中,理解递归和广度优先搜索在求解树的深度问题中的应用是关键。
摘要由CSDN通过智能技术生成

wy的leetcode刷题记录_Day39

声明

本文章的所有题目信息都来源于leetcode
如有侵权请联系我删掉

1704. 判断字符串的两半是否相似

今天的每日一题是:1704. 判断字符串的两半是否相似

题目介绍

给你一个偶数长度的字符串 s 。将其拆分成长度相同的两半,前一半为 a ,后一半为 b 。

两个字符串相似的前提是它们都含有相同数目的元音 (‘a’,‘e’,‘i’,‘o’,‘u’,‘A’,‘E’,‘I’,‘O’,‘U’)。注意,s 可能同时含有大写和小写字母。

如果 a 和 b 相似,返回 true ;否则,返回 false 。

示例 1:
输入:s = “book”
输出:true 解释:a = “bo” 且 b = “ok” 。a 中有 1 个元音,b 也有 1个元音。所以,a 和 b 相似。

示例 2:
输入:s = “textbook”
输出:false
解释:a = “text” 且 b = “book” 。a 中有 1个元音,b 中有 2 个元音。因此,a 和 b 不相似。 注意,元音 o 在 b 中出现两次,记为 2 个。

思路

简单模拟题:首先判断这个字符串的长度是否为偶数,如果是奇数的话就达不成上述条件。在偶数的情况下,使用一个指针指向s.size()/2的位置分开俩个字符串(可以另外用俩个字符串存贮,但是这里节省空间就没用),然后判断就行了。

代码

class Solution {
public:
    bool halvesAreAlike(string s) {
        int n=s.size();
        if(n%2!=0)
            return false;
        // string s1=s.substr(0,n/2);
        // string s2=s.substr(n/2);
        string yuanYin ="aeiouAEIOU";
        int ans=0;
        // for(int i=0;i<n/2;i++)
        // {
        //     if(yuanYin.find_first_of(s1[i]) != string::npos)
        //         ans++;
        // }
        // for(int i=0;i<n/2;i++)
        // {
        //     if(yuanYin.find_first_of(s2[i]) != string::npos)
        //         ans--;
        // }
        for(int i=0;i<n/2;i++)
        {
            if(yuanYin.find(s[i]) != string::npos)
                ans++;
        }
        for(int i=n/2;i<n;i++)
        {
            if(yuanYin.find(s[i]) != string::npos)
                ans--;
        }
        return ans==0;
    }
};

收获

了解了string:npos是代表字符串的最大长度,可以在匹配字符串或者替换字符串之类的场景使用。

104. 二叉树的最大深度

104. 二叉树的最大深度

题目介绍

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7 返回它的最大深度 3 。

思路

DFS:每次向下遍历维护一个max_depth变量。
BFS:同理。。

代码

DFS:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int max_depth=0;
    int maxDepth(TreeNode* root) {
        dfs(root,0);
        return max_depth;
    }
    void dfs(TreeNode* node ,int depth)
    {
        if(!node)
            return;
        depth++;
        max_depth=max(max_depth,depth);
        dfs(node->left,depth);
        dfs(node->right,depth);
    }
};

BFS:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=0;
        if(!root)
            return depth;
        queue<TreeNode*> qu;
        qu.push(root);
        while(!qu.empty())
        {
            int n=qu.size();
            depth++;
            for(int i=0;i<n;i++)
            {
                TreeNode* node=qu.front();
                qu.pop();
                if(node->left)
                    qu.push(node->left);
                if(node->right)
                    qu.push(node->right);
            }
        }
        return depth;
    }
};

收获

巩固了DFS和BFS

111. 二叉树的最小深度

111. 二叉树的最小深度

题目介绍

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。

示例 1:
在这里插入图片描述
输入:root = [3,9,20,null,null,15,7]
输出:2

示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5

思路

首先明确根节点不应该是最短的路径(除非其没有子节点)

代码

BFS:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode *root) {
        if (root == nullptr) {
            return 0;
        }

        queue<TreeNode *> que;
        int depth=0;
        que.push(root);
        while (!que.empty()) {
            int n=que.size();
            depth++;
            for(int i=0;i<n;i++)
            {
                TreeNode *node = que.front();
                que.pop();
                if (node->left == nullptr && node->right == nullptr) {
                    return depth;
                }
                if (node->left != nullptr) {
                    que.push(node->left);
                }
                if (node->right != nullptr) {
                    que.push(node->right);
                }
            }
        }
        return 0;
    }
};

DFS

class Solution {
public:
    int getDepth(TreeNode* node) {
        if (node == NULL) 
            return 0;
        int leftDepth = getDepth(node->left); // 左
        int rightDepth = getDepth(node->right); // 右
        // 中
        // 当⼀个左⼦树为空,右不为空,这时并不是最低点
        if (node->left == NULL && node->right != NULL) { 
        return 1 + rightDepth;
        } 
        // 当⼀个右⼦树为空,左不为空,这时并不是最低点
        if (node->left != NULL && node->right == NULL) { 
            return 1 + leftDepth;
        }
        int result = 1 + min(leftDepth, rightDepth);
            return result;
        }
        int minDepth(TreeNode* root) {
            return getDepth(root);
    }
};

收获

巩固DFS和BFS

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wy-1226

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值