From 2b034021539fb04c70c348e0653b2f59da659c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=A9=99OvO?= <976719164@qq.com> Date: Tue, 26 Dec 2023 13:15:55 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\351\242\221\345\205\203\347\264\240.cpp" | 22 ++ ...2\345\272\217\347\256\227\346\263\225.cpp" | 23 ++ ...6\345\255\227\346\216\222\345\272\217.cpp" | 243 ++++++++++++ ...2\345\272\217\347\256\227\346\263\225.cpp" | 73 ++++ ...345\272\217\346\225\260\347\273\204II.cpp" | 35 ++ ...2\345\272\217\351\223\276\350\241\250.cpp" | 22 ++ ...\347\232\204k\344\270\252\346\225\260.cpp" | 84 ++++ ...\347\232\204k\344\270\252\347\202\271.cpp" | 7 + ...7\345\255\227\347\254\246\344\270\262.cpp" | 34 ++ ...0\347\237\255\350\267\257\345\276\204.cpp" | 64 ++++ ...5\344\272\214\345\210\206\345\233\276.cpp" | 40 ++ ...0\347\232\204\345\255\230\345\202\250.cpp" | 154 ++++++++ ...4\346\211\247\346\263\225\345\256\230.cpp" | 29 ++ ...4\350\267\257\351\227\256\351\242\230.cpp" | 60 +++ .../\350\257\276\347\250\213\350\241\250.cpp" | 62 +++ ...0\345\260\217\350\264\271\347\224\250.cpp" | 59 +++ ...0\347\237\255\350\267\257\345\276\204.cpp" | 243 ++++++++++++ ...4\345\210\206\346\237\245\346\211\276.cpp" | 35 ++ ...4\345\205\261\347\245\226\345\205\210.cpp" | 43 +++ ...7\346\234\272\345\205\203\347\264\240.cpp" | 44 +++ ...6\347\232\204\347\256\227\346\263\225.cpp" | 43 +++ ...4\344\270\255\344\275\215\346\225\260.cpp" | 34 ++ ...1\344\272\214\345\217\211\346\240\221.cpp" | 358 ++++++++++++++++++ ...0\345\244\247\345\205\203\347\264\240.cpp" | 15 + ...1\345\244\247\347\232\204\346\225\260.cpp" | 23 ++ ...0\347\232\204\346\254\241\346\225\260.cpp" | 75 ++++ ...0\346\215\256\347\273\223\346\236\204.cpp" | 24 ++ 27 files changed, 1948 insertions(+) create mode 100644 "2209040048/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" create mode 100644 "2209040048/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" create mode 100644 "2209040048/chapter10/\345\256\236\347\216\260\345\255\246\347\224\237\344\277\241\346\201\257\347\232\204\345\244\232\345\205\263\351\224\256\345\255\227\346\216\222\345\272\217.cpp" create mode 100644 "2209040048/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.cpp" create mode 100644 "2209040048/chapter10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.cpp" create mode 100644 "2209040048/chapter10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" create mode 100644 "2209040048/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" create mode 100644 "2209040048/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\347\202\271.cpp" create mode 100644 "2209040048/chapter10/\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" create mode 100644 "2209040048/chapter8/\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" create mode 100644 "2209040048/chapter8/\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" create mode 100644 "2209040048/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\347\232\204\345\255\230\345\202\250.cpp" create mode 100644 "2209040048/chapter8/\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\211\247\346\263\225\345\256\230.cpp" create mode 100644 "2209040048/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" create mode 100644 "2209040048/chapter8/\350\257\276\347\250\213\350\241\250.cpp" create mode 100644 "2209040048/chapter8/\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" create mode 100644 "2209040048/chapter8/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" create mode 100644 "2209040048/chapter9/\344\272\214\345\210\206\346\237\245\346\211\276.cpp" create mode 100644 "2209040048/chapter9/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.cpp" create mode 100644 "2209040048/chapter9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" create mode 100644 "2209040048/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" create mode 100644 "2209040048/chapter9/\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.cpp" create mode 100644 "2209040048/chapter9/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" create mode 100644 "2209040048/chapter9/\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" create mode 100644 "2209040048/chapter9/\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" create mode 100644 "2209040048/chapter9/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" create mode 100644 "2209040048/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" diff --git "a/2209040048/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" "b/2209040048/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..543b3d8f --- /dev/null +++ "b/2209040048/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + if(nums.size() == 1) return nums; + unordered_map times; + int maxcount = 0; + for(auto& it: nums) maxcount = max(maxcount, ++times[it]); + + vector> bucket(maxcount+1); + for(auto& it: times) bucket[it.second].push_back(it.first); + + vector ans; + while(k > 0){ + if(!bucket[maxcount].empty()){ + k -= bucket[maxcount].size(); + ans.insert(ans.end(), bucket[maxcount].begin(), bucket[maxcount].end()); + } + maxcount--; + } + return ans; + } +}; diff --git "a/2209040048/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" "b/2209040048/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" new file mode 100644 index 00000000..5a4eabf7 --- /dev/null +++ "b/2209040048/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" @@ -0,0 +1,23 @@ +#include +int main ( ) +{ + //冒泡排序 + int a[]={3,2,6,4,8,9,1,0,3,5,7,1}; + int len=sizeof(a)/sizeof(int);//求出数组中元素的个数 +// printf("%d\n",len); + int i=0,j; + for (; ia[j+1]) {//实现元素的交换 + int temp = a[j]; + a[j] = a[j+1]; + a[j+1] = temp; + } + } + } + + for (i=0; i +#include +#include + +#define MAXR 10 +#define MAX_SIZE 10 + +typedef struct node +{ + char xm[10]; // 姓名 + char xb; // 性别 m:男 f:女 + char bh[6]; // 班号 + struct node *next; // 指向下一个结点的指针 +}stud_node; // 学生单链表结点类型 + +typedef struct +{ + char xm[10]; // 姓名 + char xb; // 性别 m:男 f:女 + char bh[6]; // 班号 +}stud_type; // 学生记录类型 + +/** +* 功能: +* 由学生记录数组students创建单链表p +* +*/ +static void create_link(stud_node *&p, stud_type students[], int n) // 指针的引用 +{ + int i; + stud_node *s, *t; + + p = NULL; + for(i = 0; i < n; i++) + { + s = (stud_node *)malloc(sizeof(stud_node)); // 动态分配存储空间(新创建的结点) + strcpy(s->xm, students[i].xm); + s->xb = students[i].xb; + strcpy(s->bh, students[i].bh); + if(p == NULL) + { + p = s; // p和t指向新创建的结点 + t = s; + } + else + { + t->next = s; // 采用尾插法建立单链表 + t = s; // t指向新创建的结点 + } + } + t->next = NULL; // 尾结点的next域置为NULL +} + +/*------------输出学生单链表p--------------*/ +static void disp_link(stud_node *p) +{ + int i = 0; + + while(p != NULL) + { + printf(" %s(%s,%c)", p->xm, p->bh, p->xb); + p = p->next; + if((i + 1) % 5 == 0) // 显示5个数据为一行 + printf("\n"); + i++; + } + printf("\n"); +} + +/*------------销毁学生单链表p--------------*/ +static void destroy_link(stud_node *p) +{ + stud_node *pre = p, *q = pre->next; + + while(q != NULL) + { + free(pre); + pre = q; + q = q->next; + } + free(pre); +} + +/** +* 功能: +* 对性别进行排序,只需进行一趟 +* @param: p为待排序序列链表指针 +* @param: r为基数 +* @param: d为关键字位数 +*/ +static void sort_by_xb(stud_node *&p, int r, int d) // 指针的引用 +{ + stud_node *head[MAXR], *tail[MAXR], *t; // 定义各链队的首尾指针 + int j; // 循环变量 + int k; // 区分链队 女:k=0,男:k=1 + + printf("按性别排序\n"); + for(j = 0; j < r; j++) // 初始化各链队首、尾指针 + { + head[j] = tail[j] = NULL; + } + + while(p != NULL) // 对于原链表中每个结点循环 + { + if(p->xb == 'f') // 找第k个链队 + { + k = 0; + } + else + { + k = 1; + } + + if(head[k] == NULL) // 进行分配,即采用尾插法建立单链表 + { + head[k] = p; + tail[k] = p; + } + else + { + tail[k]->next = p; + tail[k] = p; + } + p = p->next; // 取下一个待排序的元素 + } + + p = NULL; + for(j = 0; j < r; j++) // 对每一个链队循环 + { + if(head[j] != NULL) + { + if(p == NULL) + { + p = head[j]; + t = head[j]; + } + else + { + t->next = head[j]; + t = tail[j]; + } + } + } + t->next = NULL; // 最后一个结点的next域置为NULL + disp_link(p); // 输出单链表 +} + +/** +* 功能: +* 对班号进行排序 +* @param: p为待排序序列链表指针 +* @param: r为基数 +* @param: d为关键字位数 +*/ +static void sort_by_bh(stud_node *&p, int r, int d) // 指针的引用 +{ + stud_node *head[MAXR], *tail[MAXR], *t; // 定义各链队的首尾指针 + int i, j; // 循环变量 + int k; // k区分链队 + + printf("按班号排序\n"); + for(i = 3; i >= 2; i--) // 从低位到高位做d趟排序 + { + for(j = 0; j < r; j++) // 初始化各链队首尾指针 + { + head[j] = tail[j] = NULL; + } + while(p != NULL) // 对于原链表中每个结点循环 + { + k = p->bh[i] - '0'; // 找第k个链队 + if(head[k] == NULL) // 进行分配,即采用尾插法建立单链表 + { + head[k] = p; + tail[k] = p; + } + else + { + tail[k]->next = p; + tail[k] = p; + } + + p = p->next; // 取下一个待排序的元素 + } + p = NULL; + for(j = 0; j < r; j++) // 对每一个链队循环 + { + if(head[j] != NULL) + { + if(p == NULL) + { + p = head[j]; + t = tail[j]; + } + else + { + t->next = head[j]; + t = tail[j]; + } + } + } + t->next = NULL; // 最后一个结点的next域置为NULL + printf(" 第%d趟:\n", d - i + 2); + disp_link(p); // 输出单链表 + } +} + +/** +* 功能: +* 按班号和性别排序 +* 在班号和性别中,班号优先,性别次之,所以先按 +* 性别排序,然后再按班号排序。由于班号4位中只有后 +* 两位不同,故d=2,从低位到高位排序。 +*/ +static void sort_stus(stud_type students[], int n) +{ + stud_node *p; + + create_link(p, students, n); + printf("排序前:\n"); + disp_link(p); + sort_by_xb(p, 2, 1); + sort_by_bh(p, 10, 2); + printf("排序后:\n"); + disp_link(p); + destroy_link(p); +} + +int main(void) +{ + int n = 10; + + stud_type students[MAX_SIZE] = { + {"王华", 'm', "1003"}, {"陈兵", 'm', "1020"}, + {"许可", 'f', "1022"}, {"李英", 'f', "1003"}, + {"张冠", 'm', "1021"}, {"陈强", 'm', "1002"}, + {"李真", 'f', "1002"}, {"章华", 'm', "1001"}, + {"刘丽", 'f', "1021"}, {"王强", 'm', "1022"}, + }; + + sort_stus(students, n); + + return 0; +} diff --git "a/2209040048/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.cpp" "b/2209040048/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.cpp" new file mode 100644 index 00000000..b2059eab --- /dev/null +++ "b/2209040048/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.cpp" @@ -0,0 +1,73 @@ +#include +#include +#include + +int partion(char *s,int start,int end) +{ + //随机选取分割值 + int key = s[start]; + int temp; + while(1) + { + while(s[start] < key && (start < end) ) + { + //从左边开始遍历,当小于分割值,则留在左边 + start++; + } + + while(s[end] > key && (end > start)) + { + //从右边开始遍历,当大于分割值,则留在右边 + end--; + } + if(start >= end) + { + //分割值的位置已确定,左边的都小于分隔值,右边的都大于或等于分隔值 + break; + } + else + { + //找到本该在右边的start元素值,和本该在左边的end元素值,对他们进行交换 + temp = s[start]; + s[start] = s[end]; + s[end] = temp; + } + + if(s[end] == s[start]) + { + //当左右两边的元素相当,则不用交换位置,将左边的遍历的位置向右边移动开始新一轮的遍历 + start++; + } + } + //此时start=end,为分隔值所在的位置 + return start; +} + +int my_qsort(char *s,int start,int end) +{ + if(start < end) + { + int k = partion(s,start,end); + my_qsort(s,start,k-1); //继续排序分割值的左边 + my_qsort(s,k+1,end); //继续排序分割值的右边 + } +} + +int main(int argc,char *argv[]){ + char *str; + int num=100; + if(argv[1]!=NULL) + num = atoi(argv[1]); + printf("num:%d\n",num); + //随机生成长度为num的字符串 + str = malloc(num); + for(int i = 0;i < num;i++ ) + { + str[i] = rand()%10 + '0'; + } + printf("src str:%s,len:%d.\n",str,strlen(str)); + my_qsort(str,0,strlen(str)-1); + printf("sort str:%s.\n",str); + free(str); + return 0; +} \ No newline at end of file diff --git "a/2209040048/chapter10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.cpp" "b/2209040048/chapter10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.cpp" new file mode 100644 index 00000000..a3f4b362 --- /dev/null +++ "b/2209040048/chapter10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.cpp" @@ -0,0 +1,35 @@ +#include +#include +#include + +int* sortArrayByParityII(int* A, int ASize, int* returnSize) +{ + /*定义结果数组*/ + int *res = (int*)malloc(ASize * sizeof(int)); + /*定义偶数下标和奇数下标*/ + int oddIndex = 0; + int evenIndex = 1; + /*遍历A数组每一个元素,如果是偶数,那么就放到res数组的偶数位置,oddIndex相应+2,如果是奇数,就放到res数组奇数位置,eventIndex相应+2*/ + for (int i = 0; i < ASize; i++) + { + if (A[i] % 2 == 0) + { + res[oddIndex] = A[i]; + oddIndex += 2; + } + else + { + res[evenIndex] = A[i]; + evenIndex += 2; + } + } + /*返回结果*/ + *returnSize = ASize; + return res; +} + +/*主函数省略*/ +int main(void) +{ + +} diff --git "a/2209040048/chapter10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" "b/2209040048/chapter10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 00000000..9961e17f --- /dev/null +++ "b/2209040048/chapter10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + ListNode* sortList(ListNode* head) { + if(!head) return head; + vector a; + auto p = head; + while(p != NULL){ + a.push_back(p->val); + p = p->next; + } + delete p; + sort(a.begin(), a.end()); + auto q = head; + for(const auto &c : a){ + q->val = c; + q = q->next; + } + delete q; + + return head; + } +}; diff --git "a/2209040048/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" "b/2209040048/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" new file mode 100644 index 00000000..77a0babd --- /dev/null +++ "b/2209040048/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" @@ -0,0 +1,84 @@ +void swap(int k[], int i, int j) +{ + int temp; + + temp = k[i]; + k[i] = k[j]; + k[j] = temp; +} + +/* +@k:数组首地址 +@s:双亲 +@n:待构造的节点个数 +*/ +void HeapAdjust(int k[], int s, int n) +{ + int i, temp; + + temp = k[s];//固定住双亲节点 + + for( i=2*s; i <= n; i*=2 )//2*s 表示左孩子 , 2*s + 1 表示右孩子 + { + if( i < n && k[i] < k[i+1] ) + { + i++;//指向右孩子 + } + + if( temp >= k[i] )//若双亲大于左孩子或右孩子,则退出循环,往根节点方向继续构造 + { + break; + } + + k[s] = k[i];//若双亲小于左孩子或右孩子,则双亲替换为左孩子或右孩子,此时的双亲 + s = i;//并且把双亲的下标也替换掉。 + } + + k[s] = temp;//把开始保存的双亲替换掉左孩子或右孩子。 +} + +/*从下往上,从左到右构造大根堆*/ +void HeapSort(int k[], int n) +{ + int i; + + /*第一次构造大根堆*/ + for( i=n/2; i > 0; i-- )// n/2 表示拿到倒数第二层左节点的下标 + { + HeapAdjust(k, i, n);//构造大根堆 + } + + /*根节点和第(n-i)个节点交换,并将 (n-i)个节点重新构造大根堆*/ + for( i=n; i > 1; i-- ) + { + swap(k, 1, i);//根节点和第(n-i)个节点交换 + HeapAdjust(k, 1, i-1);//将 (n-i)个节点重新构造大根堆 + } +} + + + +int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize){ + + int i = 0; + int *ans; + + if(arr == NULL) + { + *returnSize = 0; + return NULL; + } + + ans = (int *)malloc(sizeof(int)*(arrSize+1)); + ans[0] = -1; + for(i = 1; i <= arrSize; i++) + { + ans[i] = arr[i-1]; + } + + HeapSort(ans,arrSize); + + *returnSize = k; + + return &ans[1]; +} diff --git "a/2209040048/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\347\202\271.cpp" "b/2209040048/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\347\202\271.cpp" new file mode 100644 index 00000000..b9fadf79 --- /dev/null +++ "b/2209040048/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\347\202\271.cpp" @@ -0,0 +1,7 @@ +class Solution { + public int[][] kClosest(int[][] points, int K) { + Arrays.sort(points, (a, b) -> ((a[0]*a[0]+a[1]*a[1]) - (b[0]*b[0]+b[1]*b[1]))); + return Arrays.copyOfRange(points, 0, K); + } +} + diff --git "a/2209040048/chapter10/\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" "b/2209040048/chapter10/\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" new file mode 100644 index 00000000..ea29dd29 --- /dev/null +++ "b/2209040048/chapter10/\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" @@ -0,0 +1,34 @@ +#include +#include +#define N 5 +int main() +{ + char s[N][80]; + char t[80]; + int i,j; + //输入字符串 + for(i=0;i0) + { + //互换 + strcpy(t,s[j]);//后面的拷给前面的 + strcpy(s[j],s[j+1]); + strcpy(s[j+1],t); + } + } + } + printf("After sorting:\n"); + for(i=0;i +#include +#include +#include +#include +#include "Solution1091.h" + +using namespace std; +vector> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; +int searchGrid(vector> &from, vector> &grid, int step) +{ + vector> to; + for (vector node : from) + { + int y = node[0]; + int x = node[1]; + for (vector direction : directions) + { + int newX = x + direction[1]; + int newY = y + direction[0]; + if (newX < 0 || newY < 0 || newX >= grid[0].size() || newY >= grid.size()) + { + continue; + } + if (grid[newY][newX] != 0) + { + continue; + } + if (newY == 0 && newX == 0) + { + return step; + } + grid[newY][newX] = step; + to.push_back(vector{newY, newX}); + } + } + if (to.size() == 0) + { + return 1; + } + return searchGrid(to, grid, step - 1); +} + +int Solution1091::shortestPathBinaryMatrix(vector> &grid) +{ + if (grid[0][0] == 1 || grid[grid.size() - 1][grid[0].size() - 1] == 1) + { + return -1; + } + if (grid.size() == 1 && grid[0].size() == 1) + { + return 1; + } + vector> from; + int gridSizeY = grid.size() - 1; + int gridSizeX = grid[0].size() - 1; + from.push_back(vector{gridSizeY, gridSizeX}); + int i = searchGrid(from, grid, -1); + if (i == 1) + { + return -1; + } + return i * -1 + 1; +} \ No newline at end of file diff --git "a/2209040048/chapter8/\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" "b/2209040048/chapter8/\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" new file mode 100644 index 00000000..35fe0c82 --- /dev/null +++ "b/2209040048/chapter8/\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" @@ -0,0 +1,40 @@ +vector visited; + bool ok = true; + void dfs(vector>& graph, int thisnode, int lastnode) + { + if (lastnode == -1) + { + + } + else + { + if (visited[thisnode] == -1) + { + visited[thisnode] = visited[lastnode] ^ 1; + } + else + { + if (visited[thisnode] ^ 1 != visited[lastnode]) + { + ok = false; + } + return; + } + } + for (int i = 0; i < graph[thisnode].size(); i++) + { + dfs(graph, graph[thisnode][i], thisnode); + } + } + bool isBipartite(vector>& graph) { + visited.resize(graph.size(), -1); + for (int i = 0; i < graph.size(); i++) + { + if (visited[i] == -1) + { + visited[i] = 0; + dfs(graph, i, -1); + } + } + return ok; + } diff --git "a/2209040048/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\347\232\204\345\255\230\345\202\250.cpp" "b/2209040048/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\347\232\204\345\255\230\345\202\250.cpp" new file mode 100644 index 00000000..375e90f8 --- /dev/null +++ "b/2209040048/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\347\232\204\345\255\230\345\202\250.cpp" @@ -0,0 +1,154 @@ +#include "stdio.h" +#include "malloc.h" +#define INF 32767 +#define MAXV 100 + +typedef char InfoType; + +typedef struct { + int no; + InfoType info; +} VertexType; + +typedef struct { + int edges[MAXV][MAXV]; + int n, e; + VertexType vexs[MAXV]; +} MatGraph; + +typedef struct ANode { + int adjvex; + struct ANode * nextarc; + int weight; +} ArcNode; + +typedef struct Vnode { + InfoType info; + int count; + ArcNode * firstarc; +} VNode; + +typedef struct { + VNode adjlist[MAXV]; + int n, e; +} AdjGraph; + +/* + * -----------------------邻接矩阵的基本运算算法------------------------ + * 创建图的邻接矩阵 + */ +void CreateMat( MatGraph &g, int A[MAXV][MAXV], int n, int e ) +{ + int i, j; + g.n = n; + g.e = e; + for ( i = 0; i < g.n; i++ ) + for ( j = 0; j < g.n; j++ ) + g.edges[i][j] = A[i][j]; +} + + +/* 输出邻接矩阵g */ +void DispMat( MatGraph g ) +{ + int i, j; + for ( i = 0; i < g.n; i++ ) + { + for ( j = 0; j < g.n; j++ ) + if ( g.edges[i][j] != INF ) + printf( "%4d", g.edges[i][j] ); + else + printf( "%4s", "∞" ); + printf( "\n" ); + } +} + + +/* + * -----------------------邻接表的基本运算算法------------------------ + * 创建图的邻接表 + */ +void CreateAdj( AdjGraph * & G, int A[MAXV][MAXV], int n, int e ) +{ + int i, j; + ArcNode *p; + G = (AdjGraph *) malloc( sizeof(AdjGraph) ); + for ( i = 0; i < n; i++ ) + G->adjlist[i].firstarc = NULL; + for ( i = 0; i < n; i++ ) + for ( j = n - 1; j >= 0; j-- ) + if ( A[i][j] != 0 && A[i][j] != INF ) + { + p = (ArcNode *) malloc( sizeof(ArcNode) ); + p->adjvex = j; + p->weight = A[i][j]; + p->nextarc = G->adjlist[i].firstarc; + G->adjlist[i].firstarc = p; + } + G->n = n; + G->e = e; +} + + +/* 输出临界表G */ +void DispAdj( AdjGraph *G ) +{ + ArcNode *p; + for ( int i = 0; i < G->n; i++ ) + { + p = G->adjlist[i].firstarc; + printf( "%3d:", i ); + while ( p != NULL ) + { + printf( "%3d[%d]→", p->adjvex, p->weight ); + p = p->nextarc; + } + printf( "∧\n" ); + } +} + + +/* 销毁图的邻接表 */ +void DestroyAdj( AdjGraph * &G ) +{ + ArcNode *pre, *p; + for ( int i = 0; i < G->n; i++ ) + { + pre = G->adjlist[i].firstarc; + if ( pre != NULL ) + { + p = pre->nextarc; + while ( p != NULL ) + { + free( pre ); + pre = p; + p = p->nextarc; + } + free( pre ); + } + } + free( G ); +} + + +/* --------------main----------------------------- */ +int main() +{ + MatGraph g; + AdjGraph *G; + int A[MAXV][MAXV] = { + { 0, 5, INF, 7, INF, INF }, { INF, 0, 4, INF, INF, INF }, + { 8, INF, 0, INF, INF, 9 }, { INF, INF, 5, 0, INF, 6 }, + { INF, INF, INF, 5, 0, INF }, { 3, INF, INF, INF, 1, 0 } + }; + int n = 6, e = 10; + CreateMat( g, A, n, e ); + printf( "(1)图G的邻接表:\n" ); + DispMat( g ); + CreateAdj( G, A, n, e ); + printf( "(2)图G的邻接表:\n" ); + DispAdj( G ); + printf( "(3)销毁图G的邻接表\n" ); + DestroyAdj( G ); + return(1); +} \ No newline at end of file diff --git "a/2209040048/chapter8/\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\211\247\346\263\225\345\256\230.cpp" "b/2209040048/chapter8/\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\211\247\346\263\225\345\256\230.cpp" new file mode 100644 index 00000000..1923e7a5 --- /dev/null +++ "b/2209040048/chapter8/\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\211\247\346\263\225\345\256\230.cpp" @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; + +int findJudge(int n, vector>& trust) { + vector indegree(n); + vector outdegree(n); + for (int i = 0; i < trust.size(); i++) + { + outdegree[trust[i][0] -1]++; + indegree[trust[i][1] -1]++; + } + for (int i = 0; i < n; i++) + { + if (outdegree[i] == 0 && indegree[i] == n - 1) + { + return i + 1; + } + } + return -1; +} +int main() { + vector> trust{ {1,3},{2,3} }; + int n = 3; + int res = findJudge(3, trust); + cout << res << endl; + return 0; +} diff --git "a/2209040048/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" "b/2209040048/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" new file mode 100644 index 00000000..9c90d086 --- /dev/null +++ "b/2209040048/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" @@ -0,0 +1,60 @@ +#include +#define INF 1e7 +int map[105][105]; +void Prim(int m,int c) +{ + int dist[m+1]; + int flag[m+1]; + for(int i=1;i<=m;i++){ + dist[i]=INF; + flag[i]=0; + if(map[1][i]h)){ + map[v1][v2]=h; + map[v2][v1]=h; + } + + } + Prim(m,c); + } +} diff --git "a/2209040048/chapter8/\350\257\276\347\250\213\350\241\250.cpp" "b/2209040048/chapter8/\350\257\276\347\250\213\350\241\250.cpp" new file mode 100644 index 00000000..94a58aea --- /dev/null +++ "b/2209040048/chapter8/\350\257\276\347\250\213\350\241\250.cpp" @@ -0,0 +1,62 @@ +class Solution: + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + from collections import deque + + # 将边缘列表表示的先决条件转化为 邻接表 + adjacency = [[] for _ in range(numCourses)] + # 定义列表统计图中每个节点的入度情况 + indegree = [0] * numCourses + + for info in prerequisites: + adjacency[info[1]].append(info[0]) + indegree[info[0]] += 1 + + queue = deque() + + + for i in range(len(indegree)): + if not indegree[i]: + queue.append(i) + + while queue: + u = queue.popleft() + numCourses -= 1 + # 搜索邻接节点 + for v in adjacency[u]: + # 将邻接节点入度减 1 + indegree[v] -= 1 + # 如果入度为 0,入队 + if indegree[v] == 0: + queue.append(v) + + return numCourses == 0 + +class Solution: + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + def dfs(adjacency, sign, i): + if sign[i] == -1: + return True + if sign[i] == 1: + return False + + sign[i] = 1 + + for j in adjacency[i]: + if not dfs(adjacency, sign, j): + return False + sign[i] = -1 + return True + + + sign = [0] * numCourses + + adjacency = [[] for _ in range(numCourses)] + for info in prerequisites: + adjacency[info[1]].append(info[0]) + + + for i in range(numCourses): + if not dfs(adjacency, sign, i): + return False + return True + diff --git "a/2209040048/chapter8/\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" "b/2209040048/chapter8/\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" new file mode 100644 index 00000000..72022f5d --- /dev/null +++ "b/2209040048/chapter8/\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.cpp" @@ -0,0 +1,59 @@ +class Solution { + public int minCostConnectPoints(int[][] points) { + + int[][] grid; + int rest = 0; + int INF = Integer.MAX_VALUE; + int counts = points.length; + grid = new int[counts][counts]; + for(int i = 0; i < counts; i++){ + for(int j = 0; j < counts; j++){ + grid[i][j] = grid[j][i] = i == j ? 0 : INF; + } + } + for(int i = 0; i < counts; i++){ + + for(int j = 0; j < counts; j++){ + if(j == i) continue; + int val = Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]); + grid[i][j] = val; + } + } + + boolean selected[] = new boolean[counts]; + int[] distence = new int[counts]; + int[] parent = new int[counts]; + Arrays.fill(distence,INF ); + Arrays.fill(parent, -1); + selected[0] = true; + int pre = 0; + for(int i = 1; i < counts; i++){ + + for(int j = 0; j < counts; j++){ + if(!selected[j]){ + if(distence[j] > grid[pre][j]){ + distence[j] = grid[pre][j]; + parent[j] = pre; + } + } + } + + int min = INF; + for(int k = 0; k < counts; k++){ + if(!selected[k]){ + if(distence[k] < min && distence[k] != INF){ + min = distence[k]; + pre = k; + } + } + } + + selected[pre] = true; + } + + for(int i = 1; i < counts; i++){ + rest += distence[i]; + } + return rest; + } +} diff --git "a/2209040048/chapter8/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" "b/2209040048/chapter8/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" new file mode 100644 index 00000000..72229f99 --- /dev/null +++ "b/2209040048/chapter8/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" @@ -0,0 +1,243 @@ +#include +#include + +#define INF 32767 //定义∞ +#define MAXV 100 //最大顶点个数 + +typedef char InfoType; +/*-------------------------以下定义邻接矩阵类型---------------------------*/ +typedef struct +{ + int no; //顶点编号 + InfoType info; //顶点信息 +}VertexType; //顶点类型 + +typedef struct +{ + int edges[MAXV][MAXV]; //邻接矩阵数组(用一个二维数组存放顶点间关系(边或弧)的数据) + int n; //顶点数 + int e; //边数 + VertexType vexs[MAXV]; //存放顶点信息(用一个一维数组存放图中所有顶点数据) +}MatGraph; //完整的图邻接矩阵类型 + +//邻接表表示法-将每个顶点的邻接点串成一个单链表 +/*-----------以下定义邻接表类型--------------*/ +typedef struct ArcNode +{ + int adjvex; //该边的邻接点编号 + struct ArcNode *nextarc; //指向下一条边的指针 + int weight; //该边的相关信息,如权值(用整型表示) +}ArcNode; //边结点类型 + +typedef struct VNode +{ + InfoType info; //顶点其他信息 + int cnt; //存放顶点入度,仅用于拓扑排序 + ArcNode *firstarc; //指向第一条边 +}VNode; //邻接表结点类型 + +typedef struct +{ + VNode adjlist[MAXV]; //邻接表头结点数组 + int n; //图中顶点数 + int e; //图中边数 +}AdjGraph; //完整的图邻接表类型 + +/*-------------------------邻接矩阵的基本运算算法---------------------------*/ +/*------------由边数组A、顶点数n和边数e创建图的邻接矩阵g--------------------*/ +void CreateMat(MatGraph &g, int A[MAXV][MAXV], int n, int e) +{ + int i, j; + + g.n = n; + g.e = e; + for(i = 0; i < g.n; i++) + for(j = 0; j < g.n; j++) + g.edges[i][j] = A[i][j]; +} + +/*------------输出邻接矩阵g--------------------*/ +void DispMat(MatGraph g) +{ + int i, j; + + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + if(g.edges[i][j] != INF) + printf("%4d", g.edges[i][j]); + else + printf("%4s", "∞"); + } + printf("\n"); + } +} + +/*-------------------------邻接表的基本运算算法---------------------------*/ +/*-------------------由边数组A、顶点数n和边数e创建图的邻接表G--------------------*/ +void CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e) +{ + int i, j; + ArcNode *p; + + G = (AdjGraph *)malloc(sizeof(AdjGraph)); + for(i = 0; i < n; i++) //给邻接表中所有头结点的指针域置初值NULL + { + G->adjlist[i].firstarc = NULL; + } + + for(i = 0; i < n; i++) //检查邻接矩阵中的每个元素 + { + for(j = n - 1; j >= 0; j--) + { + if(A[i][j] != 0 && A[i][j] != INF) //存在一条边 + { + p = (ArcNode *)malloc(sizeof(ArcNode)); //创建一个结点p + p->adjvex = j; //邻接点编号 + p->weight = A[i][j]; //边的权重 + p->nextarc = G->adjlist[i].firstarc; //采用头插法插入结点p + G->adjlist[i].firstarc = p; + } + } + } + G->n = n; + G->e = e; +} + +/*-------------------输出邻接表G--------------------*/ +void DispAdj(AdjGraph *G) +{ + ArcNode *p; + + for(int i = 0; i < G->n; i++) + { + p = G->adjlist[i].firstarc; + printf("顶点%d: ", i); + while(p != NULL) + { + printf("%3d[%d]->", p->adjvex, p->weight); //邻接点编号[权重] + p = p->nextarc; + } + printf("∧\n"); + } +} + +/*-------------------销毁图的邻接表G--------------------*/ +void DestroyAdj(AdjGraph *&G) +{ + ArcNode *pre, *p; + + for(int i = 0; i < G->n; i++) + { + pre = G->adjlist[i].firstarc; //pre指向第i个单链表的首结点 + if(pre != NULL) + { + p = pre->nextarc; + while(p != NULL) //释放第i个单链表的所有边结点 + { + free(pre); + pre = p; + p = p->nextarc; + } + free(pre); + } + } + free(G); //释放头结点数组 +} + +/*---------------输出图g中所有两个顶点之间的最短路径和最短路径长度-------------*/ +static void Dispath(MatGraph g, int A[][MAXV], int path[][MAXV]) +{ + int i, j, k, s; + int apath[MAXV], d; //存放一条最短路径中间顶点(反向)及其顶点个数 + + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + if((i != j) && (A[i][j] != INF)) //若顶点i和j之间存在路径 + { + printf(" 从%d到%d的路径为:", i, j); + k = path[i][j]; + d = 0; apath[d] = j; //路径上添加终点 + while((k != -1) && (k != i)) //路径上添加中间点 + { + d++; + apath[d] = k; + k = path[i][k]; + } + d++; apath[d] = i; //路径上添加起点 + printf("%d", apath[d]); //输出起点 + for(s = d - 1; s >= 0; s--) //输出路径上的中间顶点 + printf(",%d", apath[s]); + printf(" \t路径长度为:%d\n", A[i][j]); + } + } + } +} + + +/*---------------求图g中所有两个顶点之间的最短路径长度和最短路径---------------*/ +static void Floyd(MatGraph g) +{ + int A[MAXV][MAXV], path[MAXV][MAXV]; + int i, j, k; + + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + A[i][j] = g.edges[i][j]; + if((i != j) && (g.edges[i][j] < INF)) + { + path[i][j] = i; //顶点i到j有边时 + } + else + { + path[i][j] = -1; //顶点i到j没有边时 + } + } + } + + for(k = 0; k < g.n; k++) //依次考察所有顶点 + { + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + if(A[i][j] > A[i][k] + A[k][j]) + { + A[i][j] = A[i][k] + A[k][j]; //修改最短路径长度 + path[i][j] = path[k][j]; //修改最短路径 + } + } + } + } + + //输出最短路径 + Dispath(g, A, path); +} + +int main(void) +{ + MatGraph g; + int A[MAXV][MAXV] = { + {0, 5, INF, 7, INF, INF}, + {INF, 0, 4, INF, INF, INF}, + {8, INF, 0, INF, INF, 9}, + {INF, INF, 5, 0, INF, 6}, + {INF, INF, INF, 5, 0, INF}, + {3, INF, INF, INF, 1, 0} + }; + int n = 6; //图中的顶点数 + int e = 10; //图中的边数 + + CreateMat(g, A, n, e); //建立图8.1的邻接矩阵 + printf("有向图G的邻接矩阵:\n"); + DispMat(g); + printf("弗洛伊德算法求解结果:\n"); + Floyd(g); + + return 0; +} \ No newline at end of file diff --git "a/2209040048/chapter9/\344\272\214\345\210\206\346\237\245\346\211\276.cpp" "b/2209040048/chapter9/\344\272\214\345\210\206\346\237\245\346\211\276.cpp" new file mode 100644 index 00000000..9cc5e30c --- /dev/null +++ "b/2209040048/chapter9/\344\272\214\345\210\206\346\237\245\346\211\276.cpp" @@ -0,0 +1,35 @@ +#include +#define N 11 + +int main() +{ + int a[N] = {1, 5, 8, 11, 19, 22, 31, 35, 40, 49, 50}; // 准备好一个已经排序好的数组 + int low = 0, high = N - 1, mid, target; + printf("请输入要查找的值:"); + scanf("%d", &target); + printf("%d\n", target); + + // 当左边界还未超过右边界时,进行二分查找 + while(low <= high) + { + mid = (low + high) / 2; // 每次循环重新给mid赋值,改变中间值的下标 + printf("low = %d, high = %d, mid = %d\n", low, high, mid); // 此处打印各个下标值,方便观测下标变化 + // 如果中间值等于目标值,说明查找成功,此时跳出循环 + if(a[mid] == target){ + printf("目标值的下标是%d\n", mid); + break; + } + // 如果中间值大于目标值,说明目标值在左半边,此时改变右边界的下标(缩减右半边) + if(a[mid] > target) + high = mid - 1; + // 如果中间值小于目标值,说明目标值在右半边,此时改变左边界的下标(缩减左半边) + if(a[mid] < target) + low = mid + 1; + } + + // 当左边界已经超过右边界时,说明查找已经结束 + if(low > high) + printf("未找到目标值\n"); + return 0; +} + diff --git "a/2209040048/chapter9/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.cpp" "b/2209040048/chapter9/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.cpp" new file mode 100644 index 00000000..02649853 --- /dev/null +++ "b/2209040048/chapter9/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.cpp" @@ -0,0 +1,43 @@ +class Solution { +public: + bool findpath(TreeNode*cur,TreeNode*x,stack&path) //注意这里要传引用 + { + if(cur==nullptr) + return false; + path.push(cur); + if(cur==x) + return true; + + if(findpath(cur->left,x,path)) + return true; + if(findpath(cur->right,x,path)) + return true; + + path.pop(); + return false; + } + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) + { + stack ppath; + stack qpath; + findpath(root,p,ppath); + findpath(root,q,qpath); + while(ppath.size()>qpath.size()) //使两个栈一样长 + { + ppath.pop(); + } + + while(ppath.size() dict; + List list; + Random rand = new Random(); + /** Initialize your data structure here. */ + public RandomizedSet() { + dict = new HashMap<>(); + list = new ArrayList<>(); + } + + /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ + public boolean insert(int val) { + if(dict.containsKey(val)) return false; + dict.put(val, list.size()); + list.add(list.size(), val); + return true; + } + + /** Removes a value from the set. Returns true if the set contained the specified element. */ + public boolean remove(int val) { + if(!dict.containsKey(val)) return false; + int lastElement = list.get(list.size() - 1); + int idx = dict.get(val); + list.set(idx, lastElement); + dict.put(lastElement, idx); + list.remove(list.size() - 1); + dict.remove(val); + return true; + } + + /** Get a random element from the set. */ + public int getRandom() { + return list.get(rand.nextInt(list.size())); + } +} + +/** + * Your RandomizedSet object will be instantiated and called as such: + * RandomizedSet obj = new RandomizedSet(); + * boolean param_1 = obj.insert(val); + * boolean param_2 = obj.remove(val); + * int param_3 = obj.getRandom(); + */ diff --git "a/2209040048/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" "b/2209040048/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" new file mode 100644 index 00000000..bc20b0ec --- /dev/null +++ "b/2209040048/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" @@ -0,0 +1,43 @@ +#include +#define MAXL 100 //定义表中最多记录个数 +typedef int KeyType; +typedef char InfoType[10]; +typedef struct +{ + KeyType key; //KeyType为关键字的数据类型 + InfoType data; //其他数据 +} NodeType; +typedef NodeType SeqList[MAXL]; //顺序表类型 +int BinSearch(SeqList R,int n,KeyType k) //二分查找算法 +{ + int low=0,high=n-1,mid,count=0; + while (low<=high) + { + mid=(low+high)/2; + printf(" 第%d次比较:在[%d,%d]中比较元素R[%d]:%d\n",++count,low,high,mid,R[mid].key); + if (R[mid].key==k) //查找成功返回 + return mid; + if (R[mid].key>k) //继续在R[low..mid-1]中查找 + high=mid-1; + else + low=mid+1; //继续在R[mid+1..high]中查找 + } + return -1; +} +int main() +{ + SeqList R; + KeyType k=9; + int a[]={1,2,3,4,5,6,7,8,9,10},i,n=10; + for (i=0;i& nums1, vector& nums2) { + //新开辟数组data,记录nums1和nums2合并后的有序数组 + vector data; + int i=0,j=0; + while(i +#include + +typedef struct AVLNode +{ + int data; + int height; + struct AVLNode* left; + struct AVLNode* right; +}Node; +#define HEIGHT(node) ((node == NULL)? 0 : (((Node*)(node))->height )) +#define MAX(a,b) ((a > b) ? (a) : (b)) + +int get_height(Node* node) +{ + return HEIGHT(node); +} + +/*插入左孩子的左子树-右旋*/ +//传入参数为最小失衡结点tree,对tree进行右旋 +Node* left_left(Node* tree) +{ + //结点调整 + Node* k = tree->left;//保存tree的左孩子,k将是最终的父节点 + tree->left = k->right;//将k的右孩子接到tree的左子树 + k->right = tree;//tree作为k的右子树 + + //高度调整(这里指深度:用左右子树来判断) + k->height = MAX(get_height(k->left), get_height(k->right)) + 1; + tree->height = MAX(get_height(tree->left), get_height(tree->right)) + 1; + return k; +} + +/*插入右孩子的右子树-左旋*/ +//传入参数为最小失衡结点tree,对tree进行左旋 +Node* right_right(Node* tree) +{ + //结点调整 + Node* k = tree->right; + tree->right = k->left; + k->left = tree; + + //高度调整 + k->height = MAX(get_height(k->left), get_height(k->right)) + 1; + tree->height = MAX(get_height(tree->left), get_height(tree->right)) + 1; + return k; +} + +/*插入左孩子的右子树-先左旋再右旋*/ +//对tree->left左旋(left_left),对tree右旋(right_right) +Node* left_right(Node* tree) +{ + tree->left = right_right(tree->left); + tree = left_left(tree); + return tree; +} + +/*插入右孩子的左子树-先右旋再左旋*/ +//对tree->right右旋(left_left),对tree左旋 +Node* right_left(Node* tree) +{ + tree->right = left_left(tree->right); + tree = right_right(tree); + return tree; +} + +/*创建一棵树,根结点为node*/ +Node* create(int key) +{ + Node* node = (Node*)malloc(sizeof(Node)); + //此处可判断是否创建成功,我省略了 + node->data = key; + node->left = NULL; + node->right = NULL; + node->height = 0; + return node; +} + +/*往根节点为tree的树中插入一个值key*/ +//插入位置同二叉排序树的逻辑,大于向右找位置,小于向左找位置 +Node* Insert(Node* tree, int key) +{ + //如果为空,就创建一棵树 + if (tree == NULL) + { + Node* node = create(key); + tree = node; + } + //向左子树插入 + else if (key < tree->data) + { + //递归寻找插入位置 + tree->left = Insert(tree->left, key); + //判断是否失衡 + if (get_height(tree->left) - get_height(tree->right) == 2) + { + //判断插入位置在左孩子的左子树还是右子树 + if (key < tree->left->data) + tree = left_left(tree); + else + tree = left_right(tree); + } + } + //向右子树插入 + else if (key > tree->data) + { + tree->right = Insert(tree->right, key); + if (get_height(tree->right) - get_height(tree->left) == 2) + { + if (key > tree->right->data) + tree = right_right(tree); + else + tree = right_left(tree); + } + } + else + printf("不允许插入重复的值\n"); + //重新调整二叉树深度 + tree->height = MAX(get_height(tree->left), get_height(tree->right)) + 1; + return tree; +} + +/*查找结点*/ +Node* search(Node* tree, int key) +{ + if (tree == NULL || tree->data == key) + return tree; + else if (key < tree->data) + search(tree->left, key); + else + search(tree->right, key); +} +/*找到替换结点-左子树的最右边*/ +Node* mininum(Node* tree) +{ + if (tree == NULL) + return NULL; + while (tree->left) + tree = tree->left; + return tree; +} +/*删除结点*/ +Node* del(Node* tree, int key) +{ + //定位到要删除结点 + Node* node = search(tree, key); + if (tree == NULL || node == NULL) + { + printf("删除失败\n"); + return tree; + } + //若删除结点在左子树 + if (key < tree->data) + { + //递归找到要删除结点 + tree->left = del(tree->left, key); + //删除后要检查平衡 + if (get_height(tree->right) - get_height(tree->left) == 2) + { + if (key < tree->right->data) + tree = right_left(tree); + else + tree = right_right(tree); + } + } + //若删除结点在右子树 + else if (key > tree->data) + { + tree->right = del(tree->right, key); + if (get_height(tree->left) - get_height(tree->right) == 2) + { + if (key < tree->left->data) + tree = left_left(tree); + else + tree = left_right(tree); + } + } + //此时就是要删除结点,//待删除结点有左右孩子-同二叉排序树 + else if (tree->left != NULL && tree->right != NULL) + { + Node* min_node = mininum(tree->right); + tree->data = min_node->data; + tree->right = del(tree->right, min_node->data); + } + //只有一个孩子或者没有孩子 + else + tree = tree->left ? tree->left : tree->right; + + if (tree) + tree->height = MAX(get_height(tree->left), get_height(tree->right)) + 1; + return tree; +} + +/*前序遍历*/ +void pre_order(Node* tree) +{ + if (tree) + { + printf("%d ", tree->data); + pre_order(tree->left); + pre_order(tree->right); + } +} + +/*中序遍历*/ +void in_order(Node* tree) +{ + if (tree) + { + in_order(tree->left); + printf("%d ", tree->data); + in_order(tree->right); + } +} + +int main() +{ + //第一种情况-左孩子的左子树 + Node* tree1 = NULL; + int a1[] = { 13, 8, 15, 3, 10}; + int l1 = sizeof(a1) / sizeof(int); + for (int i = 0; i < l1; i++) + { + tree1 = Insert(tree1, a1[i]); + } + printf("第一种情况-左孩子的左子树\n"); + printf("前序遍历:"); + pre_order(tree1); + printf("\n"); + printf("中序遍历:"); + in_order(tree1); + printf("\n"); + printf("根结点的深度为:%d\n\n",tree1->height); + + printf("插入1\n"); + tree1 = Insert(tree1, 1); + printf("前序遍历:"); + pre_order(tree1); + printf("\n"); + printf("中序遍历:"); + in_order(tree1); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree1->height); + + printf("删除结点8\n"); + tree1 = del(tree1, 8); + printf("前序遍历:"); + pre_order(tree1); + printf("\n"); + printf("中序遍历:"); + in_order(tree1); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree1->height); + + printf("删除结点13\n"); + tree1 = del(tree1, 13); + printf("前序遍历:"); + pre_order(tree1); + printf("\n"); + printf("中序遍历:"); + in_order(tree1); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree1->height); + + printf("删除结点3\n"); + tree1 = del(tree1, 3); + printf("前序遍历:"); + pre_order(tree1); + printf("\n"); + printf("中序遍历:"); + in_order(tree1); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree1->height); + //第二种情况-右孩子的右子树 + Node* tree2 = NULL; + int a2[] = { 13, 8, 15, 14, 16 }; + int l2 = sizeof(a2) / sizeof(int); + for (int i = 0; i < l2; i++) + { + tree2 = Insert(tree2, a2[i]); + } + printf("第二种情况-右孩子的右子树\n"); + printf("前序遍历:"); + pre_order(tree2); + printf("\n"); + printf("中序遍历:"); + in_order(tree2); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree2->height); + + tree2 = Insert(tree2, 20); + printf("插入20\n"); + printf("前序遍历:"); + pre_order(tree2); + printf("\n"); + printf("中序遍历:"); + in_order(tree2); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree2->height); + + //第三种情况-左孩子的右子树 + Node* tree3 = NULL; + int a3[] = { 13, 8, 15, 3, 10 }; + int l3 = sizeof(a3) / sizeof(int); + for (int i = 0; i < l3; i++) + { + tree3 = Insert(tree3, a3[i]); + } + printf("第三种情况-左孩子的右子树\n"); + printf("前序遍历:"); + pre_order(tree3); + printf("\n"); + printf("中序遍历:"); + in_order(tree3); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree3->height); + + tree3 = Insert(tree3, 9); + printf("插入9\n"); + printf("前序遍历:"); + pre_order(tree3); + printf("\n"); + printf("中序遍历:"); + in_order(tree3); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree3->height); + + + //第四种情况 - 右孩子的左子树 + Node* tree4 = NULL; + int a4[] = { 13, 8, 18, 15, 20 }; + int l4 = sizeof(a4) / sizeof(int); + for (int i = 0; i < l4; i++) + { + tree4 = Insert(tree4, a4[i]); + } + printf("第四种情况-右孩子的左子树\n"); + printf("前序遍历:"); + pre_order(tree4); + printf("\n"); + printf("中序遍历:"); + in_order(tree4); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree4->height); + + tree4 = Insert(tree4, 14); + printf("插入14\n"); + printf("前序遍历:"); + pre_order(tree4); + printf("\n"); + printf("中序遍历:"); + in_order(tree4); + printf("\n"); + printf("根结点的深度为:%d\n\n", tree4->height); + + + +} \ No newline at end of file diff --git "a/2209040048/chapter9/\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" "b/2209040048/chapter9/\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..3c8af282 --- /dev/null +++ "b/2209040048/chapter9/\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" @@ -0,0 +1,15 @@ +int findKthLargest(int* nums, int numsSize, int k){ + //先对数组进行排序,使用冒泡进行从大到小排序,第k大的元素就是数组下标为k-1的元素 + int i,j,temp; + for(i=0; i +#include +#include + + +#define MAX_WORD (100) + +typedef struct tnode +{ + char ch; // 字符 + int cnt; // 出现次数 + struct tnode *lchild; // 左指针 + struct tnode *rchild; // 右指针 +}BSTNode; // 结点类型 + +/*-----------------采用递归方式向二叉排序树bst中插入一个字符----------------*/ +static void create_bst(BSTNode *&bt, char ch) // 指针的引用 +{ + if(bt == NULL) // bt为空,则建立一个新结点 + { + bt = (BSTNode *)malloc(sizeof(BSTNode)); + bt->ch = ch; + bt->cnt = 1; + bt->lchild = bt->rchild = NULL; + } + else if(ch == bt->ch) + bt->cnt++; + else if(ch < bt->ch) + create_bst(bt->lchild, ch); + else + create_bst(bt->rchild, ch); +} + +/*-----------------中序遍历二叉排序树bst----------------*/ +static void inorder(BSTNode *bt) +{ + if(bt != NULL) + { + inorder(bt->lchild); // 中序遍历左子树 + printf(" %c(%d)\n", bt->ch, bt->cnt); // 访问根结点 + inorder(bt->rchild); // 中序遍历右子树 + } +} + +/*-----------------销毁二叉排序树bst----------------*/ +static void destroy_bst(BSTNode *bt) +{ + if(bt != NULL) + { + destroy_bst(bt->lchild); + destroy_bst(bt->rchild); + free(bt); + } +} + +int main(int argc, char *argv[]) +{ + BSTNode *bt = NULL; + int i = 0; + char str[MAX_WORD]; + + printf("输入字符串:"); + gets(str); + while(str[i] != '\0') + { + create_bst(bt, str[i]); + i++; + } + printf("字符及出现次数:\n"); + inorder(bt); + printf("\n"); + destroy_bst(bt); + + return 0; +} diff --git "a/2209040048/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" "b/2209040048/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" new file mode 100644 index 00000000..539ffea6 --- /dev/null +++ "b/2209040048/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" @@ -0,0 +1,24 @@ +#include + +#define MaxSize 10 //定义最大长度 +typedef struct { + int data[MaxSize]; //数组存储线性表 + int length; //定义表长 +}SqList; + +//初始化,初始值置为0 +//在C语言中不存在引用的,&表示的不是引用,仅仅是取地址符。 +void InitList(SqList *L) { + for(int i=0; idata[i] = 0; + L->length = 0; + } +} + +int main() { + SqList L; + InitList(&L); + for(int i=0; i