-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
526 lines (480 loc) · 10.4 KB
/
test.cpp
File metadata and controls
526 lines (480 loc) · 10.4 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include <stdio.h>
#include <vector>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
tring multiply(string num1, string num2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
}
string multiply(const string &a, const string &b)
{
if ( a.length() == 0 || b.length() == 0) return "";
int length1 = a.length();
int length2 = b.length();
// int length3 = length1 + length2;
char* result = new char[length1+length2+1];
if ( result == NULL )
return "";
memset(result, 0, length1+length2+1);
for ( int i = 0 ; i < length1; i++)
{
for ( int j = 0; j< length2;j++)
{
if(a[length1 - i - 1] > '9' ||a[length1 - i - 1] < '0' || b[length2 - j - 1] > '9' || a[ength2 - j - 1] < '0' )
return "";
result[i+j] += (a[length1 - i - 1] - '0') * ( b[length2 - j - 1] - '0');
}
}
for ( int i = 0 ; i < length1+length2;i++)
{
if ( result[i] > 9 )
{
result[i+1] += result[i]/ 10;
result[i] = result[i] % 10;
}
}
/*char* result2 = new char[length1+length2+1];
if ( result2 == NULL )
return "";
memset(result2, 0, length1+length2+1);*/
int i = length1 + length2;
for ( ; i >= 0 ;i--)
{
if ( result[i] != 0 ){
break;
}
}
for ( int j = 0 ; j <= i/2 ;j++)
{
char ch = result[j];
result[j] = result[i- j] + '0';
result[i-j] = ch + '0';
}
// delete[] result;
return string(result);
}
void reverse(char* input)
{
int length = strlen(input) + 1;
for ( int i = 0 ; i < length/2 ; i++)
{
char ch = input[i];
input[i]= input[length - i - 1];
input[length - i - 1] = ch;
}
}
//void remove(char* input)
//{
// n
//}
// 已知n个元素,如何不用除法计算任意n-1个元素元素的乘积?
int count(int* array, int num, int target)
{
if ( array == NULL || num == 0 )
return 0;
int* a = new int[num];
if ( a == NULL )
return 0;
int* b = new int[num];
if ( b == NULL )
return 0;
a[0] = array[0];
b[0] = array[num - 1];
for ( int i = 1; i <= num ; i++ )
{
a[i] = a[i-1]*array[i];
}
for ( int i = num - 2; i >= 0; i-- )
{
b[num - i - 1] = b[num - i - 2]*array[i];
}
return a[target-2]*b[num-target-1];
}
//实现一个可以O(1)获取最小元素的Stack
//
//typedef struct _node
// {
// int value;
// struct _node* next;
// }Node;
//
//class myStack
//{
//public:
// myStack():size(0), minStack(NULL){};
// bool push(int);
// bool pop(int*);
// bool min(int *);
//private:
// int size;
//
// Node* minStack;
// vector<int> _stack;
//};
//
//bool myStack::push(int value)
//{
// _stack.push_back(value);
// size++;
//
// Node* node = new Node();
// if ( node == NULL )
// return false;
// memset(node, 0, sizeof(Node));
// node->value = value;
//
// if ( minStack == NULL ) {
// minStack = node;
// return true;
// }
// if ( minStack->value >= value )
// {
// node->next = minStack;
// minStack = node;
// return true;
// }
// Node* previous = minStack;
// Node* temp = previous->next;
// while( temp )
// {
// if ( temp->value < value )
// {
// previous = temp;
// temp = temp->next;
// }
// else
// {
// break;
// }
// }
// previous->next = node;
// node->next = temp;
//
// return true;
//}
//
//bool myStack::pop(int* value)
//{
// if ( value == NULL || size == 0 )
// return false;
//
// *value = _stack.back();
// _stack.pop_back();
// size--;
//
// if ( minStack == NULL ) {
// return false;
// }
//
// if ( minStack->value == *value )
// {
// Node* temp = minStack;
// minStack = minStack->next;
// delete temp;
// return true;
// }
//
// Node* previous = minStack;
// Node* temp = previous->next;
// while( temp )
// {
// if ( temp->value != *value )
// {
// previous = temp;
// temp = temp->next;
// }
// else
// {
// break;
// }
// }
// if ( temp != NULL )
// {
// previous->next = temp->next;
// delete temp;
// }
// else
// {
// return false;
// }
// return true;
//}
//
//bool myStack::min(int* value)
//{
// if ( value == NULL )
// return false;
//
// if ( minStack ) {
// *value = minStack->value;
// return true;
// }
// return false;
//}
//
//
//一个以Node节点表示的完全二叉树,如何计算它的最下面一层的叶子节点个数?要求复杂度低于O(N)
namespace test1
{
typedef struct node {
struct node * left;
struct node * right;
}Node;
int count2(Node* node, int max )
{
int tc;
if ( node == NULL )
return 0;
if ( max == 1 )
return max;
int temp = count2(node->right, max/2);
if ( temp != 0 )
{
tc = max/2 + temp;
}
else
{
tc = count2(node->left , max/2);
}
return tc;
}
int count(Node* node)
{
//find the deep;
if (node == NULL )
return 0;
Node* temp = node;
int depth = 0;
while(temp)
{
depth++;
temp = temp->left;
}
int max = 1 << (depth- 1);
count2(node, max);
}
};
int count(int k)
{
vector<int> a;
vector<int> b;
vector<int> c;
a.push_back(2);
b.push_back(3);
c.push_back(5);
int select;
for( int i = 0 ; i < k; i++ )
{
int list;
if ( a.front() < b.front() )
{
select = a.front();
list = 1;
}
else
{
select = b.front();
list = 2;
}
if ( select > c.front() )
{
select = c.front();
list = 3;
}
switch(list)
{
case 1:
a.erase(a.begin());
a.push_back(select*2);
b.push_back(select*3);
c.push_back(select*5);
break;
case 2:
b.erase(b.begin());
b.push_back(select*3);
c.push_back(select*5);
break;
case 3:
c.erase(c.begin());
c.push_back(select*5);
break;
default:
break;
}
}
return select;
}
typedef struct _node
{
struct _node* next;
struct _node* some;
}Node;
//已知一链表,每个节点除了有一个指向下一节点的指针外,还有一随机指针指向链表中的任意节点(可能为空,也有可能为自身),请复制一个链表,
//要求节点的顺序以及节点上的随机指针指向的节点位置和原链表一致(要求O(N)时间复杂度,O(1)空间复杂度)。
Node* build(Node* node)
{
Node* temp = node;
while ( temp )
{
Node* new_node = new Node();
if ( new_node == NULL )
return NULL;
new_node->next = temp->next;
temp->next = new_node;
temp = new_node->next;
}
temp = node;
while( temp )
{
if ( temp->some == NULL )
temp->next->some = NULL;
else if ( temp->some == temp)
temp->next->some = temp->next;
else
temp->next->some = temp->some->next;
}
Node* result = node->next;
temp = result;
while( temp->next )
{
temp->next = temp->next->next;
temp = temp->next;
}
temp->next = NULL;
return result;
}
//void mulitpul(int[][] a, int[][] b, int[][] c, int N)
//{
// if ( a == NULL || b == NULL || c == NULL )
// return;
// for ( int i = 0 ; i < N ; i++ )
// {
// for (int j = 0; j < N ;j++)
// {
// for ( int k = 0; k < N; k++ )
// c[i][j] += a[i][k] * b[K][j];
// }
// }
//}
//
//void maxfff()
//{
// int a[10][10];
// int b[10][10];
// int c[10][10];
// memset(c, 0, sizeof(c));
// mulitpul(a, b, c, 10);
//}
//计算数组中逆序对的个数(a[1..n], i<j, if(a[i]>a[j]),then (i,j)为一个逆序对),要求复杂度低于O(n^2)
int merge(int* a, int start1, int end1, int start2, int end2)
{
int* temp = new int[end2 - start1 + 1];
if ( temp == NULL )
return 0;
int result = 0;
int count1 = start1;
int count2 = start2;
int count3 = 0;
while( count1 <= end1 && count2 <= end2)
{
if ( a[count1] <= a[count2] )
{
temp[count3] = a[count1];
count1++;
count3++;
}
else
{
temp[count3] = a[count2];
count2++;
count3++;
result += end1 - count1 + 1;
}
}
while( count2 <= end2 )
{
temp[count3] = a[count2];
count2++;
count3++;
}
while( count1 <= end1 )
{
temp[count3] = a[count1];
count1++;
count3++;
}
for ( int i = 0 ; i < count3;i++)
{
a[start1 + i] = temp[i];
}
return result;
}
int count1(int* a, int start, int end)
{
if ( start == end )
return 0;
if ( end - start == 1 )
{
if ( a[start] > a[end] )
{
int temp = a[start];
a[start] = a[end];
a[end] = temp;
return 1;
}
else
return 0;
}
int mid = (end - start ) / 2 + start;
return count1(a, start, mid) + count1(a, mid+1, end)
+ merge(a, start, mid , mid+1, end);
}
int atoi(const char* a)
{
}
int main()
{
int input[10] = {3,2,1,5,3,2,6,4,2,6};
int result = count1(input,0,9);
// //int input[5] = {1,2,3,4,5};
// //int result = count(input, 5, 2);
// //printf("result is %d\n", result);
// //string result = count(string("2341235"), string("231234"));
///* char input[10] = "testtestt";
// reverse(input);
// printf("result is %s\n", input);*/
// myStack ms;
//
// ms.push(32);
// ms.push(64);
// ms.push(23);
// ms.push(12);
// ms.push(1);
// int value1 = 0;
// ms.min(&value1);
//
// int value3 = 0;
// ms.pop(&value3);
// int value2 = 0;
// ms.min(&value2);
return 0;
}