首页 > 代码库 > 编写leetcode测试用例时所用的辅助函数

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

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

 tool.h

//tool.h
#ifndef TOOL_H_
#define TOOL_H_

#include <iostream>
#include <vector>
#include <cstdarg>
#include <string>
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<int> IntVectorCreate(int count, ...);
void IntVectorPrint(const vector<int> &vec);
void IntVector2DPrint(const vector<vector<int>> &vec);

vector<string> StringVectorCreate(int count, ...);
void StringVectorPrint(const vector<string> &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<int> &vec)
{
    for (size_t i = 0; i < vec.size(); i++)
        cout << vec[i] << " , ";
    cout << endl;
}

void IntVector2DPrint(const vector<vector<int>> &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<string> &vec)
{
    cout << "------------------------------------" << endl;
    for (size_t i = 0; i < vec.size(); i++)
        cout << vec[i] << "_" << endl;
    cout << "------------------------------------" << endl;
}

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

vector<int> IntVectorCreate(int count, ...)
{
    vector<int> result;
    va_list argptr;
    va_start(argptr, count);
    
    for(int i=0; i<count; i++) 
    {
        int num = va_arg(argptr, int);
        result.push_back(num);
    }
    va_end(argptr);
    return result;
}

vector<string> StringVectorCreate(int count, ...)
{
    vector<string> 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; i<count; i++)
    {
        if( !treeArr[i] )
            continue;
 
        if( curr < count )
            treeArr[i]->left = 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<int> ivec = IntVectorCreate(5, 1, 2, 3, 4, 5);
    IntVectorPrint(ivec);

    vector<string> 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);

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

技术分享

一切搞定

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