leetcode的playground模板

  1. leetcode 是可以转移到本地进行调试的
  2. 本地文件的读写
  3. 例子

leetcode 是可以转移到本地进行调试的

将playground的代码复制到本地,然后加上各种头文件比如string
algorithm,sstream,using namespace等

本地文件的读写

引入cstdio头文件
main()函数中加入
freopen(“in.txt”,”r”,stdin);

可以利用输入与输出的方式直接对自己想要调试的文件,在断点处添加一些输出的语句将自己想要观察的变量值进行输出

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <string>
#include<iostream>
#include <sstream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdio>
using namespace std;

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

class Solution {
public:
TreeNode* increasingBST(TreeNode* root) {
vector<int> tree;
//构造新树
//TreeNode * r1;
midorder(root,tree);
TreeNode * r1 = new TreeNode(0);
TreeNode * cur = r1;
//r1->val = tree[0];
cout<<"size"<<tree.size();
for(int i =0; i< tree.size();i++){
//cur->left = NULL;
cur->right = new TreeNode(tree[i]);
cur = cur->right;
}
return r1->right;
}

void midorder(TreeNode * root,vector<int> &tree){
if(!root) return;
midorder(root->left,tree);
tree.push_back(root->val);
cout <<" jiedian"<< " "<< root->val<<" ";
midorder(root->right,tree);
}

};

void trimLeftTrailingSpaces(string &input) {
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
return !isspace(ch);
}));
}

void trimRightTrailingSpaces(string &input) {
input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
return !isspace(ch);
}).base(), input.end());
}

TreeNode* stringToTreeNode(string input) {
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
if (!input.size()) {
return nullptr;
}

string item;
stringstream ss;
ss.str(input);

getline(ss, item, ',');
TreeNode* root = new TreeNode(stoi(item));
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);

while (true) {
TreeNode* node = nodeQueue.front();
nodeQueue.pop();

if (!getline(ss, item, ',')) {
break;
}

trimLeftTrailingSpaces(item);
if (item != "null") {
int leftNumber = stoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}

if (!getline(ss, item, ',')) {
break;
}

trimLeftTrailingSpaces(item);
if (item != "null") {
int rightNumber = stoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}

string treeNodeToString(TreeNode* root) {
if (root == nullptr) {
return "[]";
}

string output = "";
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
TreeNode* node = q.front();
q.pop();

if (node == nullptr) {
output += "null, ";
continue;
}

output += to_string(node->val) + ", ";
q.push(node->left);
q.push(node->right);
}
return "[" + output.substr(0, output.length() - 2) + "]";
}

int main() {
string line;
freopen("in.txt","r",stdin);
while (getline(cin, line)) {
TreeNode* root = stringToTreeNode(line);

TreeNode* ret = Solution().increasingBST(root);

string out = treeNodeToString(ret);
cout << out << endl;
}
return 0;
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 583614868@qq.com

文章标题:leetcode的playground模板

文章字数:590

本文作者:钟帅豪

发布时间:2019-10-29, 09:45:25

最后更新:2019-10-29, 09:53:36

原始链接:http://jhshz520.github.io/2019/10/29/leetcode的playground模板/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