-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugus.cpp
More file actions
1763 lines (1585 loc) · 53.4 KB
/
augus.cpp
File metadata and controls
1763 lines (1585 loc) · 53.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by AUGUS on 2021/12/16.
//
#include "augus.h"
using namespace augus;
augus_utils_sptr AugusUtils::instance() {
static augus_utils_sptr p = nullptr;
if (p == nullptr) {
p = std::make_shared<AugusUtils>();
}
return p;
}
// 这不就是replace吗
void AugusUtils::TrimStr(std::string &s, char str) {
std::string::size_type index = 0;
if (!s.empty()) {
while ((index = s.find(str, index)) != std::string::npos) {
s.erase(index, 1);
}
}
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
}
std::string AugusUtils::HandleDateTail(std::string &str) {
// handle like 1980-01-01 23:59:59.5999+0007
// WARNING: 不可交换代码前后位置
std::string tail_str;
size_t index_dot = str.find('.');
size_t index_zone = str.find('+');
bool have_dot = index_dot != std::string::npos;
bool have_zone = index_zone != std::string::npos;
size_t len_dot = 0;
size_t len_zone = 0;
if (have_zone)
len_zone = str.size() - index_zone - 1;
if (have_dot)
len_dot = have_zone ? str.size() - index_dot - 1 - len_zone - 1
: str.size() - index_dot - 1;
std::string mask_zone(len_zone, '0');
std::string mask_dot(len_dot, '0');
if (have_dot)
tail_str += "." + mask_dot;
if (have_zone)
tail_str += "+" + mask_zone;
if (have_zone)
str = str.substr(0, index_zone);
if (have_dot)
str = str.substr(0, index_dot);
return tail_str;
};
/**
* 拼接int
*/
std::string JsonCombine::GetKeyValue(const std::string &str_key, int i_value) {
char tag[] = "\"";
char colon[] = ":";
char value[50] = {0};
std::string str_res;
str_res.append(tag);
str_res.append(str_key);
str_res.append(tag);
str_res.append(colon);
sprintf(value, "%d", i_value);
str_res.append(value);
return str_res;
}
/**
* 拼接float,保留3位
*/
std::string JsonCombine::GetKeyValue(const std::string &str_key, float f_value) {
char tag[] = "\"";
char colon[] = ":";
char value[50] = {0};
std::string str_res;
str_res.append(tag);
str_res.append(str_key);
str_res.append(tag);
str_res.append(colon);
sprintf(value, "%0.3f", f_value);
str_res.append(value);
return str_res;
}
/**
* 拼接string
*/
std::string JsonCombine::GetKeyValue(const std::string &str_key,
const std::string &str_value) {
char tag[] = "\"";
char colon[] = ":";
std::string str_res;
str_res.append(tag);
str_res.append(str_key);
str_res.append(tag);
str_res.append(colon);
str_res.append(tag);
str_res.append(str_value);
str_res.append(tag);
return str_res;
}
/**
* 拼接object
*/
std::string JsonCombine::GetKeyValueObject(const std::string &str_key,
const std::string &str_obj) {
char tag[] = "\"";
char colon[] = ":";
std::string str_res;
str_res.append(tag);
str_res.append(str_key);
str_res.append(tag);
str_res.append(colon);
str_res.append(str_obj);
return str_res;
}
/**
* 拼接array
*/
std::string JsonCombine::GetKeyValueArray(const std::string &str_key,
const std::string &str_arr) {
char tag[] = "\"";
char colon[] = ":";
std::string str_res;
str_res.append(tag);
str_res.append(str_key);
str_res.append(tag);
str_res.append(colon);
str_res.append("[");
str_res.append(str_arr);
str_res.append("]");
return str_res;
}
void JsonCombine::use() {
int value1 = 1;
float value2 = 1.0f;
std::string str_json_res("{");
str_json_res.append(GetKeyValue("key1", value1));
str_json_res.append(",");
str_json_res.append(GetKeyValue("key2", value2));
str_json_res.append("}");
PrintTest(str_json_res);
}
// 插入排序(算法中是直接交换节点,时间复杂度O(n^2),空间复杂度O(1))
ListNode *ListSort::insertionSortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (head == nullptr || head->next == nullptr)
return head;
ListNode *p = head->next, *pstart = new ListNode(0), *pend = head;
pstart->next = head; // 为了操作方便,添加一个头结点
while (p != nullptr) {
ListNode *tmp = pstart->next, *pre = pstart;
// 找到插入位置
while (tmp != p && p->val >= tmp->val) {
tmp = tmp->next;
pre = pre->next;
}
if (tmp == p)
pend = p;
else {
pend->next = p->next;
p->next = tmp;
pre->next = p;
}
p = pend->next;
}
head = pstart->next;
delete pstart;
return head;
}
// 选择排序(算法中只是交换节点的val值,时间复杂度O(n^2),空间复杂度O(1))
ListNode *ListSort::selectSortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
// 选择排序
if (head == nullptr || head->next == nullptr)
return head;
ListNode *pstart = new ListNode(0);
pstart->next = head; // 为了操作方便,添加一个头结点
ListNode *sortedTail = pstart; // 指向已排好序的部分的尾部
while (sortedTail->next != nullptr) {
ListNode *minNode = sortedTail->next, *p = sortedTail->next->next;
// 寻找未排序部分的最小节点
while (p != nullptr) {
if (p->val < minNode->val)
minNode = p;
p = p->next;
}
std::swap(minNode->val, sortedTail->next->val);
sortedTail = sortedTail->next;
}
head = pstart->next;
delete pstart;
return head;
}
// 归并排序(算法交换链表节点,时间复杂度O(nlogn),不考虑递归栈空间的话空间复杂度是O(1))
//
// 首先用快慢指针的方法找到链表中间节点,然后递归的对两个子链表排序,把两个排好序的子链表合并成一条有序的链表。归并排序应该算是链表排序最佳的选择了,保证了最好和最坏时间复杂度都是nlogn,而且它在数组排序中广受诟病的空间复杂度在链表排序中也从O(n)降到了O(1)
ListNode *ListSort::mergeSortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
// 链表归并排序
if (head == NULL || head->next == NULL)
return head;
else {
// 快慢指针找到中间节点
ListNode *fast = head, *slow = head;
while (fast->next != NULL && fast->next->next != NULL) {
fast = fast->next->next;
slow = slow->next;
}
fast = slow;
slow = slow->next;
fast->next = NULL;
// fast = sortList(head);//前半段排序
// slow = sortList(slow);//后半段排序
return merge(fast, slow);
}
}
// merge two sorted list to one
ListNode *ListSort::merge(ListNode *head1, ListNode *head2) {
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
ListNode *res, *p;
if (head1->val < head2->val) {
res = head1;
head1 = head1->next;
} else {
res = head2;
head2 = head2->next;
}
p = res;
while (head1 != NULL && head2 != NULL) {
if (head1->val < head2->val) {
p->next = head1;
head1 = head1->next;
} else {
p->next = head2;
head2 = head2->next;
}
p = p->next;
}
if (head1 != NULL)
p->next = head1;
else if (head2 != NULL)
p->next = head2;
return res;
}
// 冒泡排序(算法交换链表节点val值,时间复杂度O(n^2),空间复杂度O(1))
ListNode *ListSort::bubbleSortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
// 链表快速排序
if (head == NULL || head->next == NULL)
return head;
ListNode *p = NULL;
bool isChange = true;
while (p != head->next && isChange) {
ListNode *q = head;
isChange =
false; // 标志当前这一轮中又没有发生元素交换,如果没有则表示数组已经有序
for (; q->next && q->next != p; q = q->next) {
if (q->val > q->next->val) {
std::swap(q->val, q->next->val);
isChange = true;
}
}
p = q;
}
return head;
}
// 对于希尔排序,因为排序过程中经常涉及到arr[i+gap]操作,其中gap为希尔排序的当前步长,这种操作不适合链表。
//
// 对于堆排序,一般是用数组来实现二叉堆,当然可以用二叉树来实现,但是这么做太麻烦,还得花费额外的空间构建二叉树
ListSortPtr ListSort::instance() {
static ListSortPtr ptr = nullptr;
if (ptr == nullptr) {
ptr = std::make_shared<ListSort>();
}
return ptr;
}
MathPtr Math::instance() {
static MathPtr ptr = nullptr;
if (ptr == nullptr) {
ptr = std::make_shared<Math>();
}
return ptr;
}
//TEST(Math, gcd) {
// auto p = Math::instance();
// EXPECT_EQ(p->gcd(49, 28), 7);
//}
// 常见的随机分布模板类
// 均匀分布:
// uniform_int_distribution 整数均匀分
// uniform_real_distribution 浮点数均匀分布
// 注意,uniform_int_distribution的随机数的范围不是半开范围[ ),而是
// [],对于uniform_real_distribution却是半开范围[ )。
// 伯努利类型分布:(仅有yes/no两种结果,概率一个p,一个1-p)
// bernoulli_distribution 伯努利分布
// binomial_distribution 二项分布
// geometry_distribution 几何分布
// negative_biomial_distribution 负二项分布
// Rate-based distributions:
// poisson_distribution 泊松分布
// exponential_distribution指数分布
// gamma_distribution 伽马分布
// weibull_distribution 威布尔分布
// extreme_value_distribution 极值分布
// 正态分布相关:
// normal_distribution 正态分布
// chi_squared_distribution卡方分布
// cauchy_distribution 柯西分布
// fisher_f_distribution 费歇尔F分布
// student_t_distribution t分布
// 分段分布相关:
// discrete_distribution离散分布
// piecewise_constant_distribution分段常数分布
// piecewise_linear_distribution分段线性分布
void Math::getRand() {
// 生成random_device对象sd做种子
std::random_device sd;
// 使用种子初始化linear_congruential_engine对象,
// 为的是使用它来做我们下面随机分布的种子以输出多个随机分布.
// 注意这里要使用()操作符,因为minst_rand()接受的是一个值(你用srand也是给出这样的一个值)
std::minstd_rand linearRan(sd());
// 生成01序列
std::uniform_int_distribution<int> dis1(0, 1);
std::cout << "\nuniform_int_distribution:";
for (int i = 0; i < 100; i++) {
// 使用linear engine做种子,
// 注意这里传入的不是一个值而是一个引擎:
// 【随机分布函数需要传入一个随机数引擎作为参数,其实random_device也是一个引擎,这里把sd传入也不会出错】
std::cout << dis1(linearRan) << " ";
}
std::cout << "\n";
}
////////////////////////////////////////////////////////////////////////////////////////////////
basic_cpp::ComplexDefine::ComplexDefine() {
void *(*(*fp1)(int))[10];
float (*(*fp2)(int, int, int))(int);
// int (*( *fp355)())[10]();
int id[sizeof(unsigned long)];
}
basic_cpp::ComplexDefine::~ComplexDefine() = default;
basic_cpp::OPChar::OPChar() {
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char *str5 = "abc";
const char *str6 = "abc";
// char *str7 = "abc"; //warning in c++11
// char *str8 = "abc";
std::cout << (str1 == str2) << std::endl;
std::cout << (str3 == str4) << std::endl;
std::cout << (str5 == str6) << std::endl;
// std::cout << (str7 == str8) << std::endl;
}
int basic_cpp::OPChar::opWchar() {
wchar_t szName1[16] = {'1', '2', '3', '4', '5', '7', '7'},
szName2[16] = {'1', '2', '3', '4', '5'};
auto res = wcscmp(szName2, szName1);
std::cout << "compare res " << res << std::endl;
return 0;
}
basic_cpp::OPSwap::OPSwap(int *a, int *b) {
for (auto i = 0; i < 5; i++) {
std::swap(*(a + i), *(b + i));
}
}
int basic_cpp::OPSwap::test() {
int a0929[] = {0, 1, 3, 5, 8}, b0929[] = {2, 4, 9, 12, 31};
OPSwap(a0929, b0929);
return 0;
}
int basic_cpp::SomeSort::libFuc() {
int a[20] = {2, 4, 1, 1, 2, 23, 5, 76, 0, 43, 24, 65}, i;
for (i = 0; i < 20; i++) {
std::cout << a[i] << " ";
}
std::cout << std::endl;
sort(a, a + 20, std::greater_equal<int>());
for (i = 0; i < 20; i++) {
std::cout << a[i] << " ";
}
return 0;
}
bool op_list::op_array::Duplicate::isContainsDuplicate(const std::vector<int> &v) {
std::unordered_set<int> s(v.size() * 2);
for (auto x : v) {
if (!s.insert(x)
.second) { /// insert failed -> repeat insert,conclude have duplicate
return true;
}
}
return false;
}
int op_list::op_array::Duplicate::findDuplicateElements(int *arr, int length,
std::set<int> s,
std::vector<int> &output) {
if (arr == nullptr || length <= 0) {
/// 数组为空
return -1;
}
for (int i = 0; i < length; i++) {
/// 为什么要让 arr[i] > length - 1 ?????
if (arr[i] < 0 /*|| arr[i] > length - 1*/) {
/// 输入数据超出范围
return -1;
}
}
std::sort(arr, arr + length);
int k = 0, j = 0;
/// 拿数组中第k个位置和第j+1个位置的数据相比,只能是arr[k] <= arr[j + 1]
while (k < length - 1 && j < length - 1) {
/// arr[k] < arr[j + 1]的时候,只存在两种情况k == j或k < j
if (arr[k] < arr[j + 1]) {
if (k == j) {
k++;
j++;
}
/// k<j时,就让k+1
else {
k++;
}
}
/// arr[k] = arr[j + 1],就把arr[k]插入到集合中,避免出现多次的数据重复出现
else {
/// 重复数字为 arr[k]
s.insert(arr[k]);
j++;
}
}
std::set<int>::iterator it;
for (it = s.begin(); it != s.end(); it++) /// 使用迭代器对集合进行遍历
{
output.push_back(*it);
}
return 0;
}
int op_list::op_array::Duplicate::Test() {
/// Record 4 First to know set and Finding duplicate value. Duplicate times just using
/// <count> method
std::cout << "\nRecord 4 First to know set and Finding duplicate value. Duplicate "
"times just "
"using <count> method\n";
std::set<int> set_temp;
const int kDuplicateTemp[] = {6, 5, 12, 94, 12, 15, 15, 3, 6, 5, 5,
12, 12, 3, 1, 5, 3, 4, 3, 2, 7, 5};
int duplicate_temp[] = {6, 5, 12, 94, 12, 15, 15, 3, 6, 5, 5,
12, 12, 3, 1, 5, 3, 4, 3, 2, 7, 5};
std::vector<int> vec_output;
findDuplicateElements(duplicate_temp,
sizeof(duplicate_temp) / sizeof(duplicate_temp[0]), set_temp,
vec_output);
std::cout << "\niterator Traverse -> ";
for (int &iter : vec_output) { /// auto -> std::vector<int>::iterator
std::cout << iter << " ";
}
std::cout << std::endl;
std::vector<int> vec_duplicate_temp(
duplicate_temp,
duplicate_temp + sizeof(duplicate_temp) / sizeof(duplicate_temp[0]));
for (auto i : vec_output) {
std::cout << "duplicate value <" << i << "> has "
<< count(vec_duplicate_temp.begin(), vec_duplicate_temp.end(), i)
<< " repeats" << std::endl;
}
return 0;
}
// 寻找元素在Vector的位置
int op_list::op_array::VectorCtrl::findPosVector(std::vector<int> input, int number) {
auto iter = find(input.begin(), input.end(), number); // 返回的是一个迭代器指针
if (iter == input.end()) {
return -1;
} else {
return distance(input.begin(), iter);
}
}
int op_list::op_array::VectorCtrl::findVectorSub() {
/// Record 5 finding subscript
std::vector<int> vec_test2 = {5, 6, 8, 5, 4, 3, 2};
std::cout << std::endl << "Record 5 Finding subscript in std::vector" << std::endl;
std::vector<int> vecFind = vec_test2;
const int findVal = 5;
auto isFind = find(vecFind.begin(), vecFind.end(),
findVal); /// auto -> std::vector<int>::iterator
if (isFind != vecFind.end()) {
std::cout << "I can find this value -> " << findVal << std::endl;
int res = isFind - vecFind.begin(); /// res即是target在vector数组中的下标
int indexBack = &*isFind - &vecFind[0]; /// other way
std::cout << " -> It's subscript is " << res << std::endl;
std::cout << " -> Other way find the subscript is " << indexBack << std::endl;
} else {
std::cout << "I can't find this value -> " << findVal << std::endl;
}
vecFind.at(25);
vecFind.at(5);
return 0;
}
// 容器vector中元素的去重
std::vector<int> op_list::op_array::VectorCtrl::unique_element_in_vector(
std::vector<int> v) {
std::vector<int>::iterator vector_iterator;
sort(v.begin(), v.end());
vector_iterator = unique(v.begin(), v.end());
if (vector_iterator != v.end()) {
v.erase(vector_iterator, v.end());
}
return v;
}
// 两个vector求交集
std::vector<int> op_list::op_array::VectorCtrl::vectors_intersection(
std::vector<int> v1, std::vector<int> v2) {
std::vector<int> v;
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),
back_inserter(v)); // 求交集
return v;
}
// 两个vector求并集
std::vector<int> op_list::op_array::VectorCtrl::vectors_set_union(std::vector<int> v1,
std::vector<int> v2) {
std::vector<int> v;
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v)); // 求交集
return v;
}
op_list::op_array::ArrayLength::ArrayLength() {
/// Record 2 Compute Array Length
/// array length Two-dimensional array
std::cout << std::endl << "Record 2 Compute Array Length" << std::endl;
int two_dimensional_array[3][5] = {0};
int len = sizeof(two_dimensional_array) / sizeof(int);
int second_dimension_length =
sizeof(two_dimensional_array[0]) / sizeof(two_dimensional_array[0][0]);
int first_dimension_length = len / second_dimension_length;
/// 不要想着去在二维数组入参的时候求长度,能求出来个锤子???
/// 二维数组寻址方式
/// 对于数组 int p[m][n];
/// 如果要取 p[i][j] 的值(i>=0 && m<=0 && j>=0 &&
/// n<=0),编译器是这样寻址的,它的地址为:
/// p + i*n + j;
/// 所以不能省略第二维,省去编译器将不知道如何正确的寻址
std::cout << "This array -> First dimension length is " << first_dimension_length
<< ", Second dimension length is " << second_dimension_length << std::endl;
}
int op_list::op_array::LC::pivotIndex(std::vector<int> &nums) {
if (nums.empty()) {
return -1;
}
int sumL = 0, sumR = 0;
for (auto val : nums) {
sumR += val;
}
for (auto i = 0; i < nums.size(); i++) {
if (i - 1 >= 0) {
sumL += nums[i - 1];
}
sumR -= nums[i];
if (sumR == sumL) {
return i;
}
}
return -1;
}
int op_list::op_array::LC::searchInsert(std::vector<int> &nums, int target) {
if (nums.empty()) {
return 0;
}
int res = 0;
for (auto i = 0; i < nums.size(); i++) {
if (target == nums[i]) {
return i;
}
if (target > nums[i]) {
res = i + 1;
}
}
return res;
}
void op_list::op_array::LC::rotate(std::vector<std::vector<int>> &matrix) {
// 上下翻转
for (auto i = 0; i < matrix.size() / 2; i++) {
for (auto j = 0; j < matrix.size(); j++) {
std::swap(matrix[i][j], matrix[matrix.size() - 1 - i][j]);
}
}
// 对角翻转
for (auto i = 0; i < matrix.size(); i++) {
for (auto j = 0; j <= i; j++) {
std::swap(matrix[i][j], matrix[j][i]);
}
}
}
void op_list::op_array::LC::setZeroes(std::vector<std::vector<int>> &matrix) {
int M = matrix.size(), N = matrix[0].size();
std::vector<int> A, B;
for (auto i = 0; i < M; i++) {
for (auto j = 0; j < N; j++) {
if (!matrix[i][j]) {
A.push_back(i);
B.push_back(j);
}
}
}
for (auto a : A) {
for (auto j = 0; j < N; j++) {
matrix[a][j] = 0;
}
}
for (auto b : B) {
for (auto i = 0; i < M; i++) {
matrix[i][b] = 0;
}
}
}
std::string op_list::op_array::LC::longestCommonPrefix(std::vector<std::string> &strs) {
if (strs.empty()) {
return "";
}
std::string res;
for (int i = 0; i < strs[0].size(); i++) {
auto s = strs[0][i];
res += s;
for (auto subStr : strs) {
if (subStr[i] != s) {
res.erase(res.size() - 1);
return res;
}
}
}
return res;
}
void op_list::op_array::LC::test() {
std::vector<std::string> as = {"flower", "flow", "flight"};
std::string s = longestCommonPrefix(as);
std::string s2 = s;
}
std::vector<int> op_list::op_array::mergeTest(std::vector<int> &nums1, unsigned m,
std::vector<int> &nums2, unsigned n) {
std::vector<int> res(m + n);
unsigned p1 = m - 1, p2 = n - 1, p = m + n - 1;
while ((p1 >= 0) && (p2 >= 0)) {
res[p--] = (nums1[p1] > nums2[p2]) ? nums1[p1--] : nums2[p2--];
}
while (p1 >= 0) {
res[p--] = nums1[p1--];
}
while (p2 >= 0) {
res[p--] = nums2[p2--];
}
return res;
}
int op_list::op_array::findMaxInArray() {
std::vector<int> v{6, 54, 31, 62, 23, 46, 89, 8};
auto biggest = max_element(begin(v), end(v));
/// or std::vector<int>::iterator biggest = max_element(v.begin(), v.end);
std::cout << "Max element is " << *biggest << " at position "
<< distance(begin(v), biggest) << std::endl;
/// 另一方面,取最大位置也可以这样来写:
/// int nPos = (int)(max_element(v.begin(), v.end()) - (v.begin());
/// 效果和采用distance(...)函数效果一致
/// 说明:max_element(v.begin(), v.end()) 返回的是vector<int>::iterator,
/// 相当于指针的位置,减去初始指针的位置结果即为最大值得索引。
auto smallest = min_element(begin(v), end(v));
std::cout << "min element is " << *smallest << " at position "
<< distance(begin(v), smallest) << std::endl;
/// 对于普通数组
/// 总体实现:(索引值是通过计算数组的起始地址和最大值或最小值的地址之间距离的来计算的)
int TempArr[] = {2, 3, 1, 6, 7, 3};
/// 求数组最大值以及最大值的索引
std::cout << "Max element: "
<< *std::max_element(TempArr,
TempArr + sizeof(TempArr) / sizeof(TempArr[0]))
<< "\n";
std::cout << "Max element location: "
<< std::distance(
TempArr, std::max_element(TempArr, TempArr + sizeof(TempArr) /
sizeof(TempArr[0])))
<< "\n";
/// 求数组最小值以及最小值的索引
std::cout << "Min element: "
<< *std::min_element(TempArr,
TempArr + sizeof(TempArr) / sizeof(TempArr[0]))
<< "\n";
std::cout << "Min element location: "
<< std::distance(
TempArr, std::min_element(TempArr, TempArr + sizeof(TempArr) /
sizeof(TempArr[0])))
<< "\n";
/// 求数组的和
std::valarray<int> ValTempArr(TempArr, sizeof(TempArr) / sizeof(TempArr[0]));
std::cout << ValTempArr.sum();
return 0;
}
int op_list::op_array::commonVectorOP() {
std::cout << std::endl;
std::cout << "BELOW ARE VECTOR COMMON OPERATE ->\n";
std::cout << "vec_test1 source->";
std::vector<int> vec_test1 = {0, 1, 2, 5, 6, 9, 8, 2, 0, 7, 55, 24, 3};
auto begin = vec_test1.begin(), end = vec_test1.end();
while (begin != end) {
*begin = 0;
++begin;
}
PrintTest(vec_test1);
/// case 1 copy
const std::vector<int> &vec_test2 = vec_test1;
std::cout << "case 1 copy -> vec_test2 copy from vec_test1";
PrintTest(vec_test2);
/// case 2 copy
std::vector<int> vec_test3(vec_test1);
std::cout << "case 2 copy -> vec_test3 copy from vec_test1";
PrintTest(vec_test3);
/// std::swap vec
std::vector<int> vec_test4 = {0, 1, 2, 5, 6, 9, 8, 2, 0, 7};
/// clear vec_test4
std::cout << "vec_test4 clear before->";
PrintTest(vec_test4);
std::vector<int>().swap(vec_test4);
std::cout << "vec_test4 cleared";
PrintTest(vec_test4);
std::vector<int> vec_test5 = {
0, 1, 2, 5, 6,
};
std::cout << "vec_test5 source->";
PrintTest(vec_test5);
vec_test5.swap(vec_test1);
std::cout << "std::swap vec_test5 vec_test1-> Now vec_test5";
PrintTest(vec_test5);
/// assign
std::vector<int> vec_test6 = {0, 1, 2, 5, 6, 8, 56};
std::cout << "vec_test6 source";
PrintTest(vec_test6);
std::cout << "vec_test6 assign vec_test1";
vec_test6.assign(vec_test1.begin(), vec_test1.end());
PrintTest(vec_test6);
std::cout << "vec_test6 set 3 0s";
vec_test6.assign(3, 0);
PrintTest(vec_test6);
/// concat std::vector
int arr_test0817[][13] = {
{0, 1, 2, 5, 6, 9, 8, 2, 0, 7, 55, 24, 3},
{5, 1, 2, 5, 6, 9, 8, 2, 0, 7, 55, 24, 3},
{21, 1, 2, 5, 6, 9, 8, 2, 0, 7, 55, 24, 3},
};
std::vector<int> vec_res0817;
for (auto &i : arr_test0817) {
vec_res0817.insert(vec_res0817.end(), i, i + 13);
}
std::cout << "vec_res0817";
PrintTest(vec_res0817);
return 0;
}
int op_list::op_array::switchArr2Vec() {
/// Record 3 Switch Arr to Vec
std::cout << std::endl << "Record 3 Switch Arr to Vec" << std::endl;
int hand_card[] = {0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x18, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
0x3D, 0x4E, 0x4F, 0x05, 0x08, 0x09, 0x0A, 0x0B};
const int kCountPutBack = 8;
std::vector<int> vec_hand_card(hand_card,
hand_card + sizeof(hand_card) / sizeof(hand_card[0]));
/// sort
sort(vec_hand_card.begin(), vec_hand_card.end()); /// Ascend
sort(vec_hand_card.rbegin(), vec_hand_card.rend()); /// Descend
reverse(vec_hand_card.rbegin(), vec_hand_card.rend()); /// change to back
std::vector<int> vec_put_back_card;
vec_put_back_card.reserve(kCountPutBack);
for (int i = 0; i < kCountPutBack; i++) {
vec_put_back_card.push_back(vec_hand_card[i]);
}
// int *put_back_card = new int[8];
int put_back_card[kCountPutBack];
if (!vec_put_back_card.empty()) {
memcpy(put_back_card, &vec_put_back_card[0],
vec_put_back_card.size() * sizeof(vec_put_back_card[0])); /// copy all
memcpy(put_back_card, &vec_put_back_card[5], kCountPutBack - 5); /// copy last 3
memcpy(put_back_card, &vec_put_back_card[0], 3); /// copy first 3
}
std::cout << "smallest back card is \n";
for (auto i : put_back_card) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
//[1,2,2,3,1,4,2]
int op_list::op_array::findShortestSubArray(std::vector<int> &nums) {
std::map<int, std::vector<int>> NumPos;
for (int i = 0; i < nums.size(); ++i) {
auto it2 = NumPos.find(nums[i]);
if (it2 != NumPos.end()) {
it2->second.push_back(i);
} else {
NumPos.insert(std::pair<int, std::vector<int>>(nums[i], {i}));
}
}
std::vector<int> MaxCount;
for (auto it = NumPos.begin(); it != NumPos.end(); ++it) {
if (MaxCount.empty()) {
MaxCount.push_back(it->first);
} else {
if (NumPos[MaxCount[0]].size() == NumPos[it->first].size()) {
MaxCount.push_back(it->first);
} else {
if (NumPos[MaxCount[0]].size() < NumPos[it->first].size()) {
MaxCount.clear();
MaxCount.push_back(it->first);
}
}
}
}
int Min = 50000;
for (int &i : MaxCount) {
auto V = NumPos[i];
int Dis = V[V.size() - 1] - V[0];
Min = std::min(Min, Dis);
}
return Min + 1;
}
int op_list::commonOP() {
int valueInitial[13] = {0x2b, 0x2a, 0x29, 0x19, 0x18, 0x1a, 0x1b,
0x1c, 0x06, 0x05, 0x04, 0x03, 0x02};
/// list 使用
std::cout << "list exercise" << std::endl;
std::list<int> list(valueInitial, valueInitial + 13);
auto listBegin = list.begin();
for (int i = 0; i < 12; i++) {
++listBegin;
}
std::cout << *(listBegin) << std::endl;
std::cout << "there is the copy nature?" << std::endl;
int test09093[13] = {0};
for (int i = 0; i < 13; i++) {
*(test09093 + i) = *(valueInitial + i);
}
for (int i : test09093) {
std::cout << i << " ";
}
std::vector<int> test09091221(valueInitial, valueInitial + 13);
PrintTest(test09091221);
std::cout << "\nWHICH ONE\n";
std::vector<int> test09091222(&valueInitial[0], &valueInitial[0] + 13);
test09091222.capacity();
std::vector<int>().swap(test09091222);
int count_del = 4;
int cbCardData[50] = {0};
if (!test09091222.empty()) {
memcpy(cbCardData, &test09091222[0], 50 - count_del);
}
PrintTest(test09091222);
return 0;
}
using BINARY_TREE_BASIC_OP = binary_tree::BinaryTree;
void BINARY_TREE_BASIC_OP::preOrderRecur(TreeNode *head) {
if (head == nullptr) {
return;
}
std::cout << head->value << ",";
preOrderRecur(head->left);
preOrderRecur(head->right);
}
void BINARY_TREE_BASIC_OP::inOrderRecur(TreeNode *head) {
if (head == nullptr) {
return;
}
inOrderRecur(head->left);
std::cout << head->value << ",";
inOrderRecur(head->right);
}
void BINARY_TREE_BASIC_OP::posOrderRecur(TreeNode *head) {
if (head == nullptr) {
return;
}
posOrderRecur(head->left);
posOrderRecur(head->right);
std::cout << head->value << ",";
}
void BINARY_TREE_BASIC_OP::preOrderUnRecur(TreeNode *head) {
if (head == nullptr) {
return;
}
std::stack<TreeNode *> nstack;
nstack.push(head);
while (!nstack.empty()) {
TreeNode *head = nstack.top(); // get std::stack top
std::cout << head->value << ",";
nstack.pop();
if (head->right != nullptr) {
nstack.push(head->right);
}
if (head->left != nullptr) {
nstack.push(head->left);
}
}
}
void BINARY_TREE_BASIC_OP::inOrderUnRecur(TreeNode *head) {
if (head == nullptr) {
return;
}
std::stack<TreeNode *> nstack;
while (!nstack.empty() || head != nullptr) {
if (head != nullptr) {
nstack.push(head);
head = head->left;