Skip to content

Latest commit

 

History

History
251 lines (215 loc) · 6.74 KB

LeetcodeIO.md

File metadata and controls

251 lines (215 loc) · 6.74 KB

一、模板类别

​ 数据结构:模拟力扣平台的输入输出

二、模板功能

备注:

  1. 本文件建立在 FastIO.h 头文件基础上。

  2. 本地调试的输入输出文件路径默认与 InputHelperOutputHelper 的默认路径相同。可以通过 LeetcodeInputHelper::setStreamLeetcodeOutputHelper::setStream 指定输入输出的流,也就是指定一个 InputHelper 或者 OutputHelper

  3. 逐行调试时,构造函数位于 _LeetcodeConstructorFactory::operator() 函数中,成员函数位于 _LeetcodeMemberFunction::exec 函数中,可以在相关处打断点;但是一般来说在主文件内打断点就足够了。

  4. 构建 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();
        }
    }
    
  5. 构建 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();
            }
        }
    }
    
  6. 有了 FastIO.hLeetcodeIO.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