-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
156 lines (134 loc) · 4.32 KB
/
main.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) luckystone60 Technologies Co., Ltd. 2012-2018. All right reserved.
* Description: main.cpp
* Author:luckystone60
* Create:2020-03-01
*/
#include "Helper.h"
#include "Solution.h"
using namespace std;
using namespace LEETCODE::HELPER;
#ifdef TEST
int TestHelper()
{
int intTest = 0;
float floatTest = 0.0f;
double doubleTest = 0.0f;
string stringTest = "test";
string stringNullTest = "";
char * strTest = "test";
vector<int> vecIntTest = {0, 2};
vector<int> vecIntNullTest = {};
vector<vector<int>> matIntTest = {{0,3}, {2, 4}};
vector<vector<int>> matIntNullTest = {{}};
// // [2.123,0.234,34]
string inputDoubleVec = "[2.123,0.234,34]";
string inputIntVec = "[2, 9, 0]";
auto doubleVec = CreateVector<double>(inputDoubleVec);
PrintVector(doubleVec);
cout << "++++++++++++++++++++++++++++++++++++++" << endl;
// ["sdf", "sdfr", "seosd"]
string inputStrVec = R"(["sdf", "sdfr", "seosd"])";
auto strVec = CreateVector<string>(inputStrVec);
PrintVector(strVec);
cout << "++++++++++++++++++++++++++++++++++++++" << endl;
// [[2.123,0.234,34], [2.123,0.234,34], [2.123,0.234,34]]
string inputFloatMat = R"([[-2.123,0.234,34], [2.123,0.234,34], [-2.123,0.234,34]])";
auto floatMat = CreateMatrix<float>(inputFloatMat);
PrintMatrix(floatMat);
cout << "++++++++++++++++++++++++++++++++++++++" << endl;
ListNode *head = CreateList(inputIntVec);
PrintList(head);
DestroyList(head);
cout << "++++++++++++++++++++++++++++++++++++++" << endl;
string inputTree = "[1, -2, 3, 4, null, 5, 6]";
string inputTree2 = "[1, -2, 3, 4, null, 5, 6, 7, 8, 9, 10, 11, 12]";
TreeNode *root = CreateTree(inputTree2);
PrintTree(root);
DestroyTree(root);
cout << "++++++++++++++++++++++++++++++++++++++" << endl;
return 0;
}
#endif
class Solution235 {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) return root;
if(p->val<root->val&&q->val<root->val)
return lowestCommonAncestor(root->left, p, q);
if(p->val>root->val&&q->val>root->val)
return lowestCommonAncestor(root->right, p, q);
return root;
}
};
class Solution418 {
public:
bool isValid(vector<int> &pos, int &wordInd)
{
if (cols - pos[1] == sentence[wordInd].size()) {
cout << sentence[wordInd] << endl;
pos[0]++;
pos[1] = 0;
wordInd++;
return true;
} else if (cols - pos[1] > sentence[wordInd].size()) {
pos[1] += sentence[wordInd].size() + 1;
if (pos[1] == cols) {
pos[0]++;
pos[1] = 0;
cout << sentence[wordInd] << "-" << endl;
} else {
cout << sentence[wordInd] << "-";
}
wordInd++;
return true;
} else {
cout << endl;
pos[0]++;
pos[1] = 0;
return false;
}
}
int wordsTyping(vector<string>& sentence, int rows, int cols) {
this->rows = rows;
this->cols = cols;
this->sentence = sentence;
vector<int> pos(2, 0);
int wordInd = 0;
int ret = 0;
int loopCount = 0;
while (pos[0] < rows) {
if (isValid(pos, wordInd)) {
if (wordInd == sentence.size()) {
ret++;
wordInd = 0;
if (pos[1] == 0 && pos[0] < rows && loopCount == 0) {
loopCount = ret;
int tmp = (rows - pos[0]) / loopCount;
pos[0] += tmp * loopCount;
ret += tmp * loopCount;
}
}
}
}
return ret;
}
int rows;
int cols;
vector<string> sentence;
};
int main()
{
// Solution235 so;
// auto input = CreateTree("[6,2,8,0,4,7,9,null,null,3,5]");
// PrintTree(input);
// auto p = GetSpecifiedValueNode(input, 2);
// auto q = GetSpecifiedValueNode(input, 8);
// auto ret = so.lowestCommonAncestor(input, p, q);
// PrintValue(ret->val, "result ");
Solution418 so;
vector<string> input = {"a", "b", "e"};
PrintValue(so.wordsTyping(input, 10000, 7));
system("pause");
return 0;
}