每日一练--IT冷知识&C/C++--第六天

神奇魔盒

1925 年 10 月 2 日,John. L. Baird 制造出了世界上第一台 _____

A能解多项式方程的计算器
B能每分钟印刷 100 页的油印机
C能传输图像的机械式电视机
D能近距离传输图像的传真机

答案:能传输图像的机械式电视机


Gzip

Gzip 是 GNUzip 的缩写,由 Jean-loup Gailly 和 Mark Adler 创建。我们在 Linux 中经常会用到后缀为.gz 的文件,它们就是 GZIP 格式的。Gzip 现今已经成为 Internet 上使用非常普遍的一种数据压缩格式,或者说一种文件格式。

Gzip 格式最开始被用来在什么操作系统上工作?

ALinux
BUnix
CDOS
DWindows

答案:Unix


Twitter 的创始人

Twitter(推特)是一家美国社交网络及微博客服务的公司,致力于服务公众对话。Twitter 的几位共同创始人里,有一位也是移动支付公司 Square 的创始人,如今的他留着显眼的大胡子。

Twitter 的这位创始人是谁?

A马克·扎克伯格(Mark Zuckerberg)
B伊万·威廉姆斯(Evan Williams)
C比兹·斯通(Biz Stone)
D杰克·多西(Jack Dorsey)

答案:杰克·多西(Jack Dorsey)


二叉树的最大深度

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

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

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

示例:
给定二叉树 [3,9,20,null,null,15,7]

 

返回它的最大深度 3 。

A#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
    int maxDepth(TreeNode *root)
    {
        int depth = 0;
        if (root != NULL)
        {
            return 0;
        }
        else
        {
            depth = max(maxDepth(root->left), maxDepth(root->right));
            return 1 + depth;
        }
    }
};
B#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
    int maxDepth(TreeNode *root)
    {
        int depth = 0;
        if (root == NULL)
        {
            return 0;
        }
        else
        {
            depth = max(maxDepth(root->left), maxDepth(root->right));
            return 1 + depth;
        }
    }
};
C#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
    int maxDepth(TreeNode *root)
    {
        int depth = 0;
        if (root >= NULL)
        {
            return 0;
        }
        else
        {
            depth = max(maxDepth(root->left), maxDepth(root->right));
            return 1 + depth;
        }
    }
};
D以上都不对

答案:

#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
    int maxDepth(TreeNode *root)
    {
        int depth = 0;
        if (root == NULL)
        {
            return 0;
        }
        else
        {
            depth = max(maxDepth(root->left), maxDepth(root->right));
            return 1 + depth;
        }
    }
};

验证二叉搜索树

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

有效 二叉搜索树定义如下:

  • 节点的左子树只包含 小于 当前节点的数。
  • 节点的右子树只包含 大于 当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入:root = [2,1,3]
输出:true

示例 2:

输入:root = [5,1,4,null,null,3,6]
输出:false
解释:根节点的值是 5 ,但是右子节点的值是 4 。

提示:

  • 树中节点数目范围在[1, 104] 内
  • -231 <= Node.val <= 231 - 1

以下程序实现了这一功能,请你填补空白处内容:

#include <bits/stdc++.h>
using namespace std;
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:
	bool isValidBST(TreeNode *root)
	{
		stack<TreeNode *> stk;
		int prev = INT_MIN;
		bool first = true;
		while (!stk.empty() || root != nullptr)
		{
			if (root != nullptr)
			{
				stk.push(root);
				root = root->left;
			}
			else
			{
				root = stk.top();
				stk.pop();
				_______________________;
			}
		}
		return true;
	}
};
Aif (first && prev >= root->val)
{
    return false;
}
first = false;
prev = root->val;
root = root->right;
Bif (first || prev >= root->val)
{
    return false;
}
first = false;
prev = root->val;
root = root->right;
Cif (!first || prev >= root->val)
{
    return false;
}
first = false;
prev = root->val;
root = root->right;
Dif (!first && prev >= root->val)
{
    return false;
}
first = false;
prev = root->val;
root = root->right;

 答案:

if (!first && prev >= root->val)
{
	return false;
}
first = false;
prev = root->val;
root = root->right;

买卖股票的最佳时机 IV

给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入:k = 2, prices = [2,4,1]
输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。

示例 2:

输入:k = 2, prices = [3,2,6,5,0,3]
输出:7
解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。

