In Leetcode, how code is debugged, creating a local running environment

Source: Internet
Author: User
Tags rand

First Contact Leetcode, I was in a recruitment site to see, this OJ really so powerful.

These days in this OJ to do a few questions, found his several characteristics, 1, the topic is not difficult (relative to the ACM, I was the ACM abuse to date powerless), the judge is not so harsh, 2, very basic, from the chain list, tree to dynamic planning, are very basic very classic content, quite by the basic skills, 3, No local debugging environment, submitted directly on the site, background evaluation system to help you complete the program input, evaluation output function, 4, domestic and foreign industry recognition, there are many people are full, leaving a lot of excellent sample code, 5, supporting the characteristics of C + + 11 Oh, a auto keyword will make you great big.

So I believe that this OJ to consolidate algorithms and data structure of the basic skills, as well as the interview written test has a great effect, decided to start brush problems. The first brush body feel very uncomfortable, the title page only need class Solution, no input, no output, what, debugging where. Later I found that, without conditions, it was necessary to create the conditions themselves. Because most of the internet is a solution to the idea of submission, not conducive to novice on the road, so I want to write such a post, hoping to help others, so Daniel Mo smiling.

Well, if there are linked problems, then we write the main function, first to construct the linked list, and then to sort out what the reverse, if there are tree problems, then we write the main function, to construct the tree, and then Debug.

Well, there are a few things that need to be done here:

1, the program input data, is used to construct the linked list, tree content data, one source is rand (), using random number, another source is manual input.

In the linked list, because we only need some content to see what sort effect, so you can generate data by random numbers, code (write only the relevant header files and statements) are as follows:

#include <stdlib.h>
#include <time.h>
int lt = Time (NULL);
Srand (LT);
int len = rand ()%100;
int data = rand ()% 100;
So to generate random data

In the tree, for example, the question is to determine whether it is a symmetric tree (symmetric trees), then we want to manually enter the tree data as well, the tree node content in the main function parameter input to the program, execution, such as:

./symmetric_tree 123# #5

Here's the 123# #5是leetcode中的树的表示, in fact, is in accordance with the two fork of the tree through the history of the structure, 123# #5构造了如下一颗树, of which 2 of the 1 of the children are 2, 3,2 the left and Right child is # #, is empty, 2 is a leaf node, 3 of the left child is 5, The right child is empty, empty will not enter the data. When debugging, we need to construct the tree in this way.

1

2 3

# # 5

2, the construction of the linked list, the code is as follows

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;
	}
}

int main ()
{
	int lt = Time (NULL);
	Srand (LT);
	int len = rand ()%20;
	cout << len<<endl;
	if (Len < 1) return
		0;
<span style= "White-space:pre" >	</span>listnode *root = NULL;
        for (int i=0; i<len; i++)
                  addtotail (&root, rand ()%100)
         

}
3. Construction Tree

According to the 123# #5构造二叉树, the Constructtree function belongs to the method of constructing a two-forked tree by layer.

#include <stdio.h> #include <stdlib.h> #include <stack> #include <iostream> #include <vector > #include <typeinfo> #include <exception> #include <map> #include <list> #include <

Algorithm> #include <time.h> using namespace std;
	struct TreeNode {int val;
	TreeNode *left;
	TreeNode *right; 
TreeNode (int val): Val (Val), left (null), right (null) {};

};
	TreeNode *constructtree (char *dat, int len) {TreeNode *root = NULL;
	int index = 0;
	if (len > 0) root = new TreeNode (Dat[index]-' 0 ');

	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]-' 0 ');
				Node.push_back (Root->left);
			Index + +; } if (Index < len) {if (Dat[index]!= ' # ') {root->right = new TreeNode(Dat[index]-' 0 ');
				Node.push_back (Root->right);
			Index + +;
		} node.pop_front ();
} return root;
			Class Solution {public:vector<int> inordertraversal (TreeNode *root) {vector<int > tree;
TreeNode *node = root;
			Traversal (node, tree);			
			Itera_traversal (node, tree);
		return to tree;

			} void Traversal (TreeNode *node, vector<int> &tree) {if (!node) return;
			Traversal (node->left, tree);
			Tree.push_back (Node->val);

		Traversal (node->right, tree);
			} void Itera_traversal (TreeNode *node, vector<int> &tree) {Stack<treenode *>;
			TreeNode *root = node;
					while (Root | |!lroot.empty ()) {while (root) {Lroot.push (root);
				root = root->left;
					} if (!lroot.empty ()) {root = Lroot.top ();
					Tree.push_back (Root->val);
					Lroot.pop ();
				root = root->right;


}
			}		
		}
}; int main (int argc, char *argv[]) {if (argc &LT
		2) {cout << "Usage:./binary_tree_inorder_traversal treelist (1234#5### #6)" <<endl;
	Exit (1);
	} string treeinfo = Argv[1];
		if (Treeinfo.size () < 1) {cout << "tree data Invalid" <<endl;
	Exit (1);
	int len = Treeinfo.size ();
	Char *dat = new Char[len];	
	for (int i=0; i<len; i++) {dat[i] = Treeinfo[i];
	for (int i=0; i<len; i++) cout << "\ t" <<dat[i];

	cout << Endl;
	TreeNode *root = NULL;

	root = Constructtree (dat, Len);
	Solution s;
	Vector<int > seq = s.inordertraversal (root);
	for (int i=0; i<seq.size (); i++) cout << "\ T" <<seq[i];
	cout << Endl;

	cout << Endl;
Delete dat; }

However, this kind of 123# #5的方式有一定缺陷, when a node value of 15, 20,344, and so on, what to do, it can only follow all parameters are entered to the main function, the execution is similar to

./VALIDATE_BINARY_SEARCH_TREE.O 4 2 6 5 3 1 # 6

#include <stdio.h> #include <stdlib.h> #include <stack> #include <iostream> #include <vector > #include <typeinfo> #include <exception> #include <map> #include <list> #include <

algorithm> #include <time.h> #include <string.h> 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);
			Class Solution {Public:bool Isvalidbst (TreeNode *root) {int minmum = 0xFFFFFFFF;
		Return judge (Root, Minmum);
			BOOL Judge (TreeNode *root, int &min) {if (!root) return true;
			BOOL flag = Judge (Root->left, Min);
			if (!flag) return false;
			if (Root->val < min) return false;
			Min = root->val;
		Return judge (Root->right, Min);



}
}; int main (int argc, char *argv[]) {if (ARGC < 2) {cout << Usage:./binary_tree_inorder_traversal tree_list (4
		2 6 5 3 1 # 6) "<<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;
cout << S.isvalidbst (tree) <<endl; }

This method atoi each parameter to an integer, so in constructing the two-fork tree function Constructtree, it is necessary to note that the ASCII value of ' # ' is 34, and be careful. But for testing, it's barely available.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.