From ec3bac349a134dd5beb1feb821bc3d1a64260a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Sun, 2 Sep 2018 12:09:04 +0800 Subject: [PATCH 01/19] add --- .../question.md" | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/question.md" "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/question.md" index 5222914..d8edd28 100644 --- "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/question.md" +++ "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/question.md" @@ -91,6 +91,45 @@ * 快速排序 + ``` + int partition(vector & arr, int begin, int end) + { + int partition_val = arr[end]; + int sorted_index = begin; + for (int i = begin; i < end; i++) + { + if (arr[i] < partition_val) + { + swap(arr[sorted_index], arr[i]); + sorted_index++; + } + } + swap(arr[sorted_index], arr[end]); + return sorted_index; + } + void quick_sort(vector &arr,int begin,int end) + { + int left = begin; + int right = end; + if (left < right) + { + int index = partition(arr, begin, end); + quick_sort(arr, begin, index - 1); + quick_sort(arr, index + 1, end); + } + } + + + int main() + { + + vector arr = { 5,8,1,3,10,9,4,0 }; + quick_sort(arr, 0, 7); + + return 0; + } + ``` + * 反转链表 -* 将一个链表拆分成两个(奇数位组成一个链表;偶数位组成一个链表) \ No newline at end of file +* 将一个链表拆分成两个(奇数位组成一个链表;偶数位组成一个链表) From ac97ca65b17e7cb5fc97b5654812d6ec4c274754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Sun, 2 Sep 2018 12:09:26 +0800 Subject: [PATCH 02/19] add --- .../readme.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/question.md" => "---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" (100%) diff --git "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/question.md" "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" similarity index 100% rename from "---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/question.md" rename to "---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" From 8f721a5d2632f34ea1f0f672c5df6db201a37672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Tue, 4 Sep 2018 14:49:37 +0800 Subject: [PATCH 03/19] add --- .../readme.md" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" index d8edd28..e39770a 100644 --- "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" +++ "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" @@ -133,3 +133,98 @@ * 反转链表 * 将一个链表拆分成两个(奇数位组成一个链表;偶数位组成一个链表) + +* 归并两个有序链表 + ``` + #include + #include + + using namespace std; + + struct ListNode + { + int val; + struct ListNode *next; + ListNode(int x) :val(x), next(NULL) {} + }; + + //根据vector去创建一个链表 + ListNode * create_list_by_vector(vector vi) + { + ListNode * head = new ListNode(vi[0]); + ListNode * p = head; + for (int i = 1; i < vi.size(); i++) + { + ListNode * node = new ListNode(vi[i]); + p->next = node; + p = p->next; + } + return head; + } + + ListNode* insert(ListNode * tail, ListNode * node) + { + node->next = NULL; + tail->next = node; + return node; + } + + ListNode * merge(ListNode * head1,ListNode * head2) + { + ListNode * newhead = new ListNode(-1); + ListNode * tail = newhead; + while(head1!=NULL && head2!=NULL) + { + if(head1->val < head2->val) + { + ListNode * p = head1->next; + //插入head1 + tail = insert(tail,head1); + head1 = p; + } + else + { + ListNode * p = head2->next; + //插入head2 + tail = insert(tail,head2); + head2 = p; + } + } + + if(head1!=NULL) + { + tail->next = head1; + } + else if(head2 != NULL) + { + tail->next = head2; + } + return newhead->next; + } + + void print_list(ListNode * head) + { + while(head!=NULL) + { + cout<val<next; + } + } + + int main() + { + int arr[] = {1,3,7,9,16 }; + vector vi1(arr,arr+5); + ListNode * head1 = create_list_by_vector(vi1); + + int arr2[]= {2,4,8,10,14,20}; + vector vi2(arr2,arr2+6); + ListNode * head2 = create_list_by_vector(vi2); + + ListNode * newhead = merge(head1,head2); + + print_list(newhead); + + return 0; + } + ``` From e5a64af4cf8a2f8166c0e04626f653c897515e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Thu, 6 Sep 2018 10:59:44 +0800 Subject: [PATCH 04/19] add top k --- .../readme.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" index e39770a..97a2c02 100644 --- "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" +++ "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" @@ -228,3 +228,53 @@ return 0; } ``` + +* 第K大的数组 + ``` + #include + #include + using namespace std; + + int partition(vector & vi ,int begin ,int end) + { + int partition_val = vi[end]; + if(begin &vi,int begin,int end,int k) + { + //这个边界条件好像有问题 + if(begink) + return kth(vi,begin,index-1,k); + else + return vi[k]; + } + } + + int main() + { + int arr[]={4,1,2,10,9,7,0,13}; + vector vi(arr,arr+8); + + cout< Date: Sat, 8 Sep 2018 21:54:26 +0800 Subject: [PATCH 05/19] add --- ...06\345\261\202\346\211\223\345\215\260.md" | 92 ++++++++++++ ...02\345\272\217\351\201\215\345\216\206.md" | 92 ++++++++++++ .../readme.md" | 2 +- .../readme.md" | 136 ++++++++++++++++++ 4 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 "---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\210\206\345\261\202\346\211\223\345\215\260.md" create mode 100644 "---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" create mode 100644 "---\344\272\214\345\217\211\346\240\221---/\345\210\244\346\226\255\344\270\200\346\243\265\346\240\221\346\230\257\344\270\215\346\230\257\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/readme.md" diff --git "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\210\206\345\261\202\346\211\223\345\215\260.md" "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\210\206\345\261\202\346\211\223\345\215\260.md" new file mode 100644 index 0000000..ba7c091 --- /dev/null +++ "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\210\206\345\261\202\346\211\223\345\215\260.md" @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } +}; + +//层序遍历二叉树 +void travel(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + cout << q->vec << endl; + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + } +} + +//分层打印二叉树 +void travel_by_layer(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + int size = bt_queue.size(); + for (int i = 0; i < size; i++) + { + BinaryTree * q = bt_queue.front(); + cout << q->vec << " "; + bt_queue.pop(); + + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + cout << endl; + } + } +} + + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + travel_by_layer(s_arr[0]); + + return 0; +} \ No newline at end of file diff --git "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" new file mode 100644 index 0000000..ba7c091 --- /dev/null +++ "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } +}; + +//层序遍历二叉树 +void travel(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + cout << q->vec << endl; + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + } +} + +//分层打印二叉树 +void travel_by_layer(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + int size = bt_queue.size(); + for (int i = 0; i < size; i++) + { + BinaryTree * q = bt_queue.front(); + cout << q->vec << " "; + bt_queue.pop(); + + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + cout << endl; + } + } +} + + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + travel_by_layer(s_arr[0]); + + return 0; +} \ No newline at end of file diff --git "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\344\275\216\345\205\254\345\205\261\347\245\226\345\205\210/readme.md" "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\344\275\216\345\205\254\345\205\261\347\245\226\345\205\210/readme.md" index 7f6d345..a59e04f 100644 --- "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\344\275\216\345\205\254\345\205\261\347\245\226\345\205\210/readme.md" +++ "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\344\275\216\345\205\254\345\205\261\347\245\226\345\205\210/readme.md" @@ -1,4 +1,4 @@ -## 二叉树的最低公共祖先 +## 二叉树的最近公共祖先 给出二叉树上的两个结点A,B,返回他们的最低公共祖先。 diff --git "a/---\344\272\214\345\217\211\346\240\221---/\345\210\244\346\226\255\344\270\200\346\243\265\346\240\221\346\230\257\344\270\215\346\230\257\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/readme.md" "b/---\344\272\214\345\217\211\346\240\221---/\345\210\244\346\226\255\344\270\200\346\243\265\346\240\221\346\230\257\344\270\215\346\230\257\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/readme.md" new file mode 100644 index 0000000..126a79d --- /dev/null +++ "b/---\344\272\214\345\217\211\346\240\221---/\345\210\244\346\226\255\344\270\200\346\243\265\346\240\221\346\230\257\344\270\215\346\230\257\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/readme.md" @@ -0,0 +1,136 @@ +## 判断一棵树是不是完全二叉树 + +``` +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } +}; + +//层序遍历二叉树 +void travel(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + cout << q->vec << endl; + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + } +} + +//分层打印二叉树 +void travel_by_layer(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + int size = bt_queue.size(); + for (int i = 0; i < size; i++) + { + BinaryTree * q = bt_queue.front(); + cout << q->vec << " "; + bt_queue.pop(); + + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + cout << endl; + } + } +} + + +/* +任意的一个二叉树,都可以补成一个满二叉树。这样中间就会有很多空洞。在广度优先遍历的时候,如果是满二叉树,或者完全二叉树,这些空洞是在广度优先的遍历的末尾,所以,但我们遍历到空洞的时候,整个二叉树就已经遍历完成了。而如果,是非完全二叉树, + +我们遍历到空洞的时候,就会发现,空洞后面还有没有遍历到的值。这样,只要根据是否遍历到空洞,整个树的遍历是否结束来判断是否是完全的二叉树。 +*/ +bool iscomplete_tree(BinaryTree * root) +{ + queue bt_queue; + if (root != NULL) + { + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + if (q == NULL) + { + break; + } + bt_queue.push(q->left); + bt_queue.push(q->right); + } + } + + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + + if (q != NULL) + { + return false; + } + } + return true; +} + + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + //travel_by_layer(s_arr[0]); + + cout << iscomplete_tree(s_arr[0]) << endl; + + return 0; +} +``` \ No newline at end of file From b682230feb84985e61c313c64cc2f07b8d1406c2 Mon Sep 17 00:00:00 2001 From: zhaozheng's laptop Date: Sat, 8 Sep 2018 21:59:08 +0800 Subject: [PATCH 06/19] add --- ...6\347\232\204\351\227\256\351\242\230.cpp" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" diff --git "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" new file mode 100644 index 0000000..8f3cf77 --- /dev/null +++ "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" @@ -0,0 +1,39 @@ + +class Solution +{ + public: + void handle_solution(TreeNode * root, int target,int &sum , vector path ,vector> & res) + { + if(root != NULL) + { + if(sum + root->val == target && root->left==NULL && root->right ==NULL) + { + path.push_back(root->val); + res.push_back(path); + + //清除递归条件 + path.pop_back(); + return; + } + else + { + path.push_back(root->val); + sum=sum+root->val; + handle_solution(root->left,target,sum,path,res); + handle_solution(root->right,target,sum,path,res); + //清除递归条件 + sum=sum-root->val; + path.pop_back(); + } + } + return; + } + vector> binaryTreePathSum(TreeNode * root, int target) { + // write your code here + vector path; + vector> res; + int sum = 0; + handle_solution(root,target,sum,path,res); + return res; + } +}; \ No newline at end of file From c8eefeda580c7d8ff6f67fd1e191948970bf3abb Mon Sep 17 00:00:00 2001 From: zhaozheng's laptop Date: Sun, 9 Sep 2018 11:31:42 +0800 Subject: [PATCH 07/19] add --- .../readme.md" | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 "---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\351\235\236\351\200\222\345\275\222\351\201\215\345\216\206/readme.md" diff --git "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\351\235\236\351\200\222\345\275\222\351\201\215\345\216\206/readme.md" "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\351\235\236\351\200\222\345\275\222\351\201\215\345\216\206/readme.md" new file mode 100644 index 0000000..637d154 --- /dev/null +++ "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\347\232\204\351\235\236\351\200\222\345\275\222\351\201\215\345\216\206/readme.md" @@ -0,0 +1,132 @@ +## 二叉树的 前序/中序/后序 遍历 + +``` +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } + int flag; +}; + +void pre_travel(BinaryTree * root) +{ + stack bt_stack; + while (root != NULL || !bt_stack.empty()) + { + while (root != NULL) + { + cout << root->vec << endl; + bt_stack.push(root); + root = root->left; + } + if (!bt_stack.empty()) + { + root = bt_stack.top(); + bt_stack.pop(); + root = root->right; + } + } +} + +void mid_travel(BinaryTree * root) +{ + stack bt_stack; + while (root != NULL || !bt_stack.empty()) + { + while (root != NULL) + { + bt_stack.push(root); + root = root->left; + } + if (!bt_stack.empty()) + { + root = bt_stack.top(); + cout << root->vec << endl; + bt_stack.pop(); + root = root->right; + } + } +} + +/* +对于任一结点P,将其入栈,然后沿其左子树一直往下搜索,直到搜索到没有左孩子的结点,此时该结点出现在栈顶, +但是此时不能将其出栈并访问,因此其右孩子还为被访问。 + +所以接下来按照相同的规则对其右子树进行相同的处理,当访问完其右孩子时,该结点又出现在栈顶, +此时可以将其出栈并访问。这样就保证了正确的访问顺序。 + +可以看出,在这个过程中,每个结点都两次出现在栈顶,只有在第二次出现在栈顶时,才能访问它。 +因此需要多设置一个变量标识该结点是否是第一次出现在栈顶。 +*/ +void post_travel(BinaryTree * root) +{ + stack bt_stack; + while (root != NULL || !bt_stack.empty()) + { + while (root != NULL) + { + root->flag = 0; + bt_stack.push(root); + root = root->left; + } + + if (!bt_stack.empty()) + { + root = bt_stack.top(); + bt_stack.pop(); + + //flag =0 说明右子树没有遍历 + if (root->flag == 0) + { + root->flag = 1; + bt_stack.push(root); + root = root->right; + } + else + { + cout << root->vec << endl; + root = NULL; + } + } + } +} + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + //travel_by_layer(s_arr[0]); + + //cout << iscomplete_tree(s_arr[0]) << endl; + + //pre_travel(s_arr[0]); + //mid_travel(s_arr[0]); + post_travel(s_arr[0]); + + return 0; +} +``` \ No newline at end of file From cddc33c8a793069fd5e010d08f2c515015a01929 Mon Sep 17 00:00:00 2001 From: zhaozheng's laptop Date: Tue, 18 Sep 2018 16:23:37 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E5=89=8D=E5=BA=8F=E5=92=8C=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=20=E6=9E=84=E5=BB=BA=E4=BA=8C=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../readme.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "---\345\205\263\344\272\216\344\272\214\345\217\211\346\240\221\347\232\204\351\242\230\347\233\256---/\351\200\232\350\277\207\345\211\215\345\272\217\345\222\214\344\270\255\345\272\217\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221/readme.md" diff --git "a/---\345\205\263\344\272\216\344\272\214\345\217\211\346\240\221\347\232\204\351\242\230\347\233\256---/\351\200\232\350\277\207\345\211\215\345\272\217\345\222\214\344\270\255\345\272\217\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221/readme.md" "b/---\345\205\263\344\272\216\344\272\214\345\217\211\346\240\221\347\232\204\351\242\230\347\233\256---/\351\200\232\350\277\207\345\211\215\345\272\217\345\222\214\344\270\255\345\272\217\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221/readme.md" new file mode 100644 index 0000000..2542b0f --- /dev/null +++ "b/---\345\205\263\344\272\216\344\272\214\345\217\211\346\240\221\347\232\204\351\242\230\347\233\256---/\351\200\232\350\277\207\345\211\215\345\272\217\345\222\214\344\270\255\345\272\217\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221/readme.md" @@ -0,0 +1,61 @@ +# 通过前序和中序遍历构建二叉树 + +``` + +struct Node +{ + int val; + Node * left; + Node * right; +}; +int get_index(vector vi, int val) +{ + int index = 0; + for (auto ele : vi) + { + if (ele == val) + { + return index; + } + index++; + } + return -1; +} +Node * solution(vector pre, vector mid) +{ + if (pre.size()==0||pre.size()!=mid.size()) + { + return NULL; + } + else + { + Node * root = new Node(); + root->val = pre[0]; + root->left = NULL; + root->right = NULL; + + int index = get_index(mid, pre[0]); + + vector pre_left(pre.begin()+1, pre.begin()+index+1); + vector pre_right(pre.begin()+index+1, pre.end()); + vector mid_left(mid.begin(),mid.begin()+index); + vector mid_right(mid.begin()+index+1, mid.end()); + + + + root->left = solution(pre_left, mid_left); + root->right = solution(pre_right, mid_right); + return root; + } +} + +int main() +{ + vector pre = { 1,2,4,7,3,5,6,8 }; + vector mid = { 4,7,2,1,5,3,8,6 }; + + Node * root = solution(pre, mid); + + return 0; +} +``` \ No newline at end of file From bd86c6d883908f160da306765f692215e78398b9 Mon Sep 17 00:00:00 2001 From: zhaozheng's laptop Date: Tue, 18 Sep 2018 17:05:41 +0800 Subject: [PATCH 09/19] =?UTF-8?q?add=20=E4=BA=8C=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../readme.md" | 30 ++++- .../readme.md" | 108 ++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 "---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\345\257\273\346\211\276\347\273\223\347\202\271\345\210\260\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204/readme.md" diff --git "a/---\344\272\214\345\217\211\346\240\221---/readme.md" "b/---\344\272\214\345\217\211\346\240\221---/readme.md" index 9f5ed88..c057b23 100644 --- "a/---\344\272\214\345\217\211\346\240\221---/readme.md" +++ "b/---\344\272\214\345\217\211\346\240\221---/readme.md" @@ -1 +1,29 @@ -## ڶĿ \ No newline at end of file +# 二叉树的问题 + + +二叉树的很多问题(尤其是递归的问题)都是二叉树前序遍历的思想可以解决。 + +//以打印根节点到叶子结点的路径为例 +``` +void solution(Node * root,vector path) +{ + + if(root != null) + { + //visit操作 + path.push_back(root->val); + + if (root->left == NULL && root->right == NULL) + { + print_vector(path); + } + + //递归 + solution(root->left,path); + solution(root->right,path); + + //清除递归条件 + path.pop_back(); + } +} +``` \ No newline at end of file diff --git "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\345\257\273\346\211\276\347\273\223\347\202\271\345\210\260\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204/readme.md" "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\345\257\273\346\211\276\347\273\223\347\202\271\345\210\260\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204/readme.md" new file mode 100644 index 0000000..bc98269 --- /dev/null +++ "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\345\257\273\346\211\276\347\273\223\347\202\271\345\210\260\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204/readme.md" @@ -0,0 +1,108 @@ +## 二叉树从根节点到子节点的路径 + +``` +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct Node +{ + int val; + Node * left; + Node * right; +}; +int get_index(vector vi, int val) +{ + int index = 0; + for (auto ele : vi) + { + if (ele == val) + { + return index; + } + index++; + } + return -1; +} +Node * solution(vector pre, vector mid) +{ + if (pre.size()==0||pre.size()!=mid.size()) + { + return NULL; + } + else + { + Node * root = new Node(); + root->val = pre[0]; + root->left = NULL; + root->right = NULL; + + int index = get_index(mid, pre[0]); + + vector pre_left(pre.begin()+1, pre.begin()+index+1); + vector pre_right(pre.begin()+index+1, pre.end()); + vector mid_left(mid.begin(),mid.begin()+index); + vector mid_right(mid.begin()+index+1, mid.end()); + + + + root->left = solution(pre_left, mid_left); + root->right = solution(pre_right, mid_right); + return root; + } +} +void print_vector(vector path) +{ + for (auto ele : path) + { + cout << ele << " "; + } + cout << endl; +} + + +// 寻找根节点到子节点的所有路径 +// 这个是类似与前序遍历的递归 +void solution2(Node * root,vector & path) +{ + if (root != NULL) + { + //类似于前序遍历里面的visit操作 + path.push_back(root->val); + if (root->left == NULL && root->right == NULL) + { + print_vector(path); + } + + //递归 + solution2(root->left, path); + solution2(root->right, path); + + //清楚递归条件 + path.pop_back(); + } +} + +int main() +{ + //构造二叉树 + vector pre = { 1,2,4,7,3,5,6,8 }; + vector mid = { 4,7,2,1,5,3,8,6 }; + Node * root = solution(pre, mid); + + //打印根结点到子节点的路径 + vector path; + solution2(root,path); + + return 0; +} + +``` \ No newline at end of file From 0fefc2500937ef52592d64a253d0538c399ffe2f Mon Sep 17 00:00:00 2001 From: zhaozheng's laptop Date: Tue, 18 Sep 2018 17:06:20 +0800 Subject: [PATCH 10/19] edited --- ...6\347\232\204\351\227\256\351\242\230.cpp" | 39 ------------------- 1 file changed, 39 deletions(-) delete mode 100644 "---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" diff --git "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" "b/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" deleted file mode 100644 index 8f3cf77..0000000 --- "a/---\344\272\214\345\217\211\346\240\221---/\344\272\214\345\217\211\346\240\221\351\201\215\345\216\206\347\232\204\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,39 +0,0 @@ - -class Solution -{ - public: - void handle_solution(TreeNode * root, int target,int &sum , vector path ,vector> & res) - { - if(root != NULL) - { - if(sum + root->val == target && root->left==NULL && root->right ==NULL) - { - path.push_back(root->val); - res.push_back(path); - - //清除递归条件 - path.pop_back(); - return; - } - else - { - path.push_back(root->val); - sum=sum+root->val; - handle_solution(root->left,target,sum,path,res); - handle_solution(root->right,target,sum,path,res); - //清除递归条件 - sum=sum-root->val; - path.pop_back(); - } - } - return; - } - vector> binaryTreePathSum(TreeNode * root, int target) { - // write your code here - vector path; - vector> res; - int sum = 0; - handle_solution(root,target,sum,path,res); - return res; - } -}; \ No newline at end of file From a082eeac2e06741e5e4a5f824b373db488987d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Wed, 19 Sep 2018 15:08:56 +0800 Subject: [PATCH 11/19] edited --- .../readme.md" | 57 ++++++++----------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" index 97a2c02..656ee01 100644 --- "a/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" +++ "b/---\347\231\275\346\235\277\345\206\231\344\273\243\347\240\201---/readme.md" @@ -231,50 +231,41 @@ * 第K大的数组 ``` - #include - #include - using namespace std; - - int partition(vector & vi ,int begin ,int end) + int get_partition(vector &vi, int begin, int end) { - int partition_val = vi[end]; - if(begin vi[i]) { - if(vi[i] < partition_val) - { - swap(vi[i],vi[sorted]); - sorted++; - } + swap(vi[i], vi[sorted]); + sorted++; } - swap(vi[sorted],vi[end]); - return sorted; } + swap(vi[sorted], vi[end]); + return sorted; } - int kth(vector &vi,int begin,int end,int k) + void topk(vector & vi,int k) { - //这个边界条件好像有问题 - if(begink) - return kth(vi,begin,index-1,k); + while (index != k - 1) + { + if (index < k - 1) + { + index = get_partition(vi, index + 1, end); + } else - return vi[k]; + { + index = get_partition(vi, begin, index-1); + } } - } - - int main() - { - int arr[]={4,1,2,10,9,7,0,13}; - vector vi(arr,arr+8); - cout< Date: Wed, 19 Sep 2018 15:24:53 +0800 Subject: [PATCH 12/19] add parition --- "\345\277\253\346\216\222/quicksort.cpp" | 78 ++++++++ .../kth.cpp" | 167 +++++++----------- 2 files changed, 143 insertions(+), 102 deletions(-) create mode 100644 "\345\277\253\346\216\222/quicksort.cpp" diff --git "a/\345\277\253\346\216\222/quicksort.cpp" "b/\345\277\253\346\216\222/quicksort.cpp" new file mode 100644 index 0000000..94c189e --- /dev/null +++ "b/\345\277\253\346\216\222/quicksort.cpp" @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int get_partition(vector &vi, int begin, int end) +{ + int partition = vi[end]; + int sorted = begin; + for (int i = begin; i < end; i++) + { + if (partition > vi[i]) + { + swap(vi[i], vi[sorted]); + sorted++; + } + } + swap(vi[sorted], vi[end]); + return sorted; +} + +void quicksort(vector & vi,int begin,int end) +{ + if (begin < end) + { + int index = get_partition(vi, begin, end); + quicksort(vi, begin, index - 1); + quicksort(vi, index + 1, end); + } +} + +void topk(vector & vi,int k) +{ + int begin = 0; + int end = vi.size() - 1; + int index = get_partition(vi, begin, end); + + while (index != k - 1) + { + if (index < k - 1) + { + index = get_partition(vi, index + 1, end); + } + else + { + index = get_partition(vi, begin, index-1); + } + } + + cout << vi[index] << endl; +} + +int main() +{ + + vector vi = { 4,12,1,10,5,7,0,3,2,55 }; + + quicksort(vi, 0, vi.size() - 1); + + for (auto i : vi) + { + cout << i << " "; + } + + + topk(vi, 3); + topk(vi, 4); + topk(vi, 5); + return 0; +} \ No newline at end of file diff --git "a/\346\225\260\347\273\204\351\207\214\351\235\242\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/kth.cpp" "b/\346\225\260\347\273\204\351\207\214\351\235\242\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/kth.cpp" index 6a25399..94c189e 100644 --- "a/\346\225\260\347\273\204\351\207\214\351\235\242\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/kth.cpp" +++ "b/\346\225\260\347\273\204\351\207\214\351\235\242\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/kth.cpp" @@ -1,115 +1,78 @@ -class Solution{ -public: - //使用快排划分的思想 - //partition 和partiton2 的区别是partition是一个通用的,partition2指定了搜索的区间 - //int partition(vector& nums){ - // int i=0; - // int j=nums.size()-1; - // int part=nums[0]; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; - // while(i=part&&(i &vi, int begin, int end) +{ + int partition = vi[end]; + int sorted = begin; + for (int i = begin; i < end; i++) + { + if (partition > vi[i]) + { + swap(vi[i], vi[sorted]); + sorted++; + } + } + swap(vi[sorted], vi[end]); + return sorted; +} + +void quicksort(vector & vi,int begin,int end) +{ + if (begin < end) + { + int index = get_partition(vi, begin, end); + quicksort(vi, begin, index - 1); + quicksort(vi, index + 1, end); + } +} - // while(nums[i]<=part&&(ipart){ - // swap(nums[i],nums[j]); - // j--; - // } - // } +void topk(vector & vi,int k) +{ + int begin = 0; + int end = vi.size() - 1; + int index = get_partition(vi, begin, end); - // //如果是快排的话,进入下一层的递归 - // //但是,我们这里不是快排 - // return i; - //} + while (index != k - 1) + { + if (index < k - 1) + { + index = get_partition(vi, index + 1, end); + } + else + { + index = get_partition(vi, begin, index-1); + } + } - int partition2(vector& nums,int begin,int end){ - int i=begin; - int j=end; - int part=nums[begin]; + cout << vi[index] << endl; +} - while(i=part&&(ipart){ - swap(nums[i],nums[j]); - j--; - } - } + vector vi = { 4,12,1,10,5,7,0,3,2,55 }; - //如果是快排的话,进入下一层的递归 - //但是,我们这里不是快排 - return i; - } - void kth(int k, vector nums,int begin,int end,int& res){ - //返回的第k大元素的下标,比如第4大的元素的index=3 - int index=partition2(nums,begin,end); - - //index_th表示返回的元素是第几大的 - int index_th=index+1; + quicksort(vi, 0, vi.size() - 1); - if(index_th==k){ - int ret=nums[index]; - res=ret; - } - //要找第七大的元素,结果现在找到了第五大的(index=5),那么就在后面找第(7-5)大的元素 - else if (index_th nums) { - int res=-1; - //比如说第十大的元素,转换成k_index= nums.size()-k+1,k_index=1,相当于转换成第1小的元素 - int k_index=nums.size()-k+1; - kth(k_index,nums,0,nums.size()-1,res); - return res; - } -}; -int main(){ - int arr[]={1,2,3,4,5,6,8,9,10,7}; - const int len=10; - int k_th=10; - //int arr[]={9,3,2,4,8}; - //const int len=5; - //int k_th=3; - vector vi(arr,arr+len); - Solution s; - int ret=s.kthLargestElement(k_th,vi); - cout< Date: Thu, 20 Sep 2018 09:51:53 +0800 Subject: [PATCH 13/19] add --- .../readme.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\2721/readme.md" "b/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\2721/readme.md" index 18a7a89..d3c42fe 100644 --- "a/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\2721/readme.md" +++ "b/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\2721/readme.md" @@ -10,4 +10,6 @@ ``` //维护两个数据结构 //一个记录目前为止的 最小值 minprices ,一个记录目前的最大maxprofit -``` \ No newline at end of file +``` + +这个题的本质和最长子数组是一样的。 From 9d559c98d1961308b1f4bd9c4ca68bd232157dd8 Mon Sep 17 00:00:00 2001 From: zhaozheng's laptop Date: Sun, 21 Oct 2018 18:41:23 +0800 Subject: [PATCH 14/19] add poll --- .../poll/poll_test.c" | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 "---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/poll/poll_test.c" diff --git "a/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/poll/poll_test.c" "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/poll/poll_test.c" new file mode 100644 index 0000000..cfc42dc --- /dev/null +++ "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/poll/poll_test.c" @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_FD_NUM 20 +#define MAXLEN 1024 + +int main(int argc,char* argv[]) +{ + printf("server start up\n"); + + if(argc <= 2) + { + printf("usage:%s ip port\n",basename(argv[0])); + return 1; + } + + //IPַ + const char* ip = argv[1]; + //˿ں + int port = atoi(argv[2]); + //ں˼е󳤶ȣȫӵsocket + //int backlog = atoi(argv[3]); + + //socket (TIP/IPЭ壬ʽsocket) + int server_sockfd = socket(PF_INET,SOCK_STREAM,0); + + //TCP/IPЭsocketַṹ + struct sockaddr_in server_addr; + bzero(&server_addr,sizeof(server_addr)); + server_addr.sin_family = AF_INET; //TCP/IPv4ĵַ + inet_pton(AF_INET,ip,&server_addr.sin_addr); //IPַַתΪƵaddr.sin_server_addr + server_addr.sin_port = htons(port); //˿ڣhost to netֽСˣתΪֽ򣨴ˣ + + //ļsocksocketַҪͻԶ󶨵ַ + //עҪǿתΪ struct sockaddr* + int ret = bind(server_sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)); + assert(ret != -1); + + // + ret = listen(server_sockfd,MAX_FD_NUM-1); + assert(ret != -1); + + //ȴͻЩӵع + sleep(3); + + //ͻ˵ַϢ + struct sockaddr_in client_addr; + socklen_t client_addr_len = sizeof(struct sockaddr_in); + + //poll fds + struct pollfd pollfdArry[MAX_FD_NUM]; + for(int i=0;i MAX_FD_NUM) + { + printf("socket num to much\n"); + } + else + { + //,ܵԶsockַϢڵڶ + //ֻǴӼȡӣʹͻѾϿҲacceptɹ + int client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_addr,&client_addr_len); + if(client_sockfd < 0) + { + perror("accept"); + } + else + { + //inet_ntoa(struct addr_in) IPַתΪַ + printf("accept client_addr %s\n",inet_ntoa(client_addr.sin_addr)); + for(int i=0;i Date: Sun, 7 Apr 2019 11:08:19 +0800 Subject: [PATCH 15/19] update --- 1.PNG | Bin 11386 -> 0 bytes README.md | 7 ------- 2 files changed, 7 deletions(-) delete mode 100644 1.PNG diff --git a/1.PNG b/1.PNG deleted file mode 100644 index d40cbe021e79c86e7a260911a3eb45c3f9feac55..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 11386 zcmdUVXH?VQlXnONLg*R+5g{}Q(h(_24PA<%NR=WTq>6$_k2C=VRHO?L1nGzrk=_)g zOYfjaktzrQL)jaDyZdfmJbTXW^WU6967Jl&cV_O)XXbk|kFa_gbO<&C1OlPc(o{8o zK%g)PgsdG-2}TN<&BDM3nU8@622$F~z5@QCa8lM)hCtrN(;VIcGa=Monr1!_2z?vr zkL;7@8+!;uh(SwL+33F2dS+}E`;An>?1?}~@2gN0Lep2xbTnw-h{D(M^1gZkCQBYo16O-f{}@7;2K&z@0I^0f&L7_W7bSO zHg>d!#X2x7Iy&0U=;zxZhZ3u@5j-ZWK9s<4JS=xuaw0_tTo(XKu|G73dd;D2Ir4MY z_54MorUVxw*!hWd!6$>{ss|0`QS`708WmO5_BB2vAiPS#hJrh))Zhq)llkpl_ z8!Mh>!3H<(+}sWv;j-4wnq>H>7&Vo=RofA)9vv++z|4zWlenZopH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*@qxPaFZH!& z_mBIpfkbwbPw9Ej55bm6&|GIbJmE0EzFPJ*0%v1AMg!d*JI)lQ)kdeFrzy`T+mA{b zD4{h0G{lx5_lq27PL5-C8crhvF=1S96u+QO=pys< z;~SiUN3XWu#a_hQ;GV0mQR0p(S7)=3h)N=>fM!03gykn}yy9M8P-q_5Ti5l9{y#y+ z%X`0hVDs4WKL49Ckl*Tzuln@eKrp@0tqkxRS1-9R85*2J9|h$zVnmz*+F>35V5qm< zd_wsipKY z<8uND#}cfP&(HIN{=*Ae=is>sWqe4@md(reLx#fsSz_E7%_?KiPml=;0KY!~+Z0!4 z#jH)yevORC#rD^^Z%L|pgOH5-n({hv&$#m%x$(IWtib*>g$~-Z1cZ;h#wCNfE7o*E zXkL;Q+O(^CkF<`GX9)*Ewlc{5qdDUy>$Lzm682i5%V#}jMx>1{gs{#hLKSHl5ynL$ zS}fyX@xVY5tyj{E8$+g77t;(vG_#6U8B&it|qTJ!0nAE==rqyW`qe_ zKJhkvNYI8vACvu7M;uNbDn^efmg9=^WU#tEXCRflDH9*7BS;(hj0FZf z`<5<(T)!?TXOLl{j7T148Jis^L~E6UmHWS!-Y4Eh;yOB3f5u|E&Hoc0Oa7y)wPpRP z;2um@r7#rNb E0pUt=`v3p{ diff --git a/README.md b/README.md index f3469fa..b219794 100644 --- a/README.md +++ b/README.md @@ -148,10 +148,3 @@ leetcode/lintcode上的算法题 如果你对机器学习的算法感兴趣,欢迎共同讨论: https://github.com/zhaozhengcoder/Machine-Learning - - -### Flag - -刷到200题吧~ - -![](1.PNG) \ No newline at end of file From e3facadbe8f41f38bf8d076f4de42b62a3c8b422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Sun, 7 Jul 2019 10:09:38 +0800 Subject: [PATCH 16/19] add --- .../epoll/epoll_test.cpp" | 151 ++++++++++++++++++ ...00\344\272\233\346\246\202\345\277\265.md" | 0 ...75\347\232\204\345\206\231\346\263\225.md" | 0 .../readme.md" => ---tips---/readme.md | 0 ...50\345\272\223\345\207\275\346\225\260.md" | 0 5 files changed, 151 insertions(+) create mode 100644 "---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test.cpp" rename "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/C++\347\232\204\344\270\200\344\272\233\346\246\202\345\277\265.md" => "---tips---/C++\347\232\204\344\270\200\344\272\233\346\246\202\345\277\265.md" (100%) rename "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/mark\344\270\200\344\270\213\345\245\275\347\232\204\345\206\231\346\263\225.md" => "---tips---/mark\344\270\200\344\270\213\345\245\275\347\232\204\345\206\231\346\263\225.md" (100%) rename "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/readme.md" => ---tips---/readme.md (100%) rename "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/\346\233\264\345\245\275\347\232\204\344\275\277\347\224\250\345\272\223\345\207\275\346\225\260.md" => "---tips---/\346\233\264\345\245\275\347\232\204\344\275\277\347\224\250\345\272\223\345\207\275\346\225\260.md" (100%) diff --git "a/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test.cpp" "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test.cpp" new file mode 100644 index 0000000..132b1be --- /dev/null +++ "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test.cpp" @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAXEPOLL 10000 +#define MAXLINE 1024 +#define PORT 6000 +#define MAXBACK 1000 + +//设置非阻塞 +int setnonblocking(int fd) +{ + if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK) == -1) + { + printf("Set blocking error : %d\n", errno); + return -1; + } + return 0; +} + +int main(int argc, char **argv) +{ + int listen_fd; + int conn_fd; + int epoll_fd; + int nread; + int cur_fds; //!> 当前已经存在的数量 + int wait_fds; //!> epoll_wait 的返回值 + int i; + struct sockaddr_in servaddr; + struct sockaddr_in cliaddr; + struct epoll_event ev; + struct epoll_event evs[MAXEPOLL]; + struct rlimit rlt; //!> 设置连接数所需 + char buf[MAXLINE]; + socklen_t len = sizeof(struct sockaddr_in); + + //设置每个进程允许打开的最大文件数 + //每个主机是不一样的哦,一般服务器应该很大吧! + rlt.rlim_max = rlt.rlim_cur = MAXEPOLL; + if (setrlimit(RLIMIT_NOFILE, &rlt) == -1) + { + printf("Setrlimit Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //!> server 套接口 + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(PORT); + + //建立套接字 + if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + printf("Socket Error...%d\n", errno); + exit(EXIT_FAILURE); + } + + //设置非阻塞模式 + if (setnonblocking(listen_fd) == -1) + { + printf("Setnonblocking Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //绑定 + if (bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) == -1) + { + printf("Bind Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //监听 + if (listen(listen_fd, MAXBACK) == -1) + { + printf("Listen Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //创建epoll + epoll_fd = epoll_create(MAXEPOLL); //!> create + ev.events = EPOLLIN | EPOLLET; //!> accept Read! + ev.data.fd = listen_fd; //!> 将listen_fd 加入 + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev) < 0) + { + printf("Epoll Error : %d\n", errno); + exit(EXIT_FAILURE); + } + cur_fds = 1; + + while (1) + { + if ((wait_fds = epoll_wait(epoll_fd, evs, cur_fds, -1)) == -1) + { + printf("Epoll Wait Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + for (i = 0; i < wait_fds; i++) + { + // 处理新接入的accept + if (evs[i].data.fd == listen_fd && cur_fds < MAXEPOLL) + { + cout << "debug, evs[" << i << "] events " << ev.events << " EPOLLIN : " << EPOLLIN << endl; + + if ((conn_fd = accept(listen_fd, (struct sockaddr *)&cliaddr, &len)) == -1) + { + printf("Accept Error : %d\n", errno); + exit(EXIT_FAILURE); + } + printf("Server get from client !\n" /*, inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port */); + ev.events = EPOLLIN | EPOLLET; //!> accept Read! + ev.data.fd = conn_fd; //!> 将conn_fd 加入 + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, conn_fd, &ev) < 0) + { + printf("Epoll Error : %d\n", errno); + exit(EXIT_FAILURE); + } + ++cur_fds; + continue; + } + //!> 下面处理数据 + + cout << "deug read, evs[" << i << "] events " << ev.events << " EPOLLIN : " << EPOLLIN << endl; + nread = read(evs[i].data.fd, buf, sizeof(buf)); + if (nread <= 0) //!> 结束后者出错 + { + close(evs[i].data.fd); + epoll_ctl(epoll_fd, EPOLL_CTL_DEL, evs[i].data.fd, &ev); //!> 删除计入的fd + --cur_fds; //!> 减少一个呗! + continue; + } + write(evs[i].data.fd, buf, nread); //!> 回写 + } + } + + close(listen_fd); + return 0; +} \ No newline at end of file diff --git "a/---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/C++\347\232\204\344\270\200\344\272\233\346\246\202\345\277\265.md" "b/---tips---/C++\347\232\204\344\270\200\344\272\233\346\246\202\345\277\265.md" similarity index 100% rename from "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/C++\347\232\204\344\270\200\344\272\233\346\246\202\345\277\265.md" rename to "---tips---/C++\347\232\204\344\270\200\344\272\233\346\246\202\345\277\265.md" diff --git "a/---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/mark\344\270\200\344\270\213\345\245\275\347\232\204\345\206\231\346\263\225.md" "b/---tips---/mark\344\270\200\344\270\213\345\245\275\347\232\204\345\206\231\346\263\225.md" similarity index 100% rename from "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/mark\344\270\200\344\270\213\345\245\275\347\232\204\345\206\231\346\263\225.md" rename to "---tips---/mark\344\270\200\344\270\213\345\245\275\347\232\204\345\206\231\346\263\225.md" diff --git "a/---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/readme.md" b/---tips---/readme.md similarity index 100% rename from "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/readme.md" rename to ---tips---/readme.md diff --git "a/---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/\346\233\264\345\245\275\347\232\204\344\275\277\347\224\250\345\272\223\345\207\275\346\225\260.md" "b/---tips---/\346\233\264\345\245\275\347\232\204\344\275\277\347\224\250\345\272\223\345\207\275\346\225\260.md" similarity index 100% rename from "---tips---\345\206\231\346\233\264\345\245\275\347\232\204\344\273\243\347\240\201/\346\233\264\345\245\275\347\232\204\344\275\277\347\224\250\345\272\223\345\207\275\346\225\260.md" rename to "---tips---/\346\233\264\345\245\275\347\232\204\344\275\277\347\224\250\345\272\223\345\207\275\346\225\260.md" From fae72506f41ca64f34ac630f2758c252205b693f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Sun, 7 Jul 2019 13:24:26 +0800 Subject: [PATCH 17/19] add epoll event --- .../epoll/epoll_test_event.cpp" | 226 ++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 "---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" diff --git "a/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" new file mode 100644 index 0000000..33233f3 --- /dev/null +++ "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" @@ -0,0 +1,226 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define IPADDRESS "127.0.0.1" +#define PORT 6000 +#define MAXSIZE 1024 +#define LISTENQ 5 +#define FDSIZE 1000 +#define EPOLLEVENTS 100 + +#define MAXLINE 1024 +char buf[MAXLINE]; + +static void handle_accpet(int epollfd, int listenfd); +static void do_read(int epollfd, int fd, char *buf); +static void do_write(int epollfd, int fd, char *buf); +static void delete_event(int epollfd, int fd, int state); +static void modify_event(int epollfd, int fd, int state); + +//设置非阻塞 +int setnonblocking(int fd) +{ + if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK) == -1) + { + printf("Set blocking error : %d\n", errno); + return -1; + } + return 0; +} + +//事件处理函数 +static void handle_events(int epollfd, struct epoll_event *events, int num, int listenfd, char *buf) +{ + int i; + int fd; + //进行遍历;这里只要遍历已经准备好的io事件。num并不是当初epoll_create时的FDSIZE。 + + cout << "[debug] num : " << num << " beign for" << endl; + for (i = 0; i < num; i++) + { + fd = events[i].data.fd; + //根据描述符的类型和事件类型进行处理 + if ((fd == listenfd) && (events[i].events & EPOLLIN)) + { + handle_accpet(epollfd, listenfd); + } + else if (events[i].events & EPOLLIN) + { + // 找do_read函数中,读到的数据写到buff上面 + do_read(epollfd, fd, buf); + } + else if (events[i].events & EPOLLOUT) + { + cout << "[debug] write" << endl; + do_write(epollfd, fd, buf); + } + } + cout << "[debug] end for" << endl; + cout << endl; +} + +//添加事件 +static void add_event(int epollfd, int fd, int state) +{ + struct epoll_event ev; + ev.events = state; + ev.data.fd = fd; + epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); +} + +//处理接收到的连接 +static void handle_accpet(int epollfd, int listenfd) +{ + int clifd; + struct sockaddr_in cliaddr; + socklen_t cliaddrlen; + clifd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddrlen); + if (clifd == -1) + perror("accpet error:"); + else + { + printf("accept a new client: %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port); //添加一个客户描述符和事件 + add_event(epollfd, clifd, EPOLLIN); + } +} + +//读处理 +static void do_read(int epollfd, int fd, char *buf) +{ + int nread; + // read数据到buf上面 + nread = read(fd, buf, MAXSIZE); + if (nread == -1) + { + perror("read error:"); + close(fd); //记住close fd + delete_event(epollfd, fd, EPOLLIN); //删除监听 + } + else if (nread == 0) + { + fprintf(stderr, "client close.\n"); + close(fd); //记住close fd + delete_event(epollfd, fd, EPOLLIN); //删除监听 + } + else + { + printf("[fd %d] read message is : %s", fd, buf); + //修改描述符对应的事件,由读改为写 + modify_event(epollfd, fd, EPOLLOUT); + } +} + +//写处理 +static void do_write(int epollfd, int fd, char *buf) +{ + int nwrite; + nwrite = write(fd, buf, strlen(buf)); + if (nwrite == -1) + { + perror("write error:"); + close(fd); //记住close fd + delete_event(epollfd, fd, EPOLLOUT); //删除监听 + } + else + { + //修改描述符对应的事件,由写改为读 + modify_event(epollfd, fd, EPOLLIN); + } + memset(buf, 0, MAXSIZE); +} + +//删除事件 +static void delete_event(int epollfd, int fd, int state) +{ + struct epoll_event ev; + ev.events = state; + ev.data.fd = fd; + epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev); +} + +//修改事件 +static void modify_event(int epollfd, int fd, int state) +{ + struct epoll_event ev; + ev.events = state; + ev.data.fd = fd; + epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &ev); +} + +int socket_bind(int port) +{ + int listen_fd; + + struct sockaddr_in servaddr; + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(port); + + //建立套接字 + if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + printf("Socket Error...%d\n", errno); + exit(EXIT_FAILURE); + } + + //设置非阻塞模式 + // if (setnonblocking(listen_fd) == -1) + // { + // printf("Setnonblocking Error : %d\n", errno); + // exit(EXIT_FAILURE); + // } + + //绑定 + if (bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) == -1) + { + printf("Bind Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //监听 + if (listen(listen_fd, LISTENQ) == -1) + { + printf("Listen Error : %d\n", errno); + exit(EXIT_FAILURE); + } + cout << "listen init" << endl; + return listen_fd; +} + +int main() +{ + cout << "run.." << endl; + + int listenfd = socket_bind(PORT); + + struct epoll_event events[EPOLLEVENTS]; + + //创建一个描述符 + int epollfd = epoll_create(FDSIZE); + + //添加监听描述符事件 + add_event(epollfd, listenfd, EPOLLIN); + + //循环等待 + for (;;) + { + //该函数返回已经准备好的描述符事件数目 + int ret = epoll_wait(epollfd, events, EPOLLEVENTS, -1); + //处理接收到的连接 + handle_events(epollfd, events, ret, listenfd, buf); + } +} \ No newline at end of file From 82f652780d5538a2ce3ae95d79ee19ff363f592a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Sun, 7 Jul 2019 13:26:17 +0800 Subject: [PATCH 18/19] add --- .../epoll/epoll_test_event.cpp" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" index 33233f3..3e0ccdf 100644 --- "a/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" +++ "b/---Others---/\347\275\221\347\273\234\347\274\226\347\250\213\345\237\272\346\234\254\347\232\204api/epoll/epoll_test_event.cpp" @@ -223,4 +223,6 @@ int main() //处理接收到的连接 handle_events(epollfd, events, ret, listenfd, buf); } -} \ No newline at end of file +} + +// from https://segmentfault.com/a/1190000003063859 \ No newline at end of file From e005b313fc36f907810b59fa954894eb9c81cd93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=94=BF?= Date: Tue, 25 Feb 2020 11:48:48 +0800 Subject: [PATCH 19/19] fix topk bug --- "\345\277\253\346\216\222/quicksort.cpp" | 28 ++++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git "a/\345\277\253\346\216\222/quicksort.cpp" "b/\345\277\253\346\216\222/quicksort.cpp" index 94c189e..69ea115 100644 --- "a/\345\277\253\346\216\222/quicksort.cpp" +++ "b/\345\277\253\346\216\222/quicksort.cpp" @@ -43,17 +43,21 @@ void topk(vector & vi,int k) int end = vi.size() - 1; int index = get_partition(vi, begin, end); - while (index != k - 1) - { - if (index < k - 1) - { - index = get_partition(vi, index + 1, end); - } - else - { - index = get_partition(vi, begin, index-1); - } - } + while (index != k-1) + { + if (index < k-1) + { + begin = index + 1; + end = end; + index = get_partition(nums, begin, end); + } + else + { + begin = begin; + end = index - 1; + index = get_partition(nums, begin, end); + } + } cout << vi[index] << endl; } @@ -75,4 +79,4 @@ int main() topk(vi, 4); topk(vi, 5); return 0; -} \ No newline at end of file +}