数据结构:模拟力扣平台的输入输出
备注:
-
本文件建立在
FastIO.h
头文件基础上。 -
本地调试的输入输出文件路径默认与
InputHelper
及OutputHelper
的默认路径相同。可以通过LeetcodeInputHelper::setStream
和LeetcodeOutputHelper::setStream
指定输入输出的流,也就是指定一个InputHelper
或者OutputHelper
。 -
逐行调试时,构造函数位于
_LeetcodeConstructorFactory::operator()
函数中,成员函数位于_LeetcodeMemberFunction::exec
函数中,可以在相关处打断点;但是一般来说在主文件内打断点就足够了。 -
构建
Solution
模式代码的体系:#include "IO/LeetcodeIO.h" using namespace std; class Solution { public: vector<int> twoSum(vector<int> &nums, int target) { int n = nums.size(); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (nums[i] + nums[j] == target) { return {i, j}; } } } return {}; } }; int main() { REGISTER_CONSTRUCTOR_SOLUTION; REGISTER_MEMBERFUNCTION_SOLUTION(twoSum); while (true) { executor.constructSolution(); executor.executeSolution(); } }
-
构建
Class
模式代码的体系:#include "IO/LeetcodeIO.h" using namespace std; class MKAverage { public: // 第1825题:求出MK平均值 MKAverage(int m, int k) {} void addElement(int num) {} int findK(int k) { return -1; } int calculateMKAverage() { return -1; } }; int main() { REGISTER_CONSTRUCTOR_CLASS(MKAverage, int, int); // 本行填写类名、构造函数的所有参数类 REGISTER_MEMBERFUNCTION_CLASS(MKAverage, addElement, calculateMKAverage); // 本行填写类名、要调用的成员方法名 while (true) { executor.constructClass(); while (executor) { executor.executeClass(); } } }
-
有了
FastIO.h
和LeetcodeIO.h
,其实可以将力扣网站的输入输出劫持。输出文件需要重定位到./user.out
。只需要在main
函数之前执行我们自定义的输入、计算、输出,然后调用exit(0)
退出程序,我们就完成了一波暗度陈仓,可以自行探索如何实现。
#include "IO/LeetcodeIO.h"
using namespace std;
class Solution {
public:
// 第110题:平衡二叉树,很简单
int getDepth(TreeNode *root, bool &flag) {
if (root == nullptr) return 0;
int leftDepth = getDepth(root->left, flag);
int rightDepth = getDepth(root->right, flag);
if (abs(leftDepth - rightDepth) > 1) flag = false;
return 1 + max(leftDepth, rightDepth);
}
bool isBalanced(TreeNode *root) {
bool flag = true;
int depth = getDepth(root, flag);
return flag;
}
};
int main() {
REGISTER_CONSTRUCTOR_SOLUTION;
REGISTER_MEMBERFUNCTION_SOLUTION(isBalanced);//这里填写方法名
while (true) {
executor.constructSolution();
executor.executeSolution();
}
}
#以下为 "in.txt" 中的内容
[3,9,20,null,null,15,7]
[1,2,2,3,3,null,null,4,4]
[]
#以下为程序运行之后, "out.txt" 中的内容
true
false
true