编写leetcode测试用例时所用的辅助函数

         在做leetcode题目(https://oj.leetcode.com/problemset/algorithms/)的时候,我们总要自己写测试用例,常常要生成vector, list, tree,我将这些常用操作封装了一下,方便大家使用

 tool.h

//tool.h
#ifndef TOOL_H_
#define TOOL_H_

#include 
#include 
#include 
#include 
using namespace std;

#include "windows.h"

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

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

void ArrayPrint(int arr[], int count);

vector IntVectorCreate(int count, ...);
void IntVectorPrint(const vector &vec);
void IntVector2DPrint(const vector> &vec);

vector StringVectorCreate(int count, ...);
void StringVectorPrint(const vector &vec);

TreeNode *TreeCreate(int count, ...);
void TreePrint(TreeNode *root);

ListNode *ListCreate(int count, ...);
void ListPrint(ListNode *head);

#endif
tool.cpp
//tool.cpp
#include "tool.h"

void ListPrint(ListNode *head)
{
    ListNode *node = head;
    while (node)
    {
        cout << node->val << " -> ";
        node = node->next;
    }
    cout << endl;
}

ListNode *ListCreate(int count, ...)
{
    va_list argptr;
    va_start(argptr, count);

    ListNode *head, *curr;
    int num = va_arg(argptr, int);
    head = new ListNode(num);
    curr = head;
    for (int i = 1; i < count; i++)
    {
        num = va_arg(argptr, int);
        ListNode *temp = new ListNode(num);
        curr->next = temp;
        curr = temp;
    }
    va_end(argptr);
    return head;
}


////////////////////////////////////////////

void ArrayPrint(int arr[], int count)
{
    for (int i = 0; i < count; i++)
        cout << arr[i] << " , ";
    cout << endl;
}

void IntVectorPrint(const vector &vec)
{
    for (size_t i = 0; i < vec.size(); i++)
        cout << vec[i] << " , ";
    cout << endl;
}

void IntVector2DPrint(const vector> &vec)
{
    printf("[\n");
    for (size_t i = 0; i < vec.size(); i++)
    {
        printf("[ ");
        for (size_t j = 0; j < vec[i].size(); j++)
            printf("%d, ", vec[i][j]);
        printf(" ]\n");
    }
    printf("]\n");
}

void StringVectorPrint(const vector &vec)
{
    cout << "------------------------------------" << endl;
    for (size_t i = 0; i < vec.size(); i++)
        cout << vec[i] << "_" << endl;
    cout << "------------------------------------" << endl;
}

//////////////////////////////////////////////////////////

vector IntVectorCreate(int count, ...)
{
    vector result;
    va_list argptr;
    va_start(argptr, count);
    
    for(int i=0; i StringVectorCreate(int count, ...)
{
    vector result;
    va_list argptr;
    va_start(argptr, count);

    for (int i = 0; i < count; i++)
    {
        char *str = va_arg(argptr, char*);
        result.push_back(str);
    }
    va_end(argptr);
    return result;
}

//////////////////////////////////////////////

TreeNode *TreeCreate(int count, ...)
{
    va_list argptr;
    va_start(argptr, count);

    TreeNode **treeArr = new TreeNode*[count];
    for (int i = 0; i < count; i++)
    {
        int num = va_arg(argptr, int);
        if (INT_MAX == num)
            treeArr[i] = NULL;
        else
            treeArr[i] = new TreeNode(num);
    }

    int curr = 1;
    for (int i = 0; ileft = treeArr[curr++];
        if (curr < count)
            treeArr[i]->right = treeArr[curr++];
    }
    
    va_end(argptr);
    return treeArr[0];
}


void SubTreePrint(TreeNode *node, int level)
{
    if ( !node )
        return;

    SubTreePrint(node->right, level + 1);
    for (int i = 0; i < level; i++)
        printf("    ");
    printf("%04d\n", node->val);
    SubTreePrint(node->left, level + 1);
}

void TreePrint(TreeNode *root)
{
    cout << "------------------------------------" << endl;
    SubTreePrint(root, 0);
    cout << "------------------------------------" << endl;
}

array, vector, list这几个结构的Create和Print很简单,就不多解释了

PS: XXXCreate函数的第一个参数总是元素的个数

int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    ArrayPrint(arr, 5);

    vector ivec = IntVectorCreate(5, 1, 2, 3, 4, 5);
    IntVectorPrint(ivec);

    vector svec = StringVectorCreate(5, "11", "22", "33", "44", "55");
    StringVectorPrint(svec);

    ListNode *node = ListCreate(5, 1, 2, 3, 4, 5);
    ListPrint(node);

    getchar();
    return 0;
}


tree的情况比较复杂,详细解释一下

假设一棵树如下:


那么leetcode上的表示是:{1,2,3,#,#,4,#,#,5}

代码是这个样子的:(#用INT_MAX代替)

    TreeNode *tree = TreeCreate(9, 1, 2, 3, INT_MAX, INT_MAX, 4, INT_MAX, INT_MAX, 5);
    TreePrint(tree);

打印出来之后,就是这个样子的


一切搞定

你可能感兴趣的:(C/C++)