1. 程式人生 > >牛客網線上程式設計《劍指Offer》如何通過本地來除錯

牛客網線上程式設計《劍指Offer》如何通過本地來除錯

一、序言(廢話)

在我準備找工作的時候, 雖然讀的通訊,但是還是想著往比較火的網際網路轉。然後呢,就到牛客網上去刷題。

無非就是這麼幾個線上程式設計專題 ,我主要針對的是【華為機試板塊】,但是找工作嘛,那句俗話

                                                     【劍指Offer一定要刷三遍以上

前輩們的金口玉言,怎麼能不聽?但是一點開進去傻眼了。【劍指Offer】這一塊,只給了一個Class,給好了介面,你往裡填就好了。拜託,我想要本地除錯,人嘛,總有點虛榮心,你讓我在OJ上提交了去碰運氣看寫沒寫對,那個醒目的通過率低的嚇人。我也要自尊心的,我也要虛榮心的。我錯了,你就跟我說哪一行錯了,不夠的嘛。我需要更多的提示,我好去google的好嗎。無論怎樣,我水平不高,我就想要本地除錯

二、來,我們來看看牛客網給的介面是什麼鬼:

題目:輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        //等待你的程式碼
    }
};

這個反饋介面,不友好。可是我要自己自己本地調,怎麼用?

三、自己寫的本地除錯框架

#include "pch.h" #include <iostream> #include <vector> using namespace std;

注:這上面是VS自動生成的,下面是把牛客網註釋掉的結構體搬過來的

struct TreeNode {      int val;     TreeNode *left;     TreeNode *right;     //TreeNode(int x) : val(x), left(NULL), right(NULL) {} };

class Solution { public:     TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {         4.這裡面寫的就是牛客網OJ上需要的         vector<int> vinA, vinB, preA, preB;         if (pre.empty() || vin.empty()) {             return NULL;         }         int root = pre[0], index;         TreeNode* res = new TreeNode;

        得到想要的preA、vinA、preB, vinB

        res->val = root;         5.呼叫自身做遞迴         res->left = reConstructBinaryTree(preA, vinA);         res->right = reConstructBinaryTree(preB, vinB);         return res;     } };

下面這一塊就是本地除錯需要好好寫的 

int main() {     1.將題目給出的例子直接寫下來     vector<int> pre = {1,2,4,7,3,5,6,8};     vector<int> vin = {4,7,2,1,5,3,8,6};     2.關鍵:建立這個Class,類是於先用“int i;”宣告一個int型型別i,這裡用class Solution宣告一個Solu類     class Solution Solu;     3.呼叫類裡面的函式(傳入引數進去)     Solu.reConstructBinaryTree(pre,vin);     return 0; }

附錄:

1.牛客網上提交 


class Solution {
public:
	TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
		vector<int> vinA,vinB,preA,preB;
		
		if (pre.empty() || vin.empty()) {
			return NULL;
		}
		int root = pre[0],index;
        struct TreeNode* res = new TreeNode(root);
		for (int i = 0; i < vin.size(); i++) {
			if (vin[i] != root) {
				vinA.push_back(vin[i]);
			}
			else {
				index = i;
				break;
			}
		}
		for (int i = index + 1; i < vin.size(); i++) {
			vinB.push_back(vin[i]);
		}
		int lenPre = vinA.size();
		for (int i = 1; i < 1 + lenPre; i++) {
			preA.push_back(pre[i]);
		}
		for (int i = 1 + lenPre; i < pre.size(); i++) {
			preB.push_back(pre[i]);
		}
		res->val = root;
		res->left = reConstructBinaryTree(preA,vinA);
		res->right= reConstructBinaryTree(preB, vinB);
		return res;
	}
};

2.本地除錯完整的,需要注意的是本地除錯用的,OJ上不一定能用,有需要的改一下就好 

// Temp.cpp : 此檔案包含 "main" 函式。程式執行將在此處開始並結束。
//

#include "pch.h"
#include <iostream>
#include <vector>
using namespace std;

struct TreeNode {
	int val;
    TreeNode *left;
    TreeNode *right;
	//TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
	TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
		vector<int> vinA, vinB, preA, preB;
		if (pre.empty() || vin.empty()) {
			return NULL;
		}
		int root = pre[0], index;
		TreeNode* res = new TreeNode;
		for (int i = 0; i < vin.size(); i++) {
			if (vin[i] != root) {
				vinA.push_back(vin[i]);
			}
			else {
				index = i;
				break;
			}
		}
		for (int i = index + 1; i < vin.size(); i++) {
			vinB.push_back(vin[i]);
		}
		int lenPre = vinA.size();
		for (int i = 1; i < 1 + lenPre; i++) {
			preA.push_back(pre[i]);
		}
		for (int i = 1 + lenPre; i < pre.size(); i++) {
			preB.push_back(pre[i]);
		}
		res->val = root;
		res->left = reConstructBinaryTree(preA, vinA);
		res->right = reConstructBinaryTree(preB, vinB);
		return res;
	}
};;

int main()
{
	vector<int> pre = {1,2,4,7,3,5,6,8};
	vector<int> vin = {4,7,2,1,5,3,8,6};
	class Solution Solu;
	Solu.reConstructBinaryTree(pre,vin);
	return 0;
}