神奇魔盒
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 格式最开始被用来在什么操作系统上工作?
A | Linux |
B | Unix |
C | DOS |
D | Windows |
答案: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;
}
};
A | if (first && prev >= root->val) { return false; } first = false; prev = root->val; root = root->right; |
B | if (first || prev >= root->val) { return false; } first = false; prev = root->val; root = root->right; |
C | if (!first || prev >= root->val) { return false; } first = false; prev = root->val; root = root->right; |
D | if (!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];
}
};