面试专题算法题精选(二)

测试常见的八股文

这篇文章总结了一下我之前学习软件测试面试的一些算题。 文章主要包含Leecode一些简单和中等难度的题。

1. 每日温度

Leecode链接: 739.每日温度

class Solution {
   
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
   
        int n = temperatures.size();

        vector<int> res(n, 0);
        stack<int> stk;
        for (int i = n - 1; i >= 0; i -- )
        {
   
            while(stk.size() && temperatures[stk.top()] <= temperatures[i]) stk.pop();
            if (stk.empty()) res[i] = 0;
            else res[i] = stk.top() - i;

            stk.push(i);
        }

        return res;
        
    }
};

2. 二叉树的最小深度

Leecode链接: 111.二叉树的最小深度

/**
 * 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) {
   
        // 最小深度考虑用dfs
        if (root == nullptr) return 0;
        queue<TreeNode*> q;
        q.push(root);
        int depth = 1;

        while (!q.empty())
        {
   
            int sz = q.size();
            for (int i = 0; i < sz; i ++ )
            {
   
                auto t = q.front();
                q.pop();
                // 判断一下bfs的终止条件
                if (t -> left == nullptr && t -> right == nullptr)
                    return depth;

                if (t -> left != nullptr) q.push(t -> left);
                if (t -> right !=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值