├── 0-0.cpp ├── 1-1 ├── 1-1.cpp ├── 1-1.o ├── 1-2 ├── 1-2.cpp ├── 1-2.o ├── 1-3 ├── 1-3.cpp ├── 1-3.o ├── 1-4 ├── 1-4.cpp ├── 1-4.o ├── 1-5 ├── 1-5.cpp ├── 1-5.o ├── 1-6 ├── 1-6.cpp ├── 1-6.o ├── 1-7 ├── 1-7.cpp ├── 1-7.o ├── 1-8 ├── 1-8.cpp ├── 1-8.o ├── 10-4 ├── 10-4.cpp ├── 10-4.o ├── 11-1 ├── 11-1.cpp ├── 11-1.o ├── 11-2 ├── 11-2.cpp ├── 11-2.o ├── 11-6 ├── 11-6.cpp ├── 11-6.o ├── 13-1 ├── 13-1.cpp ├── 13-1.o ├── 13-1f.txt ├── 2-1 ├── 2-1.cpp ├── 2-1.o ├── 2-2 ├── 2-2.cpp ├── 2-2.o ├── 2-3 ├── 2-3.cpp ├── 2-3.o ├── 2-4 ├── 2-4.cpp ├── 2-4.o ├── 2-5 ├── 2-5.cpp ├── 2-5.o ├── 2-6 ├── 2-6.cpp ├── 2-6.o ├── 2-7 ├── 2-7.cpp ├── 2-7.o ├── 3-1.txt ├── 3-2 ├── 3-2.cpp ├── 3-2.o ├── 3-3 ├── 3-3.cpp ├── 3-3.o ├── 3-4 ├── 3-4.cpp ├── 3-4.o ├── 3-5 ├── 3-5.cpp ├── 3-5.o ├── 3-6 ├── 3-6.cpp ├── 3-6.o ├── 3-7 ├── 3-7.cpp ├── 3-7.o ├── 4-1 ├── 4-1.cpp ├── 4-1.o ├── 4-3 ├── 4-3.cpp ├── 4-3.o ├── 4-4 ├── 4-4.cpp ├── 4-4.o ├── 4-5 ├── 4-5.cpp ├── 4-5.o ├── 4-7 ├── 4-7.cpp ├── 4-7.o ├── 4-8 ├── 4-8.cpp ├── 4-8.o ├── 5-1 ├── 5-1.cpp ├── 5-1.o ├── 5-2 ├── 5-2.cpp ├── 5-2.o ├── 5-5 ├── 5-5.cpp ├── 5-5.o ├── 5-6 ├── 5-6.cpp ├── 5-6.o ├── 6-1.txt ├── 9-10 ├── 9-10.cpp ├── 9-10.o ├── 9-10v2 ├── 9-10v2.cpp ├── 9-10v2.o ├── 9-2 ├── 9-2.cpp ├── 9-2.o ├── 9-3 ├── 9-3.cpp ├── 9-3.o ├── 9-4 ├── 9-4.cpp ├── 9-4.o ├── 9-5 ├── 9-5.cpp ├── 9-5.o ├── 9-6 ├── 9-6.cpp ├── 9-6.o ├── 9-8 ├── 9-8.cpp ├── 9-8.o ├── 9-9 ├── 9-9.cpp ├── 9-9.o ├── README.md └── readme /0-0.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution{ 7 | public: 8 | 9 | }; 10 | 11 | int main(){ 12 | 13 | Solution sol; 14 | 15 | 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /1-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-1 -------------------------------------------------------------------------------- /1-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution{ 7 | public: 8 | bool isunique1(string str){ 9 | for (int i=0;i> str; 36 | cout << str << ": "; 37 | sol.isunique1(str) ? (cout << "true" << endl) : (cout << "false" << endl); 38 | cout << str<< ": "; 39 | sol.isunique2(str) ? (cout << "true" << endl) : (cout << "false" << endl); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1-1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-1.o -------------------------------------------------------------------------------- /1-2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-2 -------------------------------------------------------------------------------- /1-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void reverse(char* str){ 6 | char* ed = str; 7 | char* st = str; 8 | while (*ed) { ed++; } 9 | ed--; 10 | while (st "; 23 | reverse(str); 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /1-2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-2.o -------------------------------------------------------------------------------- /1-3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-3 -------------------------------------------------------------------------------- /1-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isAnag(string &str1, string &str2){ 7 | if (str1.size()!=str2.size()){return false;} 8 | int *A = new int[256]; 9 | for (int i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution{ 7 | public: 8 | void replace(string &str){ 9 | int sz = str.size(); 10 | int c = 0; 11 | if (sz == 0 ) {return;} 12 | for (int i = sz-1;i>=0;i--){ 13 | if (str[i]==' ') {c++;} 14 | } 15 | int j = sz + c*2; 16 | str = str+string(c*2,' ') ; 17 | for (int i = sz-1;i>=0;i--){ 18 | if (str[i]!=' ') { 19 | str[--j]=str[i]; 20 | }else{ 21 | str[--j] ='%'; 22 | str[--j]='0'; 23 | str[--j]='2'; 24 | } 25 | } 26 | } 27 | }; 28 | 29 | int main(){ 30 | 31 | Solution sol; 32 | string str = "I am programming c++ language. That's good!" ; 33 | cout << str << endl; 34 | sol.replace(str); 35 | cout << str << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1-4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-4.o -------------------------------------------------------------------------------- /1-5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-5 -------------------------------------------------------------------------------- /1-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | class Solution{ 8 | public: 9 | string compress(string str){ 10 | string res=""; 11 | if (str.size()==0) {return str;} 12 | char last = str[0]; 13 | int count = 0; 14 | for (int i=0;i1){ 20 | string cst = ""; 21 | while(count!=0){ 22 | cst = char(count%10+'0')+cst; 23 | count = count/10; 24 | } 25 | res = res+cst; 26 | } 27 | count = 1; 28 | last = str[i]; 29 | } 30 | } 31 | string cst = ""; 32 | if (count >1){ 33 | while(count!=0){ 34 | cst = char(count%10+'0')+cst; 35 | count = count/10; 36 | } 37 | } 38 | res = res + last + cst; 39 | return res; 40 | } 41 | 42 | 43 | }; 44 | 45 | int main(){ 46 | Solution sol; 47 | //string str1 = "aaaabbcccddeeaaaaaaaavbaaaa"; 48 | string str1 = "abcdefg"; 49 | string str2 = sol.compress(str1); 50 | cout << str1 << endl; 51 | cout << str2 << endl; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /1-5.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-5.o -------------------------------------------------------------------------------- /1-6: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/1-6 -------------------------------------------------------------------------------- /1-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution{ 7 | public: 8 | void rotate(int a[][5], int n){ 9 | for (int layer = 0; layer 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution{ 7 | public: 8 | void rotate(int a[][5], int n){ 9 | bool col=true; 10 | bool row=true; 11 | for (int i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution{ 7 | public: 8 | bool check(string str1, string str2){ 9 | str1 = str1 + str1; 10 | if (str1.find(str2)==-1) 11 | return false; 12 | else 13 | return true; 14 | } 15 | }; 16 | 17 | int main(){ 18 | 19 | Solution sol; 20 | string str1 = "erbottlewat"; 21 | string str2 = "waterbottle"; 22 | 23 | if (sol.check(str1,str2)) 24 | cout << str1 << " is a rotation of " << str2 < 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void genArr(int arr[], int N){ 7 | int n = 20; //nubmer of duplicates 8 | int *dup = new int[n]; 9 | cout << "GroundTruth: "; 10 | for (int i=0;i bs; // 4 kilo-bytes 33 | bs.reset(); 34 | 35 | int* arr = new int[N]; 36 | genArr(arr,N); 37 | 38 | cout << "PredicteRes: " ; 39 | 40 | for (int i=0;i 2 | using namespace std; 3 | 4 | 5 | void merge2(int A[], int na, int B[], int nb){ 6 | int ia = na-1; 7 | int ib = nb-1; 8 | int idx = na+nb-1; 9 | 10 | while (ia>=0 && ib>=0){ 11 | A[idx--]=A[ia]>B[ib]?A[ia--]:B[ib--]; 12 | } 13 | 14 | while (ib>=0){ 15 | A[idx--]=B[ib--]; 16 | } 17 | 18 | } 19 | 20 | 21 | int main(){ 22 | int A[12] = {1,3,5,6,9,10,0,0,0,0,0,0}; 23 | int B[6] = {2,4,7,8,11,12}; 24 | 25 | merge2(A,6,B,6); 26 | 27 | cout << "Merged: "; 28 | for (int i=0;i<12;i++){ 29 | cout << A[i] << " "; 30 | } 31 | cout < 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | 8 | bool myfunc(string a, string b){ 9 | sort(a.begin(),a.end()); 10 | sort(b.begin(),b.end()); 11 | return (a &str_arr){ 15 | sort(str_arr.begin(),str_arr.end(),myfunc); 16 | } 17 | 18 | 19 | 20 | 21 | int main(){ 22 | vector str_arr; 23 | str_arr.push_back("bcdde"); 24 | str_arr.push_back("abcde"); 25 | str_arr.push_back("aaaaa"); 26 | str_arr.push_back("bcdec"); 27 | str_arr.push_back("adbce"); 28 | str_arr.push_back("bdcce"); 29 | str_arr.push_back("ccccc"); 30 | str_arr.push_back("aaacc"); 31 | str_arr.push_back("ddecb"); 32 | str_arr.push_back("ccaaa"); 33 | 34 | 35 | sortArr(str_arr); 36 | 37 | for (int i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | void findEle(const vector > &matrix,int x,int &row,int &col){ 9 | int nrow = matrix.size(); 10 | if (nrow==0){return;} 11 | int ncol = matrix[0].size(); 12 | 13 | int i=0; 14 | int j=ncol-1; 15 | 16 | 17 | while (i=0){ 18 | if (matrix[i][j]==x){ 19 | row=i; 20 | col=j; 21 | return; 22 | }else{ 23 | if (matrix[i][j] > matrix(4,vector(4,0)); 43 | cout << "Matrix: " << endl; 44 | for (int i=0;i<4;i++){ 45 | for (int j=0;j<4;j++){ 46 | matrix[i][j]=A[i][j]; 47 | cout << matrix[i][j] << " " ; 48 | } 49 | cout << endl; 50 | } 51 | 52 | int row=-1; 53 | int col=-1; 54 | int x = 55; 55 | cout << "Finding element: "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class ListNode{ 7 | public: 8 | int val; 9 | ListNode* next; 10 | ListNode(int v): val(v),next(NULL){} 11 | }; 12 | 13 | void deletedup(ListNode* head){ 14 | ListNode* p=head; 15 | ListNode* q=head; 16 | while (p->next){ 17 | bool f = true; 18 | while(q!=p->next){ 19 | if (p->next->val==q->val){ 20 | p->next=p->next->next; 21 | f = false; 22 | break; 23 | } 24 | q=q->next; 25 | } 26 | if (f) {p=p->next;} 27 | q=head; 28 | } 29 | 30 | } 31 | 32 | 33 | int main(){ 34 | ListNode* head = new ListNode(1); 35 | ListNode* q=head; 36 | int a[15] = {1,2,3,4,5,6,7,4,5,2,10,8,9,0,0}; 37 | //int a[15] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; 38 | for (int i=0;i<15;i++){ 39 | ListNode* p=new ListNode(a[i]); 40 | q->next = p; 41 | q=p; 42 | } 43 | 44 | deletedup(head); 45 | if (head==NULL){cout << "Empty List" << endl; return 0;} 46 | while (head!=NULL){ 47 | cout << head->val << " "; 48 | head = head->next; 49 | } 50 | cout << endl; 51 | 52 | return 0; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /2-1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-1.o -------------------------------------------------------------------------------- /2-2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-2 -------------------------------------------------------------------------------- /2-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class ListNode{ 6 | public: 7 | int val; 8 | ListNode* next; 9 | ListNode(int i): val(i), next(NULL){} 10 | }; 11 | 12 | 13 | int findlastn(ListNode* head, int n){ 14 | ListNode* p=head; 15 | while(n>0 && p){ 16 | p=p->next; 17 | n--; 18 | } 19 | if (n>0) {return 0;} 20 | 21 | ListNode* q = head; 22 | 23 | while (p->next){ 24 | q=q->next; 25 | p=p->next; 26 | } 27 | return q->val; 28 | 29 | } 30 | 31 | int main(){ 32 | ListNode* head = new ListNode(1); 33 | ListNode* q=head; 34 | int a[15] = {1,2,3,4,5,6,7,4,5,2,10,8,9,0,5}; 35 | //int a[15] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; 36 | for (int i=0;i<15;i++){ 37 | ListNode* p=new ListNode(a[i]); 38 | q->next = p; 39 | q=p; 40 | } 41 | int n = 5; 42 | cout << "The " << n << "th to last element is " << findlastn(head,n-1) << endl; 43 | } 44 | -------------------------------------------------------------------------------- /2-2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-2.o -------------------------------------------------------------------------------- /2-3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-3 -------------------------------------------------------------------------------- /2-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ListNode{ 5 | public: 6 | int val; 7 | ListNode* next; 8 | ListNode(int i): val(i),next(NULL){} 9 | }; 10 | 11 | 12 | void delmid(ListNode* head){ 13 | ListNode* p = new ListNode(0); 14 | p->next = head; 15 | ListNode* q = head; 16 | 17 | if (!q->next){head=NULL; return;} 18 | 19 | while (q->next && q->next->next){ 20 | q = q->next->next; 21 | p = p->next; 22 | } 23 | 24 | p->next = p->next->next; 25 | 26 | } 27 | 28 | int main(){ 29 | ListNode* head = new ListNode(0); 30 | ListNode* q=head; 31 | int a[16] = {1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0}; 32 | 33 | for (int i=0;i<16;i++){ 34 | ListNode* p=new ListNode(a[i]); 35 | q->next = p; 36 | q=p; 37 | } 38 | 39 | delmid(head); 40 | 41 | 42 | if (head==NULL){cout << "Empty List" << endl; return 0;} 43 | while (head!=NULL){ 44 | cout << head->val << " "; 45 | head = head->next; 46 | } 47 | cout << endl; 48 | 49 | return 0; 50 | 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /2-3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-3.o -------------------------------------------------------------------------------- /2-4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-4 -------------------------------------------------------------------------------- /2-4.cpp: -------------------------------------------------------------------------------- 1 | // 2.5 in 5th edtion, 2.4 in 4th edtion 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | class ListNode{ 9 | public: 10 | int val; 11 | ListNode* next; 12 | ListNode(int i): val(i),next(NULL){} 13 | }; 14 | 15 | //add two list 16 | ListNode* add2l(ListNode* l1, ListNode* l2){ 17 | int carry=0; 18 | ListNode* p=l1; 19 | ListNode* q=l2; 20 | while(p && q){ 21 | int s = p->val+q->val + carry; 22 | p->val = s%10; 23 | q->val = s%10; 24 | carry = s/10; 25 | p=p->next; 26 | q=q->next; 27 | } 28 | 29 | if (p==NULL){ 30 | while (q->next && carry>0) { 31 | q->val = q->val+carry; 32 | carry = q->val/10; 33 | q->val=q->val%10; 34 | q=q->next; 35 | } 36 | if (carry>0){ 37 | q->val =q->val+carry; 38 | if (q->val>=10) { 39 | q->next = new ListNode(q->val/10); 40 | q->val = q->val%10; 41 | } 42 | } 43 | return l2; 44 | } 45 | 46 | if (q==NULL){ 47 | while (p->next && carry>0) { 48 | p->val = p->val+carry; 49 | carry = p->val/10; 50 | p->val=p->val%10; 51 | p=p->next; 52 | } 53 | if (carry>0){ 54 | p->val =p->val+carry; 55 | if (p->val>=10) { 56 | p->next = new ListNode(p->val/10); 57 | p->val = p->val%10; 58 | } 59 | } 60 | return l1; 61 | } 62 | 63 | 64 | } 65 | 66 | 67 | 68 | ListNode* creatl(int* A,int n){ 69 | ListNode* head = new ListNode(A[0]); 70 | ListNode* q=head; 71 | for (int i=1;inext = p; 74 | q=p; 75 | } 76 | return head; 77 | } 78 | 79 | void printList(ListNode* head){ 80 | if (head==NULL){cout << "Empty List" << endl; } 81 | while (head!=NULL){ 82 | cout << head->val << " "; 83 | head = head->next; 84 | } 85 | cout << endl; 86 | } 87 | 88 | int main(){ 89 | int A[4] = {4,3,6,9}; //9634 90 | int B[4] = {5,2,6}; //625 91 | ListNode* l1 = creatl(A,4); 92 | ListNode* l2 = creatl(B,3); 93 | ListNode* ls = add2l(l1,l2); 94 | printList(ls); 95 | return 0; 96 | } 97 | 98 | -------------------------------------------------------------------------------- /2-4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-4.o -------------------------------------------------------------------------------- /2-5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-5 -------------------------------------------------------------------------------- /2-5.cpp: -------------------------------------------------------------------------------- 1 | //2.4 in 5th edtion 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | class ListNode{ 8 | public: 9 | int val; 10 | ListNode* next; 11 | ListNode(int i): val(i),next(NULL){} 12 | }; 13 | 14 | 15 | ListNode* parti(ListNode *head, int x){ 16 | ListNode* p=new ListNode(0); 17 | p->next=head; 18 | head= p; 19 | while (p->next){ 20 | if (p->next->valnext; 22 | p->next = p->next->next; 23 | q->next = head->next; 24 | head->next = q; 25 | }else{ 26 | p=p->next; 27 | } 28 | } 29 | return head->next; 30 | } 31 | 32 | 33 | 34 | ListNode* creatl(int* A,int n){ 35 | ListNode* head = new ListNode(A[0]); 36 | ListNode* q=head; 37 | for (int i=1;inext = p; 40 | q=p; 41 | } 42 | return head; 43 | } 44 | 45 | void printList(ListNode* head){ 46 | if (head==NULL){cout << "Empty List" << endl; } 47 | while (head!=NULL){ 48 | cout << head->val << " "; 49 | head = head->next; 50 | } 51 | cout << endl; 52 | } 53 | 54 | int main(){ 55 | int A[12] = {9,8,7,4,5,3,2,8,4,3,6,0}; 56 | ListNode* head = creatl(A,12); 57 | int x = 5; 58 | ListNode* res = parti(head,x); 59 | printList(res); 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /2-5.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-5.o -------------------------------------------------------------------------------- /2-6: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-6 -------------------------------------------------------------------------------- /2-6.cpp: -------------------------------------------------------------------------------- 1 | //2.6 in 5th edtion 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | class ListNode{ 8 | public: 9 | int val; 10 | ListNode* next; 11 | ListNode(int i): val(i),next(NULL){} 12 | }; 13 | 14 | 15 | ListNode* ff(ListNode *node){ 16 | ListNode* p=node;//slow 17 | ListNode* q=node; //faster 18 | while (true){ 19 | p=p->next; 20 | q=q->next->next; 21 | if (p==q) break; 22 | } 23 | p=node; 24 | while (p!=q){ 25 | p=p->next; 26 | q=q->next; 27 | } 28 | 29 | return q; 30 | 31 | } 32 | 33 | 34 | 35 | ListNode* creatl(int* A,int n){ 36 | ListNode* head = new ListNode(A[0]); 37 | ListNode* q=head; 38 | for (int i=1;inext = p; 41 | q=p; 42 | } 43 | cout << "GT: last = " << q->val << endl; 44 | q->next = head->next->next->next; 45 | cout << "GT: loop = " << q->next->val << endl; 46 | return head; 47 | } 48 | 49 | void printList(ListNode* head){ 50 | if (head==NULL){cout << "Empty List" << endl; } 51 | while (head!=NULL){ 52 | cout << head->val << " "; 53 | head = head->next; 54 | } 55 | cout << endl; 56 | } 57 | 58 | int main(){ 59 | int A[11] = {9,8,7,4,5,3,2,8,4,3,6}; 60 | ListNode* head = creatl(A,11); // head = 9, last = 0, loop = 4; 61 | cout << "List head: " << head->val << endl; 62 | ListNode* res = ff(head); 63 | cout << "Loop start: " << res->val << endl; 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /2-6.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-6.o -------------------------------------------------------------------------------- /2-7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/2-7 -------------------------------------------------------------------------------- /2-7.cpp: -------------------------------------------------------------------------------- 1 | //2.4 in 5th edtion 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class ListNode{ 8 | public: 9 | int val; 10 | ListNode* next; 11 | ListNode(int i): val(i),next(NULL){} 12 | }; 13 | 14 | 15 | bool isp(ListNode* head,int len){ 16 | ListNode* p=head; 17 | stack st; 18 | int i = len/2; 19 | while (i>0){ 20 | st.push(p->val); 21 | p=p->next; 22 | i--; 23 | } 24 | 25 | if (len%2!=0){p=p->next;} 26 | 27 | while (p){ 28 | if (p->val!=st.top()){return false;} 29 | else{ 30 | st.pop(); 31 | p=p->next; 32 | } 33 | } 34 | 35 | return true; 36 | 37 | } 38 | 39 | 40 | 41 | ListNode* creatl(int* A,int n){ 42 | ListNode* head = new ListNode(A[0]); 43 | ListNode* q=head; 44 | for (int i=1;inext = p; 47 | q=p; 48 | } 49 | return head; 50 | } 51 | 52 | void printList(ListNode* head){ 53 | if (head==NULL){cout << "Empty List" << endl; } 54 | while (head!=NULL){ 55 | cout << head->val << " "; 56 | head = head->next; 57 | } 58 | cout << endl; 59 | } 60 | 61 | int main(){ 62 | int A[13] = {9,8,7,4,5,3,1,3,5,4,7,8,9}; 63 | ListNode* head = creatl(A,13); 64 | printList(head); 65 | int len = 13; 66 | if (isp(head,13)){cout << "This is a palindrome"< 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | // Composition of stack class 10 | template 11 | class StackWithMin{ 12 | stack > st; 13 | public: 14 | void push(T t){ 15 | if (!st.empty()){ 16 | T m; 17 | if (t>min()) {m = min();}else{m=t;} 18 | st.push(make_pair(t,m)); 19 | }else{ 20 | st.push(make_pair(t,t)); 21 | } 22 | } 23 | bool empty() { return st.empty(); } 24 | void pop(){st.pop();} 25 | T top(){return st.top().first;} 26 | T min(){return st.top().second;} 27 | }; 28 | 29 | 30 | 31 | //Inheritance of stack class 32 | // Note that: 33 | // (1) class StackWithMin2: public stack >{}; 34 | // (2) when using stack member functions, must contain the 35 | // template. e.g. stack >::push(make_pair(s,m)); 36 | 37 | template 38 | class StackWithMin2: public stack >{ 39 | public: 40 | void push(S s){ 41 | if (!empty()) { 42 | S m = std::min(s,min()); 43 | stack >::push(make_pair(s,m)); 44 | }else{ 45 | stack >::push(make_pair(s,s)); 46 | } 47 | } 48 | S min(){ 49 | return stack >::top().second; 50 | } 51 | 52 | S top(){return stack >::top().first;} 53 | 54 | void pop(){ stack >::pop();} 55 | 56 | bool empty() { return stack >::empty();} 57 | }; 58 | 59 | int main(){ 60 | //StackWithMin st; 61 | StackWithMin2 st; 62 | 63 | st.push(20); 64 | st.push(10); 65 | st.push(25); 66 | st.push(8); 67 | st.push(9); 68 | st.push(7); 69 | st.push(20); 70 | 71 | cout << "20" << endl; 72 | cout << "7 " << endl; 73 | cout << "9 " << endl; 74 | cout << "8 " << endl; 75 | cout << "25" << endl; 76 | cout << "10" << endl; 77 | cout << "20" << endl; 78 | 79 | 80 | while (!st.empty()){ 81 | cout << "Current min = " << st.min() << " "; 82 | cout << st.top() << " Poped. " << endl; 83 | st.pop(); 84 | } 85 | 86 | return 0; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /3-2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/3-2.o -------------------------------------------------------------------------------- /3-3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/3-3 -------------------------------------------------------------------------------- /3-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | class SetOfStack{ 9 | vector > stacks; 10 | int capacity; 11 | public: 12 | SetOfStack(int cap):capacity(cap){} 13 | 14 | void push(int i){ 15 | if (getLastStack()>=0 && stacks[getLastStack()].size() nst; 20 | nst.push(i); 21 | stacks.push_back(nst); 22 | cout << i << "pushed into new stack " << getLastStack() << endl; 23 | } 24 | } 25 | 26 | void pop(){ 27 | if (stacks[getLastStack()].size()==0){cerr<< "Empty stack!" << endl; } 28 | else{ 29 | if (stacks[getLastStack()].size()==1){ 30 | stacks.pop_back(); 31 | }else{ 32 | stacks[getLastStack()].pop(); 33 | } 34 | } 35 | } 36 | 37 | void popAt(int index){ 38 | if (index>getLastStack() || index<0){cerr<<"Index out of range!" << endl;} 39 | else{ 40 | if (stacks[index].size()==1){ 41 | stacks.erase(stacks.begin()+index); 42 | }else{ 43 | stacks[index].pop(); 44 | } 45 | 46 | } 47 | } 48 | 49 | 50 | int top() { 51 | if ( !isempty() && (!stacks[getLastStack()].empty())){ 52 | return stacks[getLastStack()].top(); 53 | }else{cerr<< "Empty Stack"<getLastStack() || index<0){cerr<<"Index out of range!" << endl;} 58 | else{ 59 | return stacks[index].top(); 60 | } 61 | } 62 | 63 | bool isempty(){ 64 | if (stacks.size()==0){return true;} 65 | else{ 66 | return false; 67 | } 68 | } 69 | 70 | int size(){ 71 | return stacks.size(); 72 | } 73 | 74 | int getLastStack(){ 75 | return stacks.size()-1; 76 | } 77 | 78 | }; 79 | 80 | 81 | 82 | int main(){ 83 | SetOfStack sst(4); 84 | for (int i=1;i<16;i++){ 85 | sst.push(i); 86 | } 87 | 88 | for (int i=0;i 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | void moveTopTo(vector >&towers, int origin, int dest){ 9 | int tmp = towers[origin].top(); 10 | towers[origin].pop(); 11 | towers[dest].push(tmp); 12 | cout << "move disk" << tmp << " from " << origin << " to " << dest << endl; 13 | } 14 | 15 | void moveplate(int n,vector >&towers, int origin, int buffer, int destination){ 16 | if (n<=0){return;} 17 | moveplate(n-1,towers,origin,buffer,destination); 18 | moveTopTo(towers, origin,destination); 19 | moveplate(n-1,towers,buffer,destination,origin); 20 | } 21 | 22 | int main(){ 23 | int np = 5; 24 | vector >towers; 25 | stack st; 26 | towers.push_back(st); 27 | towers.push_back(st); 28 | towers.push_back(st); 29 | for (int i=np;i>=1;i--){ 30 | towers[0].push(i); 31 | } 32 | moveplate(np,towers,0,1,2); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /3-4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/3-4.o -------------------------------------------------------------------------------- /3-5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/3-5 -------------------------------------------------------------------------------- /3-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | template 6 | class MyQueue{ 7 | stack s_new; 8 | stack s_old; 9 | public: 10 | void push(T i){ 11 | s_new.push(i); 12 | } 13 | void pop(){ 14 | if (!s_old.empty()){ 15 | s_old.pop(); 16 | }else{ 17 | if (s_new.empty()){ 18 | cerr<<"Empty Queue!" << endl; 19 | }else{ 20 | while (!s_new.empty()){ 21 | s_old.push(s_new.top()); 22 | s_new.pop(); 23 | } 24 | s_old.pop(); 25 | } 26 | } 27 | } 28 | 29 | T front(){ 30 | if (!s_old.empty()){ 31 | return s_old.top(); 32 | }else{ 33 | if (s_new.empty()){ 34 | cerr<<"Empty Queue!" << endl; 35 | }else{ 36 | while (!s_new.empty()){ 37 | s_old.push(s_new.top()); 38 | s_new.pop(); 39 | } 40 | return s_old.top(); 41 | } 42 | } 43 | } 44 | 45 | bool empty(){ 46 | return s_new.empty()&&s_old.empty(); 47 | } 48 | }; 49 | 50 | 51 | int main(){ 52 | MyQueue mq; 53 | 54 | for (int i=0;i<10;i++){ 55 | mq.push(i); 56 | } 57 | 58 | while (!mq.empty()) { 59 | cout<< mq.front()< 2 | #include 3 | 4 | using namespace std; 5 | 6 | void sort(stack &st1, stack &st2){ 7 | while (!st1.empty()){ 8 | int tmp = st1.top(); 9 | st1.pop(); 10 | while (!st2.empty() && st2.top()>tmp){ 11 | st1.push(st2.top()); 12 | st2.pop(); 13 | } 14 | st2.push(tmp); 15 | } 16 | } 17 | 18 | 19 | int main(){ 20 | stack st1; 21 | stack st2; 22 | int A[9] = {2,4,5,3,1,7,9,8,6}; 23 | for (int i=0;i<9;i++){ 24 | st1.push(A[i]); 25 | } 26 | sort(st1,st2); 27 | while (!st2.empty()){ 28 | cout << st2.top() << " "; 29 | st2.pop(); 30 | } 31 | cout << endl; 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /3-6.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/3-6.o -------------------------------------------------------------------------------- /3-7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/3-7 -------------------------------------------------------------------------------- /3-7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | class Animal{ 9 | int order; 10 | string name; 11 | string type; 12 | public: 13 | Animal(string str): name(str) {} 14 | virtual void setOrder(int o){order=o;} 15 | virtual void setType(string typ){type=typ;} 16 | virtual string getType(){return type;} 17 | virtual int getOrder(){return order;} 18 | virtual string getName(){return name;} 19 | bool operator> (const Animal& ani) const { 20 | return order>ani.order; 21 | } 22 | bool operator< (const Animal& ani) const { 23 | return order cats; 42 | list dogs; 43 | int order; 44 | public: 45 | Shelter():order(0){} 46 | void enqueue(Animal ani){ 47 | order++; 48 | ani.setOrder(order); 49 | if (ani.getType()=="Cat"){ 50 | cats.push_back(*(Cat*) &ani); 51 | } 52 | if (ani.getType()=="Dog"){ 53 | dogs.push_back(*(Dog*) &ani); 54 | } 55 | cout << "Enqueue: " << ani.getName() << " on time: " << ani.getOrder() << endl; 56 | } 57 | 58 | Animal dequeueAny(){ 59 | if (cats.empty() && dogs.empty()){cerr<<"Empty Shelter!!" << endl;} 60 | else{ 61 | if (!cats.empty() && dogs.empty()){ 62 | cout << "No dogs in shelter" << endl; 63 | return dequeueCat(); 64 | } 65 | if (cats.empty() && !dogs.empty()){ 66 | cout << "No cats in shelter" << endl; 67 | return dequeueDog(); 68 | } 69 | if (!cats.empty() && !dogs.empty()){ 70 | //if (cats.front().getOrder()>dogs.front().getOrder()){ 71 | if (cats.front()>dogs.front()){ 72 | return dequeueDog(); 73 | }else{ 74 | return dequeueCat(); 75 | } 76 | } 77 | } 78 | }; 79 | 80 | Dog dequeueDog(){ 81 | if (!dogs.empty()){ 82 | Dog ani = dogs.front(); 83 | dogs.pop_front(); 84 | return ani; 85 | }else{ 86 | cerr<< "Empty dogs" << endl; 87 | } 88 | }; 89 | 90 | Cat dequeueCat(){ 91 | if (!cats.empty()){ 92 | Cat ani = cats.front(); 93 | cats.pop_front(); 94 | return ani; 95 | }else{ 96 | cerr<< "Empty cats" << endl; 97 | } 98 | } 99 | }; 100 | 101 | 102 | int main(){ 103 | Shelter sh; 104 | Cat c1("Cat1"); 105 | Cat c2("Cat2"); 106 | Cat c3("Cat3"); 107 | Dog d1("Dog1"); 108 | Dog d2("Dog2"); 109 | Dog d3("Dog3"); 110 | 111 | sh.enqueue(c1); 112 | sh.enqueue(d1); 113 | sh.enqueue(d2); 114 | sh.enqueue(c2); 115 | sh.enqueue(c3); 116 | sh.enqueue(d3); 117 | 118 | 119 | Animal ani = sh.dequeueAny(); 120 | cout << "DeuqeAny: " << ani.getName() << endl; 121 | Cat cc = sh.dequeueCat(); 122 | cout << "DeuqeCat: " << cc.getName() << endl; 123 | Dog dd = sh.dequeueDog(); 124 | cout << "DeuqeDog: " << dd.getName() << endl; 125 | 126 | } 127 | -------------------------------------------------------------------------------- /3-7.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/3-7.o -------------------------------------------------------------------------------- /4-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-1 -------------------------------------------------------------------------------- /4-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | 8 | class TreeNode{ 9 | public: 10 | TreeNode* left; 11 | TreeNode* right; 12 | int val; 13 | TreeNode(int i):val(i),left(NULL),right(NULL){} 14 | }; 15 | 16 | int getH(TreeNode* root){ 17 | if (root==NULL){ 18 | return 0; 19 | }else{ 20 | int l = getH(root->left); 21 | if (l==-1){return -1;} 22 | int r = getH(root->right); 23 | if (r==-1){return -1;} 24 | 25 | if (abs(r-l)>1){ 26 | return -1; 27 | }else{ 28 | return max(l,r)+1; 29 | } 30 | 31 | } 32 | } 33 | 34 | bool isBalanced(TreeNode* root){ 35 | if (getH(root)==-1){ 36 | return false; 37 | }else{ 38 | return true; 39 | } 40 | } 41 | 42 | 43 | int main(){ 44 | TreeNode* root = new TreeNode(0); 45 | root->left = new TreeNode(1); 46 | root->right = new TreeNode(2); 47 | 48 | root->left->left = new TreeNode(3); 49 | root->left->right = new TreeNode(4); 50 | 51 | root->right->left = new TreeNode(5); 52 | root->right->right = new TreeNode(6); 53 | 54 | root->right->right->right = new TreeNode(7); 55 | root->right->right->right->right = new TreeNode(8); 56 | 57 | 58 | if (isBalanced(root)){ 59 | cout << "Balanced!"<< endl; 60 | }else{ 61 | cout << "unBalanced!" << endl; 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /4-1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-1.o -------------------------------------------------------------------------------- /4-3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-3 -------------------------------------------------------------------------------- /4-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class TreeNode{ 6 | public: 7 | TreeNode* left; 8 | TreeNode* right; 9 | int val; 10 | TreeNode(int x):val(x),left(NULL),right(NULL){} 11 | }; 12 | 13 | 14 | TreeNode* BST(int* A,int st, int ed){ 15 | if (st>ed){return NULL;} 16 | else{ 17 | int mid = st+(ed-st)/2; 18 | TreeNode* node = new TreeNode(A[mid]); 19 | //cout << A[mid] << " "; 20 | node->left = BST(A,st,mid-1); 21 | node->right = BST(A,mid+1,ed); 22 | return node; 23 | } 24 | } 25 | 26 | //print a tree according to the level 27 | void printTree(TreeNode* root){ 28 | queue q; 29 | if (root==NULL){cout << "Empty Tree! "<< endl; return;} 30 | q.push(root); 31 | while (!q.empty()){ 32 | cout << q.front()->val << " "; 33 | if (q.front()->left!=NULL){q.push(q.front()->left);} 34 | if (q.front()->right!=NULL){q.push(q.front()->right);} 35 | q.pop(); 36 | } 37 | cout << endl; 38 | } 39 | 40 | int main(){ 41 | int n=13; 42 | int* A = new int[n]; 43 | for (int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class TreeNode{ 7 | public: 8 | TreeNode* left; 9 | TreeNode* right; 10 | int val; 11 | TreeNode(int x):val(x),left(NULL),right(NULL){} 12 | }; 13 | 14 | 15 | TreeNode* BST(int* A,int st, int ed){ 16 | if (st>ed){return NULL;} 17 | else{ 18 | int mid = st+(ed-st)/2; 19 | TreeNode* node = new TreeNode(A[mid]); 20 | //cout << A[mid] << " "; 21 | node->left = BST(A,st,mid-1); 22 | node->right = BST(A,mid+1,ed); 23 | return node; 24 | } 25 | } 26 | 27 | //store a tree according to the level 28 | void storeTree(TreeNode* root, vector > &res){ 29 | queue > q; 30 | if (root==NULL){cout << "Empty Tree! "<< endl; return;} 31 | int dep=0; 32 | q.push(make_pair(root,dep)); 33 | vector lev; 34 | while (!q.empty()){ 35 | if (q.front().second!=dep){ 36 | res.push_back(lev); 37 | lev.clear(); 38 | } 39 | dep = q.front().second; 40 | lev.push_back(q.front().first->val); 41 | 42 | if (q.front().first->left!=NULL){q.push( make_pair(q.front().first->left,dep+1));} 43 | if (q.front().first->right!=NULL){q.push( make_pair(q.front().first->right,dep+1));} 44 | 45 | q.pop(); 46 | } 47 | res.push_back(lev); 48 | } 49 | 50 | int main(){ 51 | int n=13; 52 | int* A = new int[n]; 53 | for (int i=0;i > res; 61 | storeTree(root,res); 62 | 63 | for (int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class TreeNode{ 7 | public: 8 | TreeNode* left; 9 | TreeNode* right; 10 | int val; 11 | TreeNode(int x):val(x),left(NULL),right(NULL){} 12 | }; 13 | 14 | static int last = INT_MIN; 15 | 16 | class Solution{ 17 | public: 18 | void inOrder(TreeNode* root, vector &l){ 19 | if (root==NULL){ 20 | return; 21 | }else{ 22 | inOrder(root->left,l); 23 | cout << root->val << " "; 24 | l.push_back(root->val); 25 | inOrder(root->right,l); 26 | } 27 | } 28 | bool checkbst(TreeNode* root){ 29 | vector l; 30 | inOrder(root,l); 31 | for (int i=1;ileft)){return false;} 45 | if (root->valval; 47 | if (!checkbst2(root->right)){return false;} 48 | return true; 49 | } 50 | } 51 | 52 | }; 53 | 54 | int main(){ 55 | //construct tree 56 | TreeNode* root = new TreeNode(3); 57 | root->left = new TreeNode(1); 58 | root->right = new TreeNode(5); 59 | 60 | root->left->left = new TreeNode(0); 61 | root->left->right = new TreeNode(2); 62 | 63 | root->right->left = new TreeNode(4); 64 | root->right->right = new TreeNode(6); 65 | 66 | root->right->right->right = new TreeNode(7); 67 | root->right->right->right->right = new TreeNode(8); 68 | Solution sol; 69 | if (sol.checkbst(root)){ 70 | cout << "This is a BST" << endl; 71 | }else{ 72 | cout << "This is NOT a BST" << endl; 73 | } 74 | 75 | if (sol.checkbst2(root)){ 76 | cout << "This is a BST" << endl; 77 | }else{ 78 | cout << "This is NOT a BST" << endl; 79 | } 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /4-5.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-5.o -------------------------------------------------------------------------------- /4-7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-7 -------------------------------------------------------------------------------- /4-7.cpp: -------------------------------------------------------------------------------- 1 | //Common Ancestor 2 | #include 3 | using namespace std; 4 | 5 | class TreeNode{ 6 | public: 7 | TreeNode* left; 8 | TreeNode* right; 9 | int val; 10 | TreeNode(int x):val(x),left(NULL),right(NULL){} 11 | }; 12 | 13 | class Solution{ 14 | public: 15 | bool inLeft(TreeNode* root, TreeNode* node){ 16 | if (root==NULL) {return false;} 17 | if (root==node) {return true;} 18 | return inLeft(root->left,node) || inLeft(root->right,node); 19 | } 20 | 21 | TreeNode* commonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){ 22 | if (root==NULL){return NULL;} 23 | if (root==p || root==q){return root;} 24 | 25 | bool is_left_p=false; 26 | bool is_left_q=false; 27 | if (root->left){ 28 | is_left_p= inLeft(root->left,p); 29 | is_left_q= inLeft(root->left,q); 30 | } 31 | 32 | if (is_left_p!=is_left_q){return root;} 33 | if (is_left_p){return commonAncestor(root->left,p,q);} 34 | else{return commonAncestor(root->right,p,q);} 35 | } 36 | }; 37 | 38 | 39 | int main(){ 40 | //construct tree 41 | TreeNode* root = new TreeNode(3); 42 | root->left = new TreeNode(1); 43 | root->right = new TreeNode(5); 44 | 45 | root->left->left = new TreeNode(0); 46 | root->left->right = new TreeNode(2); 47 | 48 | root->right->left = new TreeNode(4); 49 | root->right->right = new TreeNode(6); 50 | 51 | root->right->right->right = new TreeNode(7); 52 | root->right->right->right->right = new TreeNode(8); 53 | 54 | TreeNode* p = root->right->right->right; 55 | //TreeNode* q = root->right->right->right->right; 56 | TreeNode* q = root->left->left; 57 | 58 | 59 | Solution sol; 60 | TreeNode* ans = sol.commonAncestor(root,p,q); 61 | cout << ans->val << endl; 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /4-7.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-7.o -------------------------------------------------------------------------------- /4-8: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-8 -------------------------------------------------------------------------------- /4-8.cpp: -------------------------------------------------------------------------------- 1 | //subtree 2 | #include 3 | 4 | using namespace std; 5 | class TreeNode{ 6 | public: 7 | TreeNode* left; 8 | TreeNode* right; 9 | int val; 10 | TreeNode(int x):val(x),left(NULL),right(NULL){} 11 | }; 12 | 13 | class Solution{ 14 | public: 15 | bool isSameTree(TreeNode *root1, TreeNode* root2){ 16 | if (root1==NULL && root2==NULL){return true;} 17 | if (root1==NULL || root2==NULL){return false;} 18 | if (root1->val!=root2->val){return false;} 19 | return isSameTree(root1->left,root2->left)&&isSameTree(root1->right,root2->right); 20 | } 21 | 22 | bool isSub(TreeNode* bigTree, TreeNode* smTree){ 23 | if (bigTree==NULL){return false;} 24 | if (bigTree->val==smTree->val && isSameTree(bigTree,smTree)) {return true;} 25 | return isSub(bigTree->left,smTree) || isSub(bigTree->right,smTree); 26 | } 27 | }; 28 | 29 | int main(){ 30 | //construct tree 31 | TreeNode* root = new TreeNode(3); 32 | root->left = new TreeNode(1); 33 | root->right = new TreeNode(5); 34 | 35 | root->left->left = new TreeNode(0); 36 | root->left->right = new TreeNode(2); 37 | 38 | root->right->left = new TreeNode(4); 39 | root->right->right = new TreeNode(6); 40 | 41 | root->right->right->right = new TreeNode(7); 42 | root->right->right->right->right = new TreeNode(8); 43 | 44 | TreeNode* root2 = root->right; // yes 45 | TreeNode* root3 = new TreeNode(4); // yes 46 | TreeNode* root4 = new TreeNode(5); // no 47 | root4->left = new TreeNode(4); 48 | root4->right = new TreeNode(6); 49 | 50 | TreeNode* root5 = new TreeNode(1); // yes 51 | root5->left=new TreeNode(0); 52 | root5->right=new TreeNode(2); 53 | 54 | 55 | Solution sol; 56 | if (sol.isSub(root,root2)){ 57 | cout << "Tree2 is a subtree of Tree1" << endl; 58 | }else{ 59 | cout << "Tree2 is NOT a subtree of Tree1" << endl; 60 | } 61 | 62 | if (sol.isSub(root,root3)){ 63 | cout << "Tree3 is a subtree of Tree1" << endl; 64 | }else{ 65 | cout << "Tree3 is NOT a subtree of Tree1" << endl; 66 | } 67 | if (sol.isSub(root,root4)){ 68 | cout << "Tree4 is a subtree of Tree1" << endl; 69 | }else{ 70 | cout << "Tree4 is NOT a subtree of Tree1" << endl; 71 | } 72 | if (sol.isSub(root,root5)){ 73 | cout << "Tree5 is a subtree of Tree1" << endl; 74 | }else{ 75 | cout << "Tree5 is NOT a subtree of Tree1" << endl; 76 | } 77 | return 0; 78 | 79 | } 80 | -------------------------------------------------------------------------------- /4-8.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/4-8.o -------------------------------------------------------------------------------- /5-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/5-1 -------------------------------------------------------------------------------- /5-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | void showbin(int v){ 7 | string str(32,'0'); 8 | int i=31; 9 | while (v>0){ 10 | if (v%2==1){str[i]='1';} 11 | v=v/2; 12 | i--; 13 | } 14 | cout << str << endl; 15 | } 16 | 17 | 18 | int main(){ 19 | 20 | int m=255; 21 | int n=14; 22 | int i=3; 23 | int j=6; 24 | 25 | 26 | int aa=~0; 27 | aa = aa << j; 28 | int bb=(1<a(aa); 31 | //cout << a.to_string() << endl; 32 | bitset<32>b(bb); 33 | //cout << b.to_string() << endl; 34 | 35 | int cc = aa|bb; 36 | bitset<32>c(cc); 37 | cout << c.to_string() << endl; 38 | 39 | m = m&cc; 40 | n=n<mm(m); 45 | bitset<32>nn(n); 46 | cout << mm.to_string() << endl; 47 | //cout << nn.to_string() << endl; 48 | int t=0; 49 | for (int k = i;k<=j;k++){ 50 | mm[k]=nn[t]; 51 | t++; 52 | } 53 | 54 | //cout << mm.to_string() << endl; 55 | 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /5-1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/5-1.o -------------------------------------------------------------------------------- /5-2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/5-2 -------------------------------------------------------------------------------- /5-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | string printBin(double x){ 7 | if (x>1 || x<0){cerr<< "Wrong input!"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | int ct(int A, int B){ 8 | bitset<8> C(A^B); 9 | return C.count(); 10 | } 11 | 12 | int main(){ 13 | int A = 7; 14 | int B = 14; 15 | bitset<8> AA(A); 16 | bitset<8> BB(B); 17 | cout << "To convert " << AA.to_string() <<" to " << BB.to_string() << ", " << ct(A,B) << " bits are needed." << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /5-5.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/5-5.o -------------------------------------------------------------------------------- /5-6: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/5-6 -------------------------------------------------------------------------------- /5-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int swap(int x){ 7 | int maskE = 170; //8 bits, 10101010 8 | int maskO = 85; //8 bit, 01010101 9 | return ((x&maskE) >> 1)|((x&maskO) << 1); 10 | } 11 | 12 | int main(){ 13 | int x = 145; 14 | bitset<8> xx(x); 15 | cout << "Swap " << xx.to_string() << endl; 16 | bitset<8> res(swap(x)); 17 | cout << "E to O " << res.to_string() << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /5-6.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/5-6.o -------------------------------------------------------------------------------- /6-1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/6-1.txt -------------------------------------------------------------------------------- /9-10: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-10 -------------------------------------------------------------------------------- /9-10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | class Box{ 8 | public: 9 | int h; 10 | int w; 11 | int d; 12 | Box(int h1,int w1,int d1):h(h1),w(w1),d(d1){}; 13 | bool canBeAbove(Box b){ 14 | if (b.empty()) {return true;} 15 | if ((h st){ 29 | int maxh=0; 30 | if (st.empty()){return 0;} 31 | for (vector::iterator it=st.begin();it!=st.end();it++){ 32 | maxh+=it->h; 33 | } 34 | return maxh; 35 | } 36 | 37 | vector creatStack(vector boxes, Box bottom){ 38 | int max_height = 0; 39 | vector max_stack; 40 | 41 | for (int i=0;i new_stack = creatStack(boxes,boxes[i]); 44 | int new_height = getHeight(new_stack); 45 | if (new_height>max_height){ 46 | max_stack = new_stack; 47 | max_height = new_height; 48 | } 49 | } 50 | } 51 | 52 | if (!bottom.empty()){ 53 | max_stack.push_back(bottom); 54 | } 55 | 56 | 57 | return max_stack; 58 | } 59 | 60 | 61 | int main(){ 62 | 63 | //input data 64 | Box b1(1,2,3); 65 | Box b2(2,3,4); 66 | Box b3(5,2,6); 67 | Box b4(7,8,9); 68 | Box b5(12,11,10); 69 | vector boxes; 70 | boxes.push_back(b1); 71 | boxes.push_back(b2); 72 | boxes.push_back(b3); 73 | boxes.push_back(b4); 74 | boxes.push_back(b5); 75 | 76 | vector stack; 77 | Box bottom(0,0,0); 78 | stack = creatStack(boxes, bottom); 79 | cout << "Max height is:" << getHeight(stack) << endl; 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /9-10.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-10.o -------------------------------------------------------------------------------- /9-10v2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-10v2 -------------------------------------------------------------------------------- /9-10v2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Box{ 7 | public: 8 | int h; 9 | int w; 10 | int d; 11 | Box(int h1,int w1,int d1):h(h1),w(w1),d(d1){}; 12 | bool empty(){ 13 | if (h==0 && w==0 && d==0){return true;} 14 | return false; 15 | } 16 | 17 | bool CanBeAbove(Box b){ 18 | if (b.empty()) {return true;} 19 | if (h &st, vector &boxes, int hh, int &maxh){ 26 | Box top(0,0,0); 27 | if (!st.empty()){ 28 | top=st.back(); 29 | } 30 | 31 | for (int i=0;imaxh){maxh=hh;} 41 | 42 | } 43 | 44 | 45 | int main(){ 46 | //input data 47 | Box b1(1,2,3); 48 | Box b2(2,3,4); 49 | Box b3(5,2,6); 50 | Box b4(7,8,9); 51 | Box b5(12,11,10); 52 | vector boxes; 53 | boxes.push_back(b1); 54 | boxes.push_back(b2); 55 | boxes.push_back(b3); 56 | boxes.push_back(b4); 57 | boxes.push_back(b5); 58 | 59 | vector st; 60 | int maxh=0; 61 | 62 | build(st, boxes, 0, maxh) ; 63 | 64 | cout << "Max height is:" << maxh << endl; 65 | return 0; 66 | } 67 | 68 | -------------------------------------------------------------------------------- /9-10v2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-10v2.o -------------------------------------------------------------------------------- /9-2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-2 -------------------------------------------------------------------------------- /9-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int getNum(int x, int y){ 7 | int m=x+y; 8 | int n=x; 9 | double res = 1; 10 | while (m!=x){ 11 | res =res*m; 12 | m--; 13 | } 14 | while (n!=0){ 15 | res = res/n; 16 | n--; 17 | } 18 | return int(res); 19 | } 20 | 21 | 22 | void getPath(int x1,int y1,int x, int y,vector &path,int &num){ 23 | if (x1==x && y1==y){ 24 | num++; 25 | cout << num << ":"; 26 | for (int i=0;i path; 57 | int num=0; 58 | getPath(0,0,x,y,path,num); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /9-2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-2.o -------------------------------------------------------------------------------- /9-3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-3 -------------------------------------------------------------------------------- /9-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | int findMagic(int A[],int st, int ed){ 8 | if (st>ed){ 9 | return -1; 10 | } 11 | int mid = st+ (ed-st)/2; 12 | if(A[mid]mid){ 17 | return findMagic(A,st,mid-1); 18 | } 19 | } 20 | 21 | int main(){ 22 | int A[10]={-10,-5,-1,1,2,3,5,7,9,12}; 23 | // int A[10]={0,1,2,3,4,5,6,7,8,9,10}; 24 | int n = 10; 25 | int idx = findMagic(A,0,n-1); 26 | if (idx==-1){ 27 | cout << "NOT found!" << endl; 28 | }else{ 29 | cout << idx << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /9-3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-3.o -------------------------------------------------------------------------------- /9-4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-4 -------------------------------------------------------------------------------- /9-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | void subSet(string str, vector &res){ 9 | int sz = str.length(); 10 | int rsz=0; 11 | sort(str.begin(),str.end()); 12 | for (int i=0;i res; 40 | subSet(str,res); 41 | for (int i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void swap(string &str, int a, int b){ 8 | char ch = str[a]; 9 | str[a]=str[b]; 10 | str[b]=ch; 11 | } 12 | 13 | void perm(string str,int k,int n, vector &res){ 14 | if (k==n){ 15 | res.push_back(str); 16 | }else{ 17 | for (int i=k;i res; 30 | perm(str,0,str.size(),res); 31 | for (int i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | void dps(string str, int l, int r, vector &res){ 9 | if (l==0 && r==0){ 10 | res.push_back(str); 11 | }else{ 12 | if (l>0){ 13 | dps(str+"(",l-1,r,res); 14 | } 15 | 16 | if (r>0 && r>l){ 17 | dps(str+")",l,r-1,res); 18 | } 19 | } 20 | 21 | } 22 | 23 | int main(){ 24 | int n=3; 25 | vector res; 26 | dps("",n,n,res); 27 | for (int i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | int mc2(int n, int d){ 7 | int next_d = 0; 8 | switch (d){ 9 | case 25: 10 | next_d=10; 11 | break; 12 | case 10: 13 | next_d=5; 14 | break; 15 | case 5: 16 | next_d=1; 17 | break; 18 | case 1: 19 | return 1; 20 | } 21 | 22 | int ways=0; 23 | for (int i=0;i*d<=n;i++) { 24 | ways+=mc2(n-i*d,next_d); 25 | } 26 | return ways; 27 | } 28 | 29 | int main(){ 30 | int n=100; 31 | cout << mc2(n,25) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /9-8.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-8.o -------------------------------------------------------------------------------- /9-9: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xperzy/careerup150/cdd6353261281935bbb6d128b3bd5be65baea2ff/9-9 -------------------------------------------------------------------------------- /9-9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | bool OK(int x, int y, const vector mp){ 8 | for (int i=0;i &mp, vector > &res, int nqueen){ 18 | if (n==nqueen){ 19 | res.push_back(mp); 20 | }else{ 21 | for (int i=0;i<8;i++){ 22 | if (OK(n,i,mp) ){ 23 | mp[n]=i; 24 | dp(n+1,mp,res,nqueen); 25 | mp[n]=0; 26 | } 27 | } 28 | } 29 | } 30 | 31 | 32 | int main(){ 33 | int nqueen = 8; 34 | vector > res; 35 | vector mp(8,0); 36 | dp(0,mp,res,nqueen); 37 | 38 | for (int i=0;i