提示:

  • 0 <= k <= 100
  • 0 <= prices.length <= 1000
  • 0 <= prices[i] <= 1000
A#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int maxProfit(int k, vector<int> &prices)
    {
        const int len = prices.size();
        if (len <= 1 || k == 0)
            return 0;
        if (k > len / 2)
            k = len / 2;
        const int count = k;
        int buy[count];
        int sell[count];
        for (int i = 0; i < count; ++i)
        {
            buy[i] = -prices[0];
            sell[i] = 0;
        }
        for (int i = 1; i < len; ++i)
        {
            buy[0] = max(buy[0], -prices[i]);
            sell[0] = max(sell[0], buy[0] + prices[i]);
            for (int j = count - 1; j > 0; --j)
            {
                buy[j] = max(buy[j], sell[j - 1] - prices[i]);
                sell[j] = max(buy[j] + prices[i], sell[j]);
            }
        }
        return sell[count - 1];
    }
};
B#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int maxProfit(int k, vector<int> &prices)
    {
        const int len = prices.size();
        if (len <= 1 || k < 0)
            return 0;
        if (k > len / 2)
            k = len / 2;
        const int count = k;
        int buy[count];
        int sell[count];
        for (int i = 0; i < count; ++i)
        {
            buy[i] = -prices[0];
            sell[i] = 0;
        }
        for (int i = 1; i < len; ++i)
        {
            buy[0] = max(buy[0], -prices[i]);
            sell[0] = max(sell[0], buy[0] + prices[i]);
            for (int j = count - 1; j > 0; --j)
            {
                buy[j] = max(buy[j], sell[j - 1] - prices[i]);
                sell[j] = max(buy[j] + prices[i], sell[j]);
            }
        }
        return sell[count - 1];
    }
};
C#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int maxProfit(int k, vector<int> &prices)
    {
        const int len = prices.size();
        if (len < 1 || k == 0)
            return 0;
        if (k > len / 2)
            k = len / 2;
        const int count = k;
        int buy[count];
        int sell[count];
        for (int i = 0; i < count; ++i)
        {
            buy[i] = -prices[0];
            sell[i] = 0;
        }
        for (int i = 1; i < len; ++i)
        {
            buy[0] = max(buy[0], -prices[i]);
            sell[0] = max(sell[0], buy[0] + prices[i]);
            for (int j = count - 1; j > 0; --j)
            {
                buy[j] = max(buy[j], sell[j - 1] - prices[i]);
                sell[j] = max(buy[j] + prices[i], sell[j]);
            }
        }
        return sell[count - 1];
    }
};
D#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int maxProfit(int k, vector<int> &prices)
    {
        const int len = prices.size();
        if (len <= 1 & k == 0)
            return 0;
        if (k > len / 2)
            k = len / 2;
        const int count = k;
        int buy[count];
        int sell[count];
        for (int i = 0; i < count; ++i)
        {
            buy[i] = -prices[0];
            sell[i] = 0;
        }
        for (int i = 1; i < len; ++i)
        {
            buy[0] = max(buy[0], -prices[i]);
            sell[0] = max(sell[0], buy[0] + prices[i]);
            for (int j = count - 1; j > 0; --j)
            {
                buy[j] = max(buy[j], sell[j - 1] - prices[i]);
                sell[j] = max(buy[j] + prices[i], sell[j]);
            }
        }
        return sell[count - 1];
    }
};

答案:

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int maxProfit(int k, vector<int> &prices)
    {
        const int len = prices.size();
        if (len <= 1 || k == 0)
            return 0;
        if (k > len / 2)
            k = len / 2;
        const int count = k;
        int buy[count];
        int sell[count];
        for (int i = 0; i < count; ++i)
        {
            buy[i] = -prices[0];
            sell[i] = 0;
        }
        for (int i = 1; i < len; ++i)
        {
            buy[0] = max(buy[0], -prices[i]);
            sell[0] = max(sell[0], buy[0] + prices[i]);
            for (int j = count - 1; j > 0; --j)
            {
                buy[j] = max(buy[j], sell[j - 1] - prices[i]);
                sell[j] = max(buy[j] + prices[i], sell[j]);
            }
        }
        return sell[count - 1];
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

兔子递归

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

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

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

打赏作者

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

抵扣说明:

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

余额充值