Skip to content

Commit 5487396

Browse files
committed
update Algorithm-DataStructure/README.md
1 parent 0f26c89 commit 5487396

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

Algorithm-DataStructure/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Tips in Algorithms & Data Structures
22

3-
Note that in this folder each file is more like a document mixed with code. :)
3+
Note that in this folder each file is more like a document mixed with code. Ignore the .cpp extension. :)
44

55
Below is a glimpse of some common C++ containers:
66
![C++ Containers](./CppContainerComplexity.png "C++ Containers")
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/********************************************
2+
Collections of tips learned from experience
3+
*********************************************/
4+
5+
/*
6+
very useful headers
7+
*/
8+
#include <iostream>
9+
#include <vector>
10+
#include <string>
11+
#include <stack>
12+
#include <queue>
13+
#include <list>
14+
#include <unordered_map>
15+
#include <unordered_set>
16+
#include <algorithm>
17+
#include <cmath>
18+
#include <climits>
19+
20+
// or an all-in-one trick
21+
//#include<bits/stdc++.h>
22+
23+
using namespace std;
24+
typedef long long ll;
25+
26+
27+
// remove duplicates and keep unique items ---> sort, unique
28+
sort(data.begin(), data.end()); // sort first s.t. duplicates are consective
29+
data.resize(unique(data.begin(), data.end()) - data.begin()); // unique returns the iterator after the last unremoved item
30+
31+
32+
// lexicography order --> sort the vector of string or char
33+
34+
35+
// find the min or max of a set which keeps changing ---> heap (C++ uses priority_queue)
36+
priority_queue<int> max_heap(data.begin(), data.end()); // priority_queue is a max heap by defalut
37+
priority_queue<int, vector<int> greater<int> > min_heap(data.begin(), data.end()); // change priority_queue to a min heap;
38+
39+
40+
// type conversion/casting
41+
string str = to_string(123); // num to string
42+
int num = stoi(str); // string to num: stoi needs header <string>
43+
int num = atoi(str.c_str()); // string to num: atoi, atoll needs header <cstdlib>
44+
char c = '0' + 6; // char of digit: c = '6'
45+
int i = c - 'a'; // interval between chars: if c = 'b', i = 1
46+
47+
48+
49+
// dfs is feasible if the factorial(n) < 10^9, thus n < 13.
50+
12! ~ 10^8
51+
13! ~ 10^9
52+
53+
54+
// fill 0 -> (n-1) in a vector
55+
vector<int> v(n);
56+
iota(v.begin(), v.end(), 0); // 0 is the starting value
57+
58+
59+
// number base conversion
60+
vector<int> digits;
61+
for (int m = n; m != 0; m /= 10) {
62+
digits.push_back(m % 10);
63+
}
64+
65+
66+

0 commit comments

Comments
 (0)