搭建leetcode 链表和二叉树本地测试环境
2015-04-30 14:50
357 查看
为什么要搭建leetcode本地测试环境
leetcode是一个很好的算法和数据结构训练平台,但是链表,二叉树等数据结构是在后台实现的,所以不能在本地环境下构造测试用例来验证代码的准确性,下面我们来搭建一个可以生成用来测试的链表和二叉树的环境。链表
生成链表的方式比较简单,不断的生成随机数,并且将该随机数作为链表的结点插入链表末尾,从而生成一个可以用来测试的链表,这里以本地测试删除链表倒数第k个节点问题为例,如果你要测试你正在解决的问题的代码,只需要在代码注释处write your function below
粘贴上你写的函数,并在主函数里进行相应的修改即可。
代码如下
/*********************** test the leetcode's linkList problem in the loacal environment ***********************/ #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x): val(x),next(NULL) {} }; void addToTail(ListNode **head,int val) { ListNode *node =new ListNode(val); if (!*head) { *head = node; } else { ListNode *tmp=*head; while (tmp->next) tmp=tmp->next; tmp->next=node; } } void printList(ListNode *head) { ListNode* tmp; tmp=head; if (tmp==NULL) cout<<"empty list"; else { while (tmp!=NULL) { cout<<tmp->val<<','; tmp=tmp->next; } cout<<endl; } } //write your function below ListNode * removeNthNodeFromEnd(ListNode *head,int n) { if(head == NULL || n == 0)//防御性编程,增加健壮性 return head; ListNode *dummy = new ListNode(-1); dummy->next = head; ListNode *p,*q,*tmp; p = dummy; q = dummy; //p先走n步 for(int i=0;i<n;i++) { if(p->next != NULL) p = p->next; else return head;//如果n超过链表的长度,直接返回head } //然后p,q一起走 while(p->next) { p = p->next; q = q->next; } //此时q指向的是待删除节点的前驱 tmp = q->next->next; delete q->next; q->next = tmp; head = dummy->next; delete dummy; return head; } int main() { int lt=time(NULL); srand(lt); int len = rand()%20; cout<<len<< endl; if (len<1) return 0; ListNode *root=NULL; for (int i=0;i<len;i++) addToTail(&root,rand()%100); printList(root); // fill the parameters in your function below ListNode *resultList=removeNthNodeFromEnd(root,4); printList(resultList); }
二叉树
二叉树的构造需要我们手动输入节点的值,首先说一下leetcode中二叉树的表示形式这个二叉树用序列 3,9,20,#,#,15,7 来表示,所以我们通过执行程序时输入序列来生成二叉树,如图所示
这里是以测试二叉树层序遍历问题为例,同样如果你要测试你自己的函数,只需要在对应的地方和主函数中稍作修改即可
代码如下
/************************************************************** this version is used for testing leetcode's binary problem in local environment which the input data include double digits or more ****************************************************************/ #include <cstdio> #include <cstdlib> #include <stack> #include <iostream> #include <vector> #include <typeinfo> #include <exception> #include <map> #include <list> #include <algorithm> #include <ctime> #include <string> #include <queue> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int val):val(val), left(NULL),right(NULL){} ; }; TreeNode *constructTree(int *dat , int len) { TreeNode *root = NULL; int index = 0; if(len > 0) root = new TreeNode(dat[index]); else return NULL; list<TreeNode *> node; node.push_back(root); index ++; while(index < len) { if(!node.empty()) { TreeNode *root = node.front(); if(index < len ) { if(dat[index] != '#') { root->left = new TreeNode(dat[index]); node.push_back(root->left); } index ++; } if(index < len ) { if(dat[index] != '#') { root->right = new TreeNode(dat[index]); node.push_back(root->right); } index ++; } node.pop_front(); } } return root; } void traversal(TreeNode *node) { if(!node) return; cout <<"\t"<< node->val; traversal(node->left); traversal(node->right); } void printvector(const vector<vector<int> > &v) { for(int i=0;i<v.size();++i) { for(int j=0;j<v[i].size();++j) cout<<v[i][j]<<','; cout<<endl; } } class Solution { public: vector<vector<int> > levelOrder(TreeNode *root) { std::vector<vector<int> > result; if(root==nullptr) return result; std::vector<int> level; queue<TreeNode *> current,next; current.push(root); while(!current.empty()) { while(!current.empty()) { TreeNode *node=current.front(); current.pop(); level.push_back(node->val); if(node->left!=nullptr) next.push(node->left); if(node->right!=nullptr) next.push(node->right); } result.push_back(level); level.clear(); swap(current,next); } return result; } }; int main(int argc, char *argv[]) { if(argc < 2) { cout <<"Usage: ./createTreev 30 50 50 2 3 3 2"<<endl; exit(1); } int len = argc -1 ; cout << len <<endl; int *data = (int *)malloc(sizeof(int) * len); memset(data, 0, sizeof(char)*len); for(int i=1; i<argc; i++) { if(*argv[i] != '#') data[i-1] = atoi(argv[i]) ; else data[i-1] = '#'; } /*for(int i=0; i < len ; i++) cout <<"\t"<< data[i] ; cout << endl; cout << endl;*/ TreeNode *tree = NULL; tree = constructTree(data, len); //traversal(tree); //cout << endl; Solution s; printvector(s.levelOrder(tree)); }
相关文章推荐
- kafka本地测试环境搭建
- 前端项目本地测试环境搭建
- java微信公众号开发(搭建本地测试环境)
- 微信公众号开发 [02] 本地测试环境搭建
- 搭建可调试的微信公众平台本地测试环境
- iOS常用技术 - 本地搭建IPv6测试环境
- Windows环境下新浪SAE本地开发环境搭建及简单测试
- 【指南】本地如何搭建IPv6环境测试你的APP
- 本地 Mac 搭建 IPv6 测试环境
- SAP本地测试环境搭建
- https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
- 【转】https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
- java微信公众号开发(搭建本地测试环境)
- 【指南】本地如何搭建IPv6环境测试你的APP
- 免费搭建微信本地测试环境
- 微信搭建本地开发测试环境
- ECSHOP(网店系统)本地测试环境搭建
- 搭建可调试的微信公众平台本地测试环境
- 本地如何搭建IPv6环境测试APP是否支持
- https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题