diff --git "a/2224020157/chapter_1/10\343\200\20112\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" "b/2224020157/chapter_1/10\343\200\20112\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" new file mode 100644 index 0000000000000000000000000000000000000000..c7397cb0ee352356e83ecd3d42b71e719ddc16c2 --- /dev/null +++ "b/2224020157/chapter_1/10\343\200\20112\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" @@ -0,0 +1,217 @@ +#include +#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; // 学生记录类型 + +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); +} + +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); // 输出单链表 +} + +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); // 输出单链表 + } +} + +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; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/10\343\200\2014\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/2224020157/chapter_1/10\343\200\2014\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 0000000000000000000000000000000000000000..a6f400786327097a405067928b5a4260f8ab1ce2 --- /dev/null +++ "b/2224020157/chapter_1/10\343\200\2014\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,63 @@ +#include +#define MaxLength 100 + +int TotalNum = 0; +void InputData( int &count, int arrt[] ); +void BubbleSort( int data[], int n ); + +int main() +{ + int count = 0, i, j; + int data[MaxLength]; + InputData( count, data ); + printf( "排序前的数据:" ); + for( i = 1; i <= count; i++ ) + { + printf( "%5d", data[i] ); + } + printf( "\n" ); + BubbleSort( data, count ); + + return 0; +} + +void BubbleSort( int data[], int n ) +{ + int i, j, k, t; + for( i = 1; i <= n-1; i++ ) + { + for( j = 1; j <= n-i; j++ ) + { + if( data[j+1] < data[j] ) + { + t = data[j+1]; + data[j+1] = data[j]; + data[j] = t; + } + } + printf( "第 %d 趟排序:", i ); + for( k = 1; k <= n; k++ ) + { + printf( "%5d", data[k] ); + } + printf( "\n" ); + } +} +void InputData( int &count, int arrt[] ) +{ + int i = 0, data; + while( 1 ) + { + printf( "input an integer(end of 65535)" ); + scanf( "%d", &data ); + if( data == 65535 ) + { + break; + } + else + { + arrt[++i] = data; + } + } + count = i; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/10\343\200\2015\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/2224020157/chapter_1/10\343\200\2015\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 0000000000000000000000000000000000000000..d657d9a64621df5fe86e8932b5281052794331fd --- /dev/null +++ "b/2224020157/chapter_1/10\343\200\2015\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,108 @@ +#include +#include + +void PrintArray(int* a, int n)//把数组中的元素打印出来 +{ + for (int i = 0; i < n; ++i) + { + printf("%d ", a[i]); + } + printf("\n"); +} +void Swap(int* p1, int* p2)//交换两个值 +{ + int tmp = *p1; + *p1 = *p2; + *p2 = tmp; +} +//挖坑法 +int PartSort1(int*a,int left,int right) +{ + int begin = left; + int end = right; + int key = a[left]; + int pivot = left; + while (begin < end) + { + //找小 + while (begin < end && a[end] >= key) + { + end--; + } + a[pivot] = a[end]; + pivot = end; + //找大 + while (begin < end && a[begin] <= key) + { + begin++; + } + a[pivot] = a[begin]; + pivot = begin; + } + pivot = begin; + a[pivot] = key; + + return pivot; +} +//左右指针法 +int PartSort2(int* a, int left, int right) +{ + int begin = left; + int end = right; + int key = a[left]; + while (begin < end) + { + //找小 + while (begin < end && a[end] >= key) + { + end--; + } + //找大 + while (begin < end && a[begin] <= key) + { + begin++; + } + Swap(&a[begin], &a[end]); + } + Swap(&a[begin], &a[left]); + return begin; +} +//前后指针法 +int PartSort3(int* a, int left, int right) +{ + int prev = left, cur = left + 1; + int key = a[left]; + while (cur <= right) + { + if (a[cur] < key && ++prev != cur) + { + Swap(&a[cur], &a[prev]); + } + cur++; + } + Swap(&a[left], &a[prev]); + return prev; +} +void QuickSort(int* a, int left,int right) +{ + if (left >= right) + { + return; + } + int keyindex = PartSort1(a, left, right); + //int keyindex = PartSort2(a, left, right);//左右指针法 + //int keyindex = PartSort3(a, left, right);//前后指针法 + QuickSort(a, left, keyindex - 1); + QuickSort(a, keyindex + 1, right); +} +void TestQuickSort() +{ + int a[] = { 6,1,2,7,9,3,4,5,10,8 }; + QuickSort(a, 0,sizeof(a) / sizeof(int)-1); + PrintArray(a, sizeof(a) / sizeof(int)); +} +int main() +{ + TestQuickSort(); + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/4\343\200\2013\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" "b/2224020157/chapter_1/4\343\200\2013\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..79e0b9007eae8bc390e43c8fe8bc5082c638ff2b --- /dev/null +++ "b/2224020157/chapter_1/4\343\200\2013\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" @@ -0,0 +1,140 @@ +#include +#include + +typedef struct String { + char* data; + int length; +}Str; + +void StrAssign(Str&, char*); +int SubStr(Str, Str); +void GetNext(Str, int*&); +void GetNextval(Str, int*&); +int KMP(Str, Str, int*); + +int main(int argc, const char* argv[]) +{ + char s[] = "abcabcdabcdeabcdefabcdefg"; + char t[] = "abcdeabcdefab"; + Str S, T; + int i,idx; + int* next = NULL, * nextval = NULL; + StrAssign(S, s); + StrAssign(T, t); + printf("简单匹配的结果(从1开始):\n"); + idx = SubStr(S, T); + if (idx != -1) { + printf("匹配成功,位置为%d。\n", idx); + } + else { + printf("匹配失败。\n"); + } + GetNext(T, next); + printf("next数组:\n"); + for (i = 0; i < T.length; i++) { + printf("%d\t", next[i]); + } + printf("\n"); + GetNextval(T, nextval); + printf("nextval数组:\n"); + for (i = 0; i < T.length; i++) { + printf("%d\t", nextval[i]); + } + printf("\n"); + printf("使用KMP的结果(从1开始):\n"); + idx = KMP(S, T, next); + if (idx != -1) { + printf("匹配成功,位置为%d。\n", idx); + } + else { + printf("匹配失败。\n"); + } + printf("使用改进KMP的结果(从1开始):\n"); + idx = KMP(S, T, nextval); + if (idx != -1) { + printf("匹配成功,位置为%d。\n", idx); + } + else { + printf("匹配失败。\n"); + } + return 0; +} +void StrAssign(Str& S, char* s) +{ + int i; + for (i = 0; s[i] != '\0'; i++); + S.length = i; + S.data = s; +} +int SubStr(Str S, Str T) +{ + int i, j; + for (i = 0; i < S.length; i++) { + for (j = 0; j < T.length; j++) { + if (S.data[i + j] != T.data[j]) + break; + } + if (j >= T.length) + return (i+1); + return -1; +} +void GetNext(Str T, int*& next) +{ + int i, j; + i = 0; + j = -1; + next = (int*)malloc(T.length * sizeof(int)); + while (next == NULL) { + next = (int*)malloc(T.length * sizeof(int)); + } + next[0] = -1; + while (i < T.length) { + if (j == -1 || T.data[i] == T.data[j]) { + i++; + j++; + next[i] = j; + } + else + j = next[j]; + } +} +void GetNextval(Str T, int*& nextval) +{ + int i, j; + i = 0; + j = -1; + nextval = (int*)malloc(T.length * sizeof(int)); + while (nextval == NULL) { + nextval = (int*)malloc(T.length * sizeof(int)); + } + nextval[0] = -1; + while (i < T.length) { + if (j == -1 || T.data[i] == T.data[j]) { + i++; + j++; + if (T.data[i] != T.data[j]) + nextval[i] = j; + else + nextval[i] = nextval[j]; + } + else + j = nextval[j]; + } +} +int KMP(Str S, Str T, int* N) +{ + int i, j; + i = j = 0; + while (i < S.length && j < T.length) { + if (j == -1 || S.data[i] == T.data[j]) { + i++; + j++; + } + else + j = N[j]; + } + if (j >= T.length) + return (i+1-T.length); + else + return -1; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/4\343\200\2015\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" "b/2224020157/chapter_1/4\343\200\2015\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..301b67ce1fc98b4971299b5c51a6446c36a2fed0 --- /dev/null +++ "b/2224020157/chapter_1/4\343\200\2015\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" @@ -0,0 +1,74 @@ +#include +#define MaxSize 100 +typedef struct { + char data[MaxSize]; + int length; +} SqString; + +void init(SqString &s) +{ + s.length=0; +} + +int insert(SqString &s,char a) +{ + if(s.length>=MaxSize) + return 0; + s.data[s.length++]=a; +} + +void print(SqString s) +{ + for(int i=0;isub.length) + { + sub.length=length; + loc=i; + } + j+=length; + } + else j++; + } + } + for(i=loc,j=0;i +#include +#include +using namespace std; + +void fail(const char* pat, int* f){ + const int len=strlen(pat); + f[0]=-1; + for (int j=1; j < len; j++){ + if (pat[j] == pat[f[j-1]+1]) + f[j] = f[j-1]+1; + else { + int i=f[j-1]; + while ( i >=0 && pat[j] != pat[i+1] ) + i = f[i]; + if (pat[j] ==pat[i+1]) + f[j] = i+1; + else + f[j] = -1; + } + } +} + +int search(const char* tg, const char *pt, const int* f){ + const int ptLen=strlen(pt); + const int tgLen = strlen(tg); + int tgPos=0,ptPos=0; + for (; tgPos < tgLen && ptPos < ptLen;) { + if (tg[tgPos] == pt[ptPos]){ + ptPos++; + tgPos++; + } + else { + if (ptPos == 0) + tgPos ++; + else + ptPos = f[ptPos-1] + 1; + } + } + return ptPos < ptLen ? -1: tgPos - ptLen; +} + +int count(const char* tg, const char *pt, const int* f){ + const int ptLen=strlen(pt); + const int tgLen = strlen(tg); + int tgPos=0,ptPos=0,count=0; + for (; tgPos < tgLen;) { + if (tg[tgPos] == pt[ptPos]){ + ptPos++; + tgPos++; + } + else { + if (ptPos == 0) + tgPos ++; + else + ptPos = f[ptPos-1] + 1; + } + if (ptPos >= ptLen ){ + count ++; + ptPos=f[ptLen-1] +1; + } + } + return count; +} + +int main() +{ + const char *tag="abcbcbcbc"; + const char *pat="bcbc"; + int len=strlen(pat); + int *f =new int[len]; + fail(pat,f); + cout <<"失效函数位置:\n"; + for(int i=0;i +void move(int n, char pos1, char pos3) +{ + printf("盘子%d: 从 %c柱 移动到 %c柱\n", n, pos1, pos3); + +} + +void Hanoi(int n, char pos1, char pos2, char pos3) +{ + if (n == 1) + { + move(n, pos1, pos3); + } + else + { + Hanoi(n-1, pos1, pos3, pos2); + move(n, pos1, pos3); + Hanoi(n-1, pos2, pos1, pos3); + } +} + +int main() +{ + int m; + printf("input the number of disks:"); + scanf("%d",&m); + char pos1 = 'A'; + char pos2 = 'B'; + char pos3 = 'C'; + + printf("移动%d个盘子的步骤如下↓\n", n); + Hanoi(n, pos1, pos2, pos3); + + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/5\343\200\2013\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" "b/2224020157/chapter_1/5\343\200\2013\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..27d3431749fc9ec0f0c16d5b31126ef286bacccc --- /dev/null +++ "b/2224020157/chapter_1/5\343\200\2013\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" @@ -0,0 +1,87 @@ +#include +#include +#include +#include +static bool valid(char *ip, int len) +{ + if (len > 1 && ip[0] == '0') + { + return false; + } + if (len == 3) + { + int n = (ip[0] - '0') * 100 + (ip[1] - '0') * 10 + (ip[2] - '0'); + if (n > 255) + { + return false; + } + } + return true; +} +#define WIDTH 4 +static void dfs(char *s, int start, char *stack, int num, char **results, int *count) +{ + int i, j; + if (num == 4) + { + if (s[start] == '\0') + { + results[*count] = malloc(3 * 4 + 3 + 1); + char *p = results[*count]; + for (j = 0; j < num; j++) + { + char *q = stack + j * WIDTH; + while ((*p++ = *q++) != '\0') + { + } + if (j != 3) + { + *(p - 1) = '.'; + } + } + (*count)++; + } + } + else + { + char *p = stack + num * WIDTH; + char *q = p; + for (i = start; s[i] != '\0' && i < start + 3; i++) + { + *q++ = s[i]; + *q = '\0'; + if (!valid(p, q - p)) + { + return; + } + dfs(s, i + 1, stack, num + 1, results, count); + if (num + 1 < 4) + { + memset(stack + (num + 1) * WIDTH, 0, WIDTH); + } + } + } +} +static char **restoreIpAddresses(char *s, int *returnSize) +{ + int count = 0; + char **results = malloc(100 * sizeof(char *)); + char addr[16] = {'\0'}; + dfs(s, 0, addr, 0, results, &count); + *returnSize = count; + return results; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test num\n"); + exit(-1); + } + int i, count = 0; + char **list = restoreIpAddresses(argv[1], &count); + for (i = 0; i < count; i++) + { + printf("%s\n", list[i]); + } +} \ No newline at end of file diff --git "a/2224020157/chapter_1/5\343\200\2017\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2224020157/chapter_1/5\343\200\2017\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8a02ef68c081465510061196b73ff749711aa684 --- /dev/null +++ "b/2224020157/chapter_1/5\343\200\2017\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" @@ -0,0 +1,44 @@ +#include +#include +int N; +int queenPos[100]; +void NQueen(int k) +{ + int i; + if(k==N) + { + for(i=0;i +#include + +#define N 4 +#define MAX_SIZE 100 + +typedef int ElemType; + +typedef struct +{ + int r; + int c; + ElemType d; +}TupNode; +typedef struct +{ + int rows; + int cols; + int nums; + TupNode data[MAX_SIZE]; +}TSMatrix; + +static void create_matrix(TSMatrix &t, ElemType A[N][N]) +{ + int i, j; + + t.rows = N; + t.cols = N; + t.nums = 0; + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + if(A[i][j] != 0) + { + t.data[t.nums].r = i; + t.data[t.nums].c = j; + t.data[t.nums].d = A[i][j]; + t.nums++; + } + } + } +} + +static void disp_matrix(TSMatrix t) +{ + int i; + + if(t.nums <= 0) + return; + printf("\t%d\t%d\t%d\n", t.rows, t.cols, t.nums); + printf("\t------------------\n"); + for(i = 0; i < t.nums; i++) + printf("\t%d\t%d\t%d\n", t.data[i].r, t.data[i].c, t.data[i].d); +} + + +static void tran_matrix(TSMatrix t, TSMatrix &tb) +{ + int p, v; + int q = 0; + + tb.rows = t.cols; + tb.cols = t.rows; + tb.nums = t.nums; + if(t.nums != 0) + { + for(v = 0; v < t.cols; v++) + { + for(p = 0; p < t.nums; p++) + { + if(t.data[p].c == v) + { + tb.data[q].r = t.data[p].c; + tb.data[q].c = t.data[p].r; + tb.data[q].d = t.data[p].d; + q++; + } + } + } + } +} + +static bool matrix_add(TSMatrix a, TSMatrix b, TSMatrix &c) +{ + int i = 0; + int j = 0; + int k = 0; + ElemType v; + + if(a.rows != b.rows || a.cols != b.cols) + return false; + + c.rows = a.rows; + c.cols = a.cols; + while(i < a.nums && j < b.nums) + { + if(a.data[i].r == b.data[j].r) + { + if(a.data[i].c < b.data[j].c) + { + + c.data[k].r = a.data[i].r; + c.data[k].c = a.data[i].c; + c.data[k].d = a.data[i].d; + k++; + i++; + } + else if(a.data[i].c > b.data[j].c) + { + + c.data[k].r = b.data[j].r; + c.data[k].c = b.data[j].c; + c.data[k].d = b.data[j].d; + k++; + j++; + } + else + { + v = a.data[i].d + b.data[j].d; + if(v != 0) + { + c.data[k].r = a.data[i].r; + c.data[k].c = a.data[i].c; + c.data[k].d = v; + k++; + } + i++; + j++; + } + } + else if(a.data[i].r < b.data[j].r) + { + + c.data[k].r = a.data[i].r; + c.data[k].c = a.data[i].c; + c.data[k].d = a.data[i].d; + k++; + i++; + } + else + { + + c.data[k].r = b.data[j].r; + c.data[k].c = b.data[j].c; + c.data[k].d = b.data[j].d; + k++; + j++; + } + c.nums = k; + } + + return true; +} + +static int get_value(TSMatrix t, int i, int j) +{ + int k = 0; + + while(k < t.nums && (t.data[k].r != i || t.data[k].c != j)) + k++; + if(k < t.nums) + return t.data[k].d; + else + return 0; +} + +static bool matrix_mul(TSMatrix a, TSMatrix b, TSMatrix &c) +{ + int i; + int j; + int k; + int p = 0; + ElemType s; + + if(a.cols != b.rows) + return false; + for(i = 0; i < a.rows; i++) + { + for(j = 0; j < b.cols; j++) + { + s = 0; + for(k = 0; k < a.cols; k++) + { + s = s + get_value(a, i, k) * get_value(b, k, j); + } + if(s != 0) + { + c.data[p].r = i; + c.data[p].c = j; + c.data[p].d = s; + p++; + } + } + } + + c.rows = a.rows; + c.cols = b.cols; + c.nums = p; // 矩阵c的非零元素个数 + + return true; +} + +int main(void) +{ + ElemType a1[N][N] = { + {1, 0, 3, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 1} + }; + ElemType b1[N][N] = { + {3, 0, 0, 0}, + {0, 4, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 2} + }; + + TSMatrix a, b, c; + + create_matrix(a, a1); + create_matrix(b, b1); + printf("a的三元组:\n"); + disp_matrix(a); + printf("b的三元组:\n"); + disp_matrix(b); + printf("a转置为c\n"); + tran_matrix(a, c); + printf("c的三元组:\n"); + disp_matrix(c); + + printf("c=a+b\n"); + matrix_add(a, b, c); + printf("c的三元组:\n"); + disp_matrix(c); + + printf("c=a*b\n"); + matrix_mul(a, b, c); + printf("c的三元组:\n"); + disp_matrix(c); + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/6\343\200\2013\346\261\2025\303\2275\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" "b/2224020157/chapter_1/6\343\200\2013\346\261\2025\303\2275\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bdc5503e0e614e36e3c4ceccfc2e5c51ccdf5134 --- /dev/null +++ "b/2224020157/chapter_1/6\343\200\2013\346\261\2025\303\2275\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" @@ -0,0 +1,66 @@ +#include + +#define MAX_LEN 10 + +/*------------------求n阶螺旋方阵a-----------------*/ +static void helix_matrix(int a[MAX_LEN][MAX_LEN], int n) +{ + int m; + int i; + int j; + int k = 0; + + if(n % 2 == 0) + m = n / 2; + else + m = n / 2 + 1; + for(i = 0; i < m; i++) + { + + for(j = i; j < n - i; j++) + { + k++; + a[i][j] = k; + } + + for(j = i + 1; j < n - i; j++) + { + k++; + a[j][n - i - 1] = k; + } + + for(j = n - i - 2; j >= i; j--) + { + k++; + a[n - i - 1][j] = k; + } + + for(j = n - i - 2; j >= i + 1; j--) + { + k++; + a[j][i] = k; + } + } +} + +int main(void) +{ + int n, i, j; + int a[MAX_LEN][MAX_LEN]; + + printf("输入n(n<10):"); + scanf("%d", &n); + helix_matrix(a, n); + printf("%d阶数字方阵如下:\n", n); + + for(i = 0; i < n; i++) + { + for(j = 0; j < n; j++) + { + printf("%4d", a[i][j]); + } + printf("\n"); + } + + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/6\343\200\2015\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" "b/2224020157/chapter_1/6\343\200\2015\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..6cf9b6fa4ee6be537ae89e9aae85ae73cecfac7a --- /dev/null +++ "b/2224020157/chapter_1/6\343\200\2015\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" @@ -0,0 +1,99 @@ +#include + +#define N 4 +#define M 10 + +/*-----------------------返回压缩存储a中a[i][j]的值-----------------------*/ + +static int value(int a[], int i, int j) +{ + if(i >= j) + return a[(i * (i + 1)) / 2 + j]; + else + return a[(j * (j + 1)) / 2 + i]; +} + +/*-----------------------求压缩存储a和b的和-----------------------*/ +static void madd(int a[], int b[], int c[][N]) +{ + int i, j; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + c[i][j] = value(a, i, j) + value(b, i, j); + } + } +} + +/*-----------------------求压缩存储a和b的乘积-----------------------*/ +static void mult(int a[], int b[], int c[][N]) +{ + int i, j, k, sum; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + sum = 0; + for(k = 0; k < N; k++) + { + sum = sum + value(a, i, k) * value(b, k, j); + } + c[i][j] = sum; + } + } +} + +/*-----------------------输出压缩存储a-----------------------*/ +static void disp1(int a[]) +{ + int i, j; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + printf("%4d", value(a, i, j)); + } + printf("\n"); + } +} + +/*-----------------------输出对称矩阵c-----------------------*/ +static void disp2(int c[][N]) +{ + int i, j; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + printf("%4d", c[i][j]); + } + printf("\n"); + } +} + +int main(int argc, char *argv[]) +{ + int a[M] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //a表示压缩存储的对称矩阵 + int b[M] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int c1[N][N], c2[N][N]; + + printf("a矩阵:\n"); + disp1(a); + printf("b矩阵:\n"); + disp1(b); + + madd(a, b, c1); + printf("a+b:\n"); + disp2(c1); + + mult(a, b, c2); + printf("a*b:\n"); + disp2(c2); + + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/7\343\200\20110\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" "b/2224020157/chapter_1/7\343\200\20110\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..86d1483295dbb00a4105eb0dd273207401171028 --- /dev/null +++ "b/2224020157/chapter_1/7\343\200\20110\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" @@ -0,0 +1,204 @@ +#include +#include +#define MaxSize 66 +#include +#include +#define MaxSons 3 +typedef char ElemType; +typedef struct node +{ + ElemType data[15]; //结点的值 + struct node *sons[MaxSons]; //指向孩子结点 +}TSonNode; //孩子链存储结构中的结点类型 +typedef struct +{ + char N[15]; + char n[15]; +}array; +//读取文件内容到数组R中 +void ReadFile(array R[],FILE *fp,int &n) +{ + while((fscanf(fp,"%s",R[n].N))!=EOF&&(fscanf(fp,"%s",R[n].n))!=EOF) + n++; +} +//创建一颗树 +TSonNode *CreateTree(char str[],array R[],int n) +{ + TSonNode *t; + int k,i=0,j=0; + t=(TSonNode *)malloc(sizeof(TSonNode)); + strcpy(t->data,str); + for(k=0;ksons[k]=NULL; + while(isons[j]=CreateTree(R[i].n,R,n); + j++; + } + i++; + } + return t; +} +//输出树(孩子链存储结构) +void DispTree(TSonNode *t) +{ + int i=0; + if(t==NULL) + printf("此树为空树!\n"); + else + { + printf("%s",t->data); + if(t->sons[i]!=NULL) //若t结点至少有一个孩子 + { + printf("("); + for(i=0;isons[i]); + if(t->sons[i+1]!=NULL) + printf(","); + else + break; + } + printf(")"); + } + } +} +//销毁树 +void DestroyTree(TSonNode *t) +{ + if(t==NULL) + printf("此树为空树!\n"); + else + { + for(int i=0;isons[i]!=NULL) + DestroyTree(t->sons[i]); + else + break; + } + free(t); + } +} +//查找某一结点 +TSonNode *FindNode(TSonNode *t,char str[]) +{ + TSonNode *p; + if(t==NULL) + return NULL; + else + { + if(strcmp(t->data,str)==0) + return t; + else + { + for(int i=0;isons[i]!=NULL) + { + p=FindNode(t->sons[i],str); + if(p!=NULL) + return p; + } + } + return NULL; + } + } +} +//求某一结点的孩子个数 +int ChildCount(TSonNode *p) +{ + int count=0; + for(int i=0;isons[i]!=NULL) + count++; + else + break; + } + return count; +} +//求某棵树中的叶子结点数 +//本例中,叶子结点数等于班级数,一个叶子结点对应一个班级 +int LeafCount(TSonNode *p) +{ + int count=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + count++; + else + { + for(int i=0;isons[i]!=NULL) + count=count+LeafCount(p->sons[i]); + else + break; + } + } + } + return count; +} +//求某棵树的叶子结点值的和 +int LeafSumOfvalue(TSonNode *p) +{ + int sum=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + return atoi(p->data); + else + { + for(int i=0;isons[i]!=NULL) + sum+=LeafSumOfvalue(p->sons[i]); + else + break; + } + } + } + return sum; +} + + +int main() +{ + int n=0; + TSonNode *t; + array R[MaxSize]; + FILE *fp; + if((fp=fopen("abc1.txt","r"))==NULL) //以只读方式打开table.txt文件 + { + printf("error!cannot open the file!"); + exit(1); + } + printf("读取文件内容存入数组R中\n"); + ReadFile(R,fp,n); + printf("输出数组R:\n"); + for(int i=0;i +#include +#define MaxSize 100 +typedef char ElemType; +typedef struct node +{ + ElemType data; + struct node *lchild; + struct node *rchild; + +}BTNode; +void CreateBTree(BTNode *&b,char *str) +{ + BTNode * St[MaxSize],*p; + int top=-1,k,j=0;char ch; + b=NULL; + ch=str[j]; + while(ch!='\0') + { + switch(ch) + { + case'(':top++;St[top]=p;k=1;break; + case')':top--;break; + case',':k=2;break; + + default:p=(BTNode *)malloc(sizeof(BTNode)); + p->data=ch;p->lchild=p->rchild=NULL; + if(b==NULL) + b=p; + else + { + switch(k) + { + case 1:St[top]->lchild=p;break; + case 2:St[top]->rchild=p;break; + } + } + } + j++;ch=str[j]; + } +} +void DestroyBTree(BTNode *&b) +{ + if(b!=NULL) + { + DestroyBTree(b->lchild); + DestroyBTree(b->rchild); + free(b); + } +} +BTNode *FindNode(BTNode *b,ElemType x) +{ + BTNode *p; + if(b==NULL) + return NULL; + else if(b->data==x) + return b; + else + { + p=FindNode(b->lchild,x); + if(p!=NULL) + return p; + else + return FindNode(b->rchild,x); + } +} +BTNode *LchildNode(BTNode *p) +{ + return p->lchild; + +} +BTNode *RchildNode(BTNode *p) +{ + return p->rchild; +} +int BTHeight(BTNode *b) +{ + int lchildh,rchildh; + if(b==NULL)return(0); + else + { + lchildh=BTHeight(b->lchild); + rchildh=BTHeight(b->rchild); + return(lchildh>rchildh)?(lchildh+1):(rchildh+1); + } +} +void DispBTree(BTNode *b) +{ + if(b!=NULL) + { + printf("%c",b->data); + if(b->lchild!=NULL||b->rchild!=NULL) + { + printf("("); + DispBTree(b->lchild); + if(b->rchild!=NULL)printf(","); + DispBTree(b->rchild); + printf(")"); + } + } +} +int main() +{ + BTNode *b,*p,*lp,*rp;; + printf("二叉树的基本运算如下:\n"); + printf(" (1)创建二叉树\n"); + CreateBTree(b,"A(B(D,E(H(J,k(L,M(,N))))),C(F,G(,I)))"); + printf(" (2)输出二叉树:");DispBTree(b);printf("\n"); + printf(" (3)H结点:"); + p=FindNode(b,'H'); + if(p!=NULL) + { lp= LchildNode(p); + if(lp!=NULL)printf("左孩子为%c",lp->data); + else printf("无左孩子"); + rp=RchildNode(p); + if(rp!=NULL) printf("右孩子为%c",rp->data); + else printf("无右孩子"); + + } + printf("\n"); + printf(" (4)二叉树b的高度:%d\n",BTHeight(b)); + printf(" (5)释放二叉树b\n"); + DestroyBTree(b); + return 1; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/7\343\200\2015\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" "b/2224020157/chapter_1/7\343\200\2015\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..f911bb547dc4a1db423158785f1473e19285a056 --- /dev/null +++ "b/2224020157/chapter_1/7\343\200\2015\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" @@ -0,0 +1,138 @@ +#include +#include +#include + +typedef double DataType; //结点权值的数据类型 + +typedef struct HTNode //单个结点的信息 +{ + DataType weight; //权值 + int parent; //父节点 + int lc, rc; //左右孩子 +}*HuffmanTree; + +typedef char **HuffmanCode; //字符指针数组中存储的元素类型 + +//在下标为1到i-1的范围找到权值最小的两个值的下标,其中s1的权值小于s2的权值 +void Select(HuffmanTree& HT, int n, int& s1, int& s2) +{ + int min; + //找第一个最小值 + for (int i = 1; i <= n; i++) + { + if (HT[i].parent == 0) + { + min = i; + break; + } + } + for (int i = min + 1; i <= n; i++) + { + if (HT[i].parent == 0 && HT[i].weight < HT[min].weight) + min = i; + } + s1 = min; //第一个最小值给s1 + //找第二个最小值 + for (int i = 1; i <= n; i++) + { + if (HT[i].parent == 0 && i != s1) + { + min = i; + break; + } + } + for (int i = min + 1; i <= n; i++) + { + if (HT[i].parent == 0 && HT[i].weight < HT[min].weight&&i != s1) + min = i; + } + s2 = min; //第二个最小值给s2 +} + +//构建哈夫曼树 +void CreateHuff(HuffmanTree& HT, DataType* w, int n) +{ + int m = 2 * n - 1; //哈夫曼树总结点数 + HT = (HuffmanTree)calloc(m + 1, sizeof(HTNode)); //开m+1个HTNode,因为下标为0的HTNode不存储数据 + for (int i = 1; i <= n; i++) + { + HT[i].weight = w[i - 1]; //赋权值给n个叶子结点 + } + for (int i = n + 1; i <= m; i++) //构建哈夫曼树 + { + //选择权值最小的s1和s2,生成它们的父结点 + int s1, s2; + Select(HT, i - 1, s1, s2); //在下标为1到i-1的范围找到权值最小的两个值的下标,其中s1的权值小于s2的权值 + HT[i].weight = HT[s1].weight + HT[s2].weight; //i的权重是s1和s2的权重之和 + HT[s1].parent = i; //s1的父亲是i + HT[s2].parent = i; //s2的父亲是i + HT[i].lc = s1; //左孩子是s1 + HT[i].rc = s2; //右孩子是s2 + } + //打印哈夫曼树中各结点之间的关系 + printf("哈夫曼树为:>\n"); + printf("下标 权值 父结点 左孩子 右孩子\n"); + printf("0 \n"); + for (int i = 1; i <= m; i++) + { + printf("%-4d %-6.2lf %-6d %-6d %-6d\n", i, HT[i].weight, HT[i].parent, HT[i].lc, HT[i].rc); + } + printf("\n"); +} + +//生成哈夫曼编码 +void HuffCoding(HuffmanTree& HT, HuffmanCode& HC, int n) +{ + HC = (HuffmanCode)malloc(sizeof(char*)*(n + 1)); //开n+1个空间,因为下标为0的空间不用 + char* code = (char*)malloc(sizeof(char)*n); //辅助空间,编码最长为n(最长时,前n-1个用于存储数据,最后1个用于存放'\0') + code[n - 1] = '\0'; //辅助空间最后一个位置为'\0' + for (int i = 1; i <= n; i++) + { + int start = n - 1; //每次生成数据的哈夫曼编码之前,先将start指针指向'\0' + int c = i; //正在进行的第i个数据的编码 + int p = HT[c].parent; //找到该数据的父结点 + while (p) //直到父结点为0,即父结点为根结点时,停止 + { + if (HT[p].lc == c) //如果该结点是其父结点的左孩子,则编码为0,否则为1 + code[--start] = '0'; + else + code[--start] = '1'; + c = p; //继续往上进行编码 + p = HT[c].parent; //c的父结点 + } + HC[i] = (char*)malloc(sizeof(char)*(n - start)); //开辟用于存储编码的内存空间 + strcpy(HC[i], &code[start]); //将编码拷贝到字符指针数组中的相应位置 + } + free(code); //释放辅助空间 +} + +//主函数 +int main() +{ + int n = 0; + printf("请输入数据个数:>"); + scanf("%d", &n); + DataType* w = (DataType*)malloc(sizeof(DataType)*n); + if (w == NULL) + { + printf("malloc fail\n"); + exit(-1); + } + printf("请输入数据:>"); + for (int i = 0; i < n; i++) + { + scanf("%lf", &w[i]); + } + HuffmanTree HT; + CreateHuff(HT, w, n); //构建哈夫曼树 + + HuffmanCode HC; + HuffCoding(HT, HC, n); //构建哈夫曼编码 + + for (int i = 1; i <= n; i++) //打印哈夫曼编码 + { + printf("数据%.2lf的编码为:%s\n", HT[i].weight, HC[i]); + } + free(w); + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/7\343\200\2017\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" "b/2224020157/chapter_1/7\343\200\2017\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e6d0bb14bb7ea1079df26f461351bd73bcfaf57a --- /dev/null +++ "b/2224020157/chapter_1/7\343\200\2017\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" @@ -0,0 +1,322 @@ +#include +#include +#include + +#define MAX_SIZE 100 + +typedef char ElemType; +typedef struct node +{ + ElemType data; // 数据元素 + struct node *lchild; // 指向左孩子结点 + struct node *rchild; // 指向右孩子结点 +}BTNode; // 声明二叉链结点类型 + +/*-------------由括号表示串str创建二叉链b-----------------*/ +static void create_btree(BTNode *&b, char *str) // 创建二叉树(形参b:指针的引用) +{ + BTNode *p; + BTNode *St[MAX_SIZE]; // 定义一个顺序栈 + int k; + int j = 0; + int top = -1; // 栈顶指针初始化 + char ch; + + b = NULL; // 建立的二叉树初始时为空 + ch = str[j]; // 取第一个字符 + while(ch != '\0') // str未扫描完时循环 + { + switch(ch) + { + case '(': // 开始处理左子树 + top++; + St[top] = p; + k = 1; + break; + case ')': // 子树处理完毕 + top--; + break; + case ',': // 开始处理右子树 + k = 2; + break; + default: + p = (BTNode *)malloc(sizeof(BTNode)); // 动态分配结点p的存储空间 + p->data = ch; + p->lchild = p->rchild = NULL; + if(b == NULL) // 若b为空,p置为二叉树的根结点 + b = p; + else // 已建立二叉树根结点 + { + switch(k) + { + case 1: + St[top]->lchild = p; + break; + case 2: + St[top]->rchild = p; + break; + } + } + break; + } + // 取下一个字符 + j++; + ch = str[j]; + } +} + +/*--------------------------以括号表示法输出二叉树b----------------------*/ +// "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))" +static void disp_btree(BTNode *b) +{ + if(b != NULL) + { + printf("%c", b->data); + if(b->lchild != NULL || b->rchild != NULL) + { + printf("("); // 有孩子结点时才输出( + disp_btree(b->lchild); // 递归处理左子树 + if(b->rchild != NULL) // 有右孩子结点时才输出, + printf(","); + disp_btree(b->rchild); // 递归处理右子树 + printf(")"); // 有孩子结点时才输出) + } + } +} + +/*--------------------------释放二叉树b的所有结点----------------------*/ +static void destroy_btree(BTNode *&b) // 销毁二叉树(形参b:指针的引用) +{ + if(b != NULL) + { + destroy_btree(b->lchild); + destroy_btree(b->rchild); + free(b); + } +} + +/*--------------------------返回b结点的左孩子结点指针----------------------*/ +static BTNode *left_child_node(BTNode *b) +{ + return b->lchild; +} + +/*--------------------------返回b结点的右孩子结点指针----------------------*/ +static BTNode *right_child_node(BTNode *b) +{ + return b->rchild; +} + +/*--------------------------返回data域为x的结点指针----------------------*/ +static BTNode *find_node(BTNode *b, ElemType x) // 查找值为x的结点 +{ + BTNode *p; + + if(b == NULL) + return NULL; + else if(b->data == x) + return b; + else + { + p = find_node(b->lchild, x); + if(p != NULL) + return p; + else + return find_node(b->rchild, x); + } +} + +/*--------------------------求二叉树b的深度----------------------*/ +static int btree_height(BTNode *b) +{ + int left_child_height, right_child_height; + + if(b == NULL) // 空树的深度为0 + return 0; + else + { + // 求左子树的深度 + left_child_height = btree_height(b->lchild); + // 求右子树的深度 + right_child_height = btree_height(b->rchild); + + return (left_child_height > right_child_height) ? (left_child_height + 1) : (right_child_height + 1); + } +} + +/*--------------------------采用先序遍历方法输出所有从叶子结点到根结点的逆路径----------------------*/ +static void all_path1(BTNode *b, ElemType path[], int path_len) +{ + int i; + + if(b == NULL) + return; + + if(b->lchild == NULL && b->rchild == NULL) // b为叶子结点 + { + printf(" %c到根结点逆路径: %c->", b->data, b->data); + for(i = path_len - 1; i > 0; i--) // 逆序输出 + printf("%c->", path[i]); + printf("%c\n", path[0]); // 输出根结点A + } + else + { + path[path_len] = b->data; // 将当前结点放入路径中 + path_len++; // 路径长度增1 + all_path1(b->lchild, path, path_len); // 递归扫描左子树 + all_path1(b->rchild, path, path_len); // 递归扫描右子树 + } +} + +/*--------------------------采用先序遍历方法输出第一条最长的逆路径----------------------*/ +static void long_path1(BTNode *b, ElemType path[], int path_len, ElemType long_path[], int &long_path_len) // 引用long_path_len带回最长路径长度 +{ + int i; + + if(b == NULL) + { + if(path_len > long_path_len) // 若当前路径更长,将路径保存在long_path中 + { + for(i = path_len - 1; i >= 0; i--) // 逆序保存到long_path + long_path[i] = path[i]; + long_path_len = path_len; // 记录最长路径长度 + } + } + else + { + path[path_len] = b->data; // 将当前结点放入路径中 + //printf("path[%d] = %c\n", path_len, path[path_len]); + path_len++; // 路径长度增1 + long_path1(b->lchild, path, path_len, long_path, long_path_len); // 递归扫描左子树 + long_path1(b->rchild, path, path_len, long_path, long_path_len); // 递归扫描右子树 + } +} + +/*--------------------------采用后序(左、右、根)非递归遍历方法输出所有从叶子结点到根结点的逆路径----------------------*/ +static void all_path2(BTNode *b) +{ + BTNode *st[MAX_SIZE]; // 定义一个顺序栈st + int top = -1; // 栈顶指针初始化 + BTNode *p; + BTNode *r; + bool flag; + + p = b; // p->A + do + { + while(p != NULL) // 扫描结点p的所有左下结点并进栈 + { + top++; + st[top] = p; // 结点p进栈 + p = p->lchild; // 移动到左孩子 + } + r = NULL; // r指向刚刚访问的结点,初始时为空 + flag = true; // flag为真表示正在处理栈顶结点 + while(top > -1 && flag) // 栈不空且flag为真时循环 + { + p = st[top]; // 取出当前的栈顶结点p + if(p->rchild == r) // 若结点p的右孩子为空或者为刚刚访问过的结点 + { + if(p->lchild == NULL && p->rchild == NULL) // 若为叶子结点 + { + // 输出栈中所有结点值 + printf(" %c到根结点逆路径: ", p->data); + for(int i = top; i > 0; i--) + printf("%c->", st[i]->data); + // 输出根结点 + printf("%c\n", st[0]->data); + } + top--; // 退栈 + r = p; // r指向刚访问过的结点 + } + else + { + p = p->rchild; // 转向处理其右子树 + flag = false; // 表示当前不是处理栈顶结点 + } + } + }while(top > -1); // 栈不空时循环 +} + +typedef struct snode +{ + BTNode *node; // 存放当前结点指针 + int parent; // 存放双亲结点在队列中的位置 +}snode; // 声明顺序队列结点类型 + +/*--------------------------采用层次遍历方法输出所有从叶子结点到根结点的逆路径----------------------*/ +static void all_path3(BTNode *b) +{ + snode qu[MAX_SIZE]; // 定义顺序队列 + int que_front, que_rear; // 定义队头和队尾指针 + int pos; + + que_front = que_rear = -1; // 设置队列为空队列 + que_rear++; + qu[que_rear].node = b; // 根结点指针进入队列 + qu[que_rear].parent = -1; // 根结点没有双亲结点 + + while(que_front < que_rear) // 队列不为空时循环 + { + que_front++; + b = qu[que_front].node; // 队头出队列 + printf(" 从队头位置que_front = [%d], 出队元素[%c]\n", que_front, b->data); + if(b->lchild == NULL && b->rchild == NULL) // b为叶子结点 + { + /*-----------------输出叶子结点信息---------------------*/ + printf(" %c到根结点逆路径: \n", b->data); + pos = que_front; + printf(" 出队列位置 = %d: \n", pos); + printf(" %c的双亲parent = %d\n", b->data, qu[pos].parent); + /*------------------------------------------------------*/ + while(qu[pos].parent != -1) + { + printf("%c->", qu[pos].node->data); // 输出结点(叶子结点和非根结点) + pos = qu[pos].parent; // 更新pos的值 + } + printf("%c\n", qu[pos].node->data); // 最后输出根结点 + } + if(b->lchild != NULL) // 若有左孩子,将其进入队列 + { + que_rear++; + qu[que_rear].node = b->lchild; + qu[que_rear].parent = que_front; + } + if(b->rchild != NULL) // 若有右孩子,将其进入队列 + { + que_rear++; + qu[que_rear].node = b->rchild; + qu[que_rear].parent = que_front; + } + } +} + +int main(int argc, char *argv[]) +{ + BTNode *b; + ElemType path[MAX_SIZE]; + ElemType long_path[MAX_SIZE]; + int i; + int long_path_len = 0; + + create_btree(b, "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); + printf("二叉树b: "); + disp_btree(b); + printf("\n"); + printf("先序遍历方法:\n"); + all_path1(b, path, 0); + long_path1(b, path, 0, long_path, long_path_len); + printf(" 第一条最长逆路径长度: %d\n", long_path_len); + printf(" 第一条最长逆路径: "); + for(i = long_path_len - 1; i >= 0; i--) + printf("%c ", long_path[i]); + printf("\n"); + printf("后序非递归遍历方法:\n"); + all_path2(b); + printf("层次遍历方法:\n"); + all_path3(b); + + destroy_btree(b); + + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/8\343\200\20114\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" "b/2224020157/chapter_1/8\343\200\20114\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 0000000000000000000000000000000000000000..60d12dcd5902ba270080ca5c81781cbc3ef09b0b --- /dev/null +++ "b/2224020157/chapter_1/8\343\200\20114\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,56 @@ +#include +#include +#include +#include +#define INF 0x3f3f3f3f +using namespace std; +struct position{ + int x,y; +}room[5001]; +bool vis[5001]; +double low[5001]; +int n; +double len(int i,int j){ + return sqrt((double)(room[i].x-room[j].x)*(room[i].x-room[j].x)+(double)(room[i].y-room[j].y)*(room[i].y-room[j].y)); +} + +void prim(){ + for(int i=1;i<=n;i++){ + low[i]=len(1,i); + //cout<low[j]){ + minn=low[j]; + temp=j; + } + } + //cout<len(temp,j)){ + low[j]=len(temp,j); + } + } + } +} +int main(){ + scanf("%d",&n); + //cout< +#include +#define INF 32767 //定义∞ +#define Vmax 100 //最大顶点个数 +typedef char VType; //顶点字符型 +typedef int EType; //边,矩阵元素int型 + +typedef struct +{ + VType vexs[Vmax]; //顶点表:存图的所有顶点,比如abcd四个顶点,放入数组中 + EType edges[Vmax][Vmax]; //邻接矩阵—边表 值为0 1整数 + int Vnum, Enum; // 图的顶点数和边数 +}MGraph; +typedef struct node +{ + EType adjvex; //该边的关联点编号 + struct node *next; //指向下一条边的指针 + int weight; //该边的相关信息,如权值(用整型表示) +}EdgeNode;//边结点 +typedef struct +{ + VType vertex; + EdgeNode *firstedge; //指向第一个边结点 +}VertexNode;//顶点结点 +typedef struct +{ + VertexNode vexlist[Vmax];//顶点结点数组 + int vexnum,arcnum;//顶点数、边数 +}AdjGraph;//图的邻接表 +/*创建邻接矩阵*/ +void creatGraph(MGraph &G) +{ + int i,j,from,to; + printf("请输入顶点的个数和边的条数:\n"); + scanf("%d %d",&G.Vnum,&G.Enum); + getchar(); + printf("请输入顶点:\n"); + for(i=0;ivexlist[i].firstedge=NULL; //将邻接表中所有头结点的指针域置为空 + for(i=0;i=0;j--) //检查邻接矩阵中的每个元素 + if(G.edges[i][j]!=INF) + { + p=(EdgeNode *)malloc(sizeof(EdgeNode)); + p->adjvex=j; + p->next=g->vexlist[i].firstedge; //头插法插入结点p + g->vexlist[i].firstedge=p; + } + g->vexnum=G.Vnum; G.Enum=g->arcnum; + //输出邻接表 + printf("图G的邻接表:\n"); + for(i=0;ivexnum;i++) + { + p=g->vexlist[i].firstedge; + printf("%c->",G.vexs[i]); + while(p!=NULL) + { + printf("%d->",p->adjvex); + p=p->next; + } + printf("NULL\n"); + } +} + +void DestroyAdj(AdjGraph *&G) //销毁图的邻接表 +{ int i; + EdgeNode *pre,*p; + for (i=0;ivexnum;i++) //扫描所有的单链表 + { pre=G->vexlist[i].firstedge; //p指向第i个单链表的首结点 + if (pre!=NULL) + { p=pre->next; + while (p!=NULL) //释放第i个单链表的所有边结点 + { free(pre); + pre=p; p=p->next; + } + free(pre); + } + } + free(G); //释放头结点数组 +} + +int main() +{ + MGraph G; + AdjGraph *g; + creatGraph(G); //建立邻接矩阵 + + printf("将g转换为邻接表G\n"); + MatToList(G,g); //输出邻接表G + DestroyAdj(g); //销毁邻接表 +return 1; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/8\343\200\2018\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/2224020157/chapter_1/8\343\200\2018\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 0000000000000000000000000000000000000000..72229f992b814092fdaff050bff4b7342dcc8214 --- /dev/null +++ "b/2224020157/chapter_1/8\343\200\2018\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/2224020157/chapter_1/9\343\200\20116\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/2224020157/chapter_1/9\343\200\20116\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 0000000000000000000000000000000000000000..bd537ae18b73b69c9a93777685822c3d30703ab0 --- /dev/null +++ "b/2224020157/chapter_1/9\343\200\20116\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,154 @@ +#include +#include +#include +#define MAXSIZE 100 //线性表最大存储100个元素 + +#define ERROR 1 +#define OK 0 +typedef int Status; + +typedef struct { + int *elem; //存储基地址 + int length; //存储顺序表的长度 +}SqList; + +//创建一个空的顺序表 +Status InItList(SqList *L) +{ + L->elem = (int*)malloc(sizeof(int)*MAXSIZE); //动态创建顺序表 + if(!L->elem) exit(OVERFLOW); //存储分配失败退出 + L->length = 0; //顺序表的长度为0 + return OK; +} + +//顺序表的初始化 +Status CreatList(SqList *L) +{ + int i; + char c; + printf("请输入顺序表的内容:\n"); + for(i=0;ielem[i]); + c=getchar(); //来接受是否是回车 + L->length++; + } + return OK; +} + +//销毁顺序表 +Status DeletList(SqList *L) +{ + free(L->elem); + L->elem = NULL; + return OK; +} +//查找值为x的某个元素。若成功,返回x在表中的位置;不成功,返回-1。 +int FindList(SqList L) +{ + int i,e=0; + printf("请输入要查询的元素值:"); + scanf("%d",&e); + for(i=0;iL->length+1) //判断插入位置是否合法 + return ERROR; //不合法返回1 + for(a=L->length-1; a>=i-1;a--) + { + L->elem[a+1] = L->elem[a]; //将元素依次向后移动一位 + } + L->elem[i-1] = e; //将新元素插入进去 + L->length++; //L的长度加一 + return OK; +} + +Status DelList(SqList *L,int i) //i表示要删除的位置 +{ + int a,k=0; + if(i<1 || i>L->length) //判断位置是否合法 + return ERROR; //不合法返回1 + k=L->elem[i-1]; //将第i个元素的值赋值给k + for(a=i-1;alength;a++) + { + L->elem[a] = L->elem[a+1]; //将后面的元素依次向前挪动一位 + } + L->length--; //L的长度减一 + if(L->elem[i-1] == k) //若第i个位置元素依然等于k + { + printf("删除失败!\n"); + } + else + { + printf("删除成功,被删除的元素值为%d\n",k); + } + return OK; +} + +//顺序表的遍历 +Status ValList(SqList L) +{ + int i; + for(i=0;i + +//查找的目标元素 +#define X 15 + +//函数声明 +int BinarySearch(int* arr, int sz, int x); + +int main() +{ + //将需要查找目标元素的数字列表存入数组 + int arr[] = { 10,11,12,13,14,15,16,17,18,19,20 }; + + //求数组元素的个数 + int sz = sizeof(arr) / sizeof(arr[0]); + + //定义查找的目标元素 + int x = X; + + //数组、数组元素个数和目标元素 + int position = BinarySearch(arr, sz, x); + + //输出目标元素的位置 + if (position != -1) + { + printf("目标元素在第 %d 个位置", position); + } + else + { + printf("没有找到"); + } + + return 0; +} + +int BinarySearch(int* arr, int sz, int x) +{ + //左下标 + int left = 0; + + //右下标 + int right = sz; + + while (left <= right) + { + //中间元素下标 + int mid = (left + right) / 2; + + if (x > arr[mid]) + { + left = mid + 1; + } + else if (x < arr[mid]) + { + right = mid - 1; + } + else + { + return mid + 1; + } + } + + //没有找到 + return -1; +} \ No newline at end of file diff --git "a/2224020157/chapter_1/9\343\200\2019\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" "b/2224020157/chapter_1/9\343\200\2019\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" new file mode 100644 index 0000000000000000000000000000000000000000..7fc9027e46b8d1f18303409238d6ae1827518a47 --- /dev/null +++ "b/2224020157/chapter_1/9\343\200\2019\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" @@ -0,0 +1,63 @@ +#include +#include +#include + + +#define MAXWORD 100 + +typedef int KeyType; + +typedef struct tnode +{ + char ch;//字符 + int count;//出现次数 + struct tnode *lchild,*rchild; +}BTree; + + +void CreaTree(BTree *&p,char c) +{ + if(p==NULL)//p为NULL,则建立一个新结点 + { + p=(BTree *)malloc(sizeof(BTree)); + p->ch=c; + p->count=1; + p->lchild=p->rchild=NULL; + } + else if(c==p->ch) + p->count++; + else if(cch) + CreaTree(p->lchild,c); + else + CreaTree(p->rchild,c); +} + +void InOrder(BTree *p)//中序遍历BST +{ + if(p!=NULL) + { + InOrder(p->lchild);//中序遍历左子树 + printf("字符 %c 在本串中出现的次数:%d \n",p->ch,p->count);//访问根节点 + InOrder(p->rchild);//中序遍历右子树 + } +} + + +int main() +{ + BTree *root=NULL; + int i=0; + char str[MAXWORD]; + printf("\n"); + printf("输入字符串:"); + gets(str); + while(str[i]!='\0') + { + CreaTree(root,str[i]); + i++; + } + printf("各字符及出现次数:\n"); + InOrder(root); + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/2224020157/chapter_2/10.11\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" "b/2224020157/chapter_2/10.11\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7127b1140c2b6e4534ef7aebe770740f6888d60e --- /dev/null +++ "b/2224020157/chapter_2/10.11\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" @@ -0,0 +1,20 @@ +class Solution +{ +public: + vector GetLeastNumbers_Solution(vector input, int k) + { + vector ret; + if (input.empty() || k > input.size()) + return ret; + + make_heap(input.begin(), input.end(), greater()); + + for (int i = 0; i < k; i++) + { + pop_heap(input.begin(), input.end(), greater()); + ret.push_back(input.back()); + input.pop_back(); + } + return ret; + } +}; diff --git "a/2224020157/chapter_2/10.14\345\211\215\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" "b/2224020157/chapter_2/10.14\345\211\215\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..6af7db7a19dd0464e2e86d7ff6a66fcdee11d6e1 --- /dev/null +++ "b/2224020157/chapter_2/10.14\345\211\215\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" @@ -0,0 +1,32 @@ +class Solution { +public: + static bool cmp(pair& m, pair& n) { + return m.second > n.second; + } + + vector topKFrequent(vector& nums, int k) { + unordered_map occurrences; + for (auto& v : nums) { + occurrences[v]++; + } + + // pair 的第一个元素代表数组的值,第二个元素代表了该值出现的次数 + priority_queue, vector>, decltype(&cmp)> q(cmp); + for (auto& [num, count] : occurrences) { + if (q.size() == k) { + if (q.top().second < count) { + q.pop(); + q.emplace(num, count); + } + } else { + q.emplace(num, count); + } + } + vector ret; + while (!q.empty()) { + ret.emplace_back(q.top().first); + q.pop(); + } + return ret; + } +}; diff --git "a/2224020157/chapter_2/10.1\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" "b/2224020157/chapter_2/10.1\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 0000000000000000000000000000000000000000..c8ca323d64f0ef8fdc7afdf82ead80cebd155f1f --- /dev/null +++ "b/2224020157/chapter_2/10.1\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,12 @@ +class Solution { +public: + string restoreString(string s, vector& indices) { + int length = s.length(); + string result(length, 0); + + for(int i = 0; i < length; i++) { + result[indices[i]] = s[i]; + } + return result; + } +}; diff --git "a/2224020157/chapter_2/10.3\346\216\222\345\272\217\351\223\276\350\241\250.cpp" "b/2224020157/chapter_2/10.3\346\216\222\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d7b63515c37381235a02cc2e16dc48f6b1000bca --- /dev/null +++ "b/2224020157/chapter_2/10.3\346\216\222\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,47 @@ +class Solution { +public: + ListNode* sortList(ListNode* head) { + return sortList(head, nullptr); + } + + ListNode* sortList(ListNode* head, ListNode* tail) { + if (head == nullptr) { + return head; + } + if (head->next == tail) { + head->next = nullptr; + return head; + } + ListNode* slow = head, *fast = head; + while (fast != tail) { + slow = slow->next; + fast = fast->next; + if (fast != tail) { + fast = fast->next; + } + } + ListNode* mid = slow; + return merge(sortList(head, mid), sortList(mid, tail)); + } + + ListNode* merge(ListNode* head1, ListNode* head2) { + ListNode* dummyHead = new ListNode(0); + ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2; + while (temp1 != nullptr && temp2 != nullptr) { + if (temp1->val <= temp2->val) { + temp->next = temp1; + temp1 = temp1->next; + } else { + temp->next = temp2; + temp2 = temp2->next; + } + temp = temp->next; + } + if (temp1 != nullptr) { + temp->next = temp1; + } else if (temp2 != nullptr) { + temp->next = temp2; + } + return dummyHead->next; + } +}; diff --git "a/2224020157/chapter_2/10.4\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\342\205\241.cpp" "b/2224020157/chapter_2/10.4\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\342\205\241.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ec94d79c42ad662693033fe5f2501f60b30bf7be --- /dev/null +++ "b/2224020157/chapter_2/10.4\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\342\205\241.cpp" @@ -0,0 +1,23 @@ +class Solution { +public: + vector sortArrayByParityII(vector& nums) { + int n = nums.size(); + vector ans(n); + + int i = 0; + for (int x: nums) { + if (x % 2 == 0) { + ans[i] = x; + i += 2; + } + } + i = 1; + for (int x: nums) { + if (x % 2 == 1) { + ans[i] = x; + i += 2; + } + } + return ans; + } +}; diff --git "a/2224020157/chapter_2/10.8\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/2224020157/chapter_2/10.8\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 0000000000000000000000000000000000000000..9106286d2e242a8018ada08b8b8c3b11bf44326e --- /dev/null +++ "b/2224020157/chapter_2/10.8\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,9 @@ +class Solution { +public: + vector> kClosest(vector>& points, int k) { + sort(points.begin(), points.end(), [](const vector& u, const vector& v) { + return u[0] * u[0] + u[1] * u[1] < v[0] * v[0] + v[1] * v[1]; + }); + return {points.begin(), points.begin() + k}; + } +}; diff --git "a/2224020157/chapter_2/4.1\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" "b/2224020157/chapter_2/4.1\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0141857fb84c41572724010c5e7323b94a409cec --- /dev/null +++ "b/2224020157/chapter_2/4.1\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + bool isPalindrome(string s) { +int n; + string d; + for (char ch: s) { + if (isalnum(ch)) { + d += tolower(ch); + } + } + n = d.size(); + int l = 0, r = n - 1; + while (l < r) { + if (d[l] != d[r]) { + return false; + } + ++l; + --r; + } + return true; + } +}; \ No newline at end of file diff --git "a/2224020157/chapter_2/4.2\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200cpp.cpp" "b/2224020157/chapter_2/4.2\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200cpp.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..a7a70dd8fe0e9dc3a4e6fe59a37f72ca967c90f6 --- /dev/null +++ "b/2224020157/chapter_2/4.2\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200cpp.cpp" @@ -0,0 +1,15 @@ +class Solution { +public: + string longestCommonPrefix(vector& strs) { + int temp=0; + while(temp next(n); + next[0] = 0; + for(int i = 1; i < n; i++){ + while(s[i] != s[j] && j > 0){ + j = next[j - 1]; + } + if(s[i] == s[j]){ + j++; + } + next[i] = j; + } + if(next[n - 1] * 2 >= n && n % (n - next[n - 1]) == 0) + return 1; + else + return 0; + } +}; \ No newline at end of file diff --git "a/2224020157/chapter_2/5.1\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" "b/2224020157/chapter_2/5.1\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b8936f6a5945a375538e55c1a32db268a62836de --- /dev/null +++ "b/2224020157/chapter_2/5.1\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" @@ -0,0 +1,15 @@ +class Solution { +public: + int fib(int n) { + if (n < 2) { + return n; + } + int p = 0, q = 0, r = 1; + for (int i = 2; i <= n; ++i) { + p = q; + q = r; + r = p + q; + } + return r; + } +}; diff --git "a/2224020157/chapter_2/5.3\347\277\273\350\275\254\351\223\276\350\241\250.cpp" "b/2224020157/chapter_2/5.3\347\277\273\350\275\254\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..392a3ac6845149a8fc1b8a90f02ab34e8934715e --- /dev/null +++ "b/2224020157/chapter_2/5.3\347\277\273\350\275\254\351\223\276\350\241\250.cpp" @@ -0,0 +1,45 @@ +class Solution { +public: + // 翻转一个子链表,并且返回新的头与尾 + pair myReverse(ListNode* head, ListNode* tail) { + ListNode* prev = tail->next; + ListNode* p = head; + while (prev != tail) { + ListNode* nex = p->next; + p->next = prev; + prev = p; + p = nex; + } + return {tail, head}; + } + + ListNode* reverseKGroup(ListNode* head, int k) { + ListNode* hair = new ListNode(0); + hair->next = head; + ListNode* pre = hair; + + while (head) { + ListNode* tail = pre; + // 查看剩余部分长度是否大于等于 k + for (int i = 0; i < k; ++i) { + tail = tail->next; + if (!tail) { + return hair->next; + } + } + ListNode* nex = tail->next; + // 这里是 C++17 的写法,也可以写成 + // pair result = myReverse(head, tail); + // head = result.first; + // tail = result.second; + tie(head, tail) = myReverse(head, tail); + // 把子链表重新接回原链表 + pre->next = head; + tail->next = nex; + pre = tail; + head = tail->next; + } + + return hair->next; + } +}; diff --git "a/2224020157/chapter_2/5.7n\347\232\207\345\220\216.cpp" "b/2224020157/chapter_2/5.7n\347\232\207\345\220\216.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b890f907979228834b60f08f5d8f266238b1fffd --- /dev/null +++ "b/2224020157/chapter_2/5.7n\347\232\207\345\220\216.cpp" @@ -0,0 +1,52 @@ +class Solution { +public: + vector> solveNQueens(int n) { + auto solutions = vector>(); + auto queens = vector(n, -1); + auto columns = unordered_set(); + auto diagonals1 = unordered_set(); + auto diagonals2 = unordered_set(); + backtrack(solutions, queens, n, 0, columns, diagonals1, diagonals2); + return solutions; + } + + void backtrack(vector> &solutions, vector &queens, int n, int row, unordered_set &columns, unordered_set &diagonals1, unordered_set &diagonals2) { + if (row == n) { + vector board = generateBoard(queens, n); + solutions.push_back(board); + } else { + for (int i = 0; i < n; i++) { + if (columns.find(i) != columns.end()) { + continue; + } + int diagonal1 = row - i; + if (diagonals1.find(diagonal1) != diagonals1.end()) { + continue; + } + int diagonal2 = row + i; + if (diagonals2.find(diagonal2) != diagonals2.end()) { + continue; + } + queens[row] = i; + columns.insert(i); + diagonals1.insert(diagonal1); + diagonals2.insert(diagonal2); + backtrack(solutions, queens, n, row + 1, columns, diagonals1, diagonals2); + queens[row] = -1; + columns.erase(i); + diagonals1.erase(diagonal1); + diagonals2.erase(diagonal2); + } + } + } + + vector generateBoard(vector &queens, int n) { + auto board = vector(); + for (int i = 0; i < n; i++) { + string row = string(n, '.'); + row[queens[i]] = 'Q'; + board.push_back(row); + } + return board; + } +}; diff --git "a/2224020157/chapter_2/6.1\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.cpp" "b/2224020157/chapter_2/6.1\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1b43f6399161c5fb2fb939978cf7481fc012f805 --- /dev/null +++ "b/2224020157/chapter_2/6.1\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.cpp" @@ -0,0 +1,17 @@ +class Solution { +public: + int findMaxConsecutiveOnes(vector& nums) { + int maxCount = 0, count = 0; + int n = nums.size(); + for (int i = 0; i < n; i++) { + if (nums[i] == 1) { + count++; + } else { + maxCount = max(maxCount, count); + count = 0; + } + } + maxCount = max(maxCount, count); + return maxCount; + } +}; diff --git "a/2224020157/chapter_2/6.3\347\247\273\345\212\2500.cpp" "b/2224020157/chapter_2/6.3\347\247\273\345\212\2500.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b5324ec2d649b2eda7965a8dbb89b6486d46dc6b --- /dev/null +++ "b/2224020157/chapter_2/6.3\347\247\273\345\212\2500.cpp" @@ -0,0 +1,13 @@ +class Solution { +public: + void moveZeroes(vector& nums) { + int n = nums.size(), left = 0, right = 0; + while (right < n) { + if (nums[right]) { + swap(nums[left], nums[right]); + left++; + } + right++; + } + } +}; diff --git "a/2224020157/chapter_2/6.5\347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.cpp" "b/2224020157/chapter_2/6.5\347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..3eafd74ddc452c4d4ba1a45f490620134dd66108 --- /dev/null +++ "b/2224020157/chapter_2/6.5\347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.cpp" @@ -0,0 +1,14 @@ +class Solution { +public: + int diagonalSum(vector>& mat) { + int n = mat.size(), sum = 0; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (i == j || i + j == n - 1) { + sum += mat[i][j]; + } + } + } + return sum; + } +}; diff --git "a/2224020157/chapter_2/6.6\351\207\215\345\241\221\347\237\251\351\230\265.cpp" "b/2224020157/chapter_2/6.6\351\207\215\345\241\221\347\237\251\351\230\265.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bd70f2cdbc796b3fc3952f368438155646e427af --- /dev/null +++ "b/2224020157/chapter_2/6.6\351\207\215\345\241\221\347\237\251\351\230\265.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + vector> matrixReshape(vector>& nums, int r, int c) { + int m = nums.size(); + int n = nums[0].size(); + if (m * n != r * c) { + return nums; + } + + vector> ans(r, vector(c)); + for (int x = 0; x < m * n; ++x) { + ans[x / c][x % c] = nums[x / n][x % n]; + } + return ans; + } +}; diff --git "a/2224020157/chapter_2/7.11\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" "b/2224020157/chapter_2/7.11\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b881091958508c31ff39f0743dcc0be9002479dc --- /dev/null +++ "b/2224020157/chapter_2/7.11\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" @@ -0,0 +1,20 @@ +class Solution { +public: + int maxDepth(TreeNode* root) { + if (root == nullptr) return 0; + queue Q; + Q.push(root); + int ans = 0; + while (!Q.empty()) { + int sz = Q.size(); + while (sz > 0) { + TreeNode* node = Q.front();Q.pop(); + if (node->left) Q.push(node->left); + if (node->right) Q.push(node->right); + sz -= 1; + } + ans += 1; + } + return ans; + } +}; diff --git "a/2224020157/chapter_2/7.14\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" "b/2224020157/chapter_2/7.14\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..f936adbbc8792e1fdf9cc267329c09cac11af3c5 --- /dev/null +++ "b/2224020157/chapter_2/7.14\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" @@ -0,0 +1,25 @@ +class Solution { +public: + void dfs(vector& res, TreeNode* root, int curHeight) { + if (curHeight == res.size()) { + res.push_back(root->val); + } else { + res[curHeight] = max(res[curHeight], root->val); + } + if (root->left) { + dfs(res, root->left, curHeight + 1); + } + if (root->right) { + dfs(res, root->right, curHeight + 1); + } + } + + vector largestValues(TreeNode* root) { + if (!root) { + return {}; + } + vector res; + dfs(res, root, 0); + return res; + } +}; diff --git "a/2224020157/chapter_2/7.18\350\267\257\345\276\204\346\200\273\345\222\214.cpp" "b/2224020157/chapter_2/7.18\350\267\257\345\276\204\346\200\273\345\222\214.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b818638dfe0e14799774f3f3285ff65f3e847098 --- /dev/null +++ "b/2224020157/chapter_2/7.18\350\267\257\345\276\204\346\200\273\345\222\214.cpp" @@ -0,0 +1,33 @@ +class Solution { +public: + bool hasPathSum(TreeNode *root, int sum) { + if (root == nullptr) { + return false; + } + queue que_node; + queue que_val; + que_node.push(root); + que_val.push(root->val); + while (!que_node.empty()) { + TreeNode *now = que_node.front(); + int temp = que_val.front(); + que_node.pop(); + que_val.pop(); + if (now->left == nullptr && now->right == nullptr) { + if (temp == sum) { + return true; + } + continue; + } + if (now->left != nullptr) { + que_node.push(now->left); + que_val.push(now->left->val + temp); + } + if (now->right != nullptr) { + que_node.push(now->right); + que_val.push(now->right->val + temp); + } + } + return false; + } +}; diff --git "a/2224020157/chapter_2/7.21\344\273\216\345\205\210\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.cpp" "b/2224020157/chapter_2/7.21\344\273\216\345\205\210\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7c996f3ad55971651a73140e0e74ea1e92841387 --- /dev/null +++ "b/2224020157/chapter_2/7.21\344\273\216\345\205\210\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.cpp" @@ -0,0 +1,28 @@ +class Solution { +private: + unordered_map index; + +public: + TreeNode* myBuildTree(const vector& preorder, const vector& inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) { + if (preorder_left > preorder_right) { + return nullptr; + } + + int preorder_root = preorder_left; + int inorder_root = index[preorder[preorder_root]]; + + TreeNode* root = new TreeNode(preorder[preorder_root]); + int size_left_subtree = inorder_root - inorder_left; + root->left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1); + root->right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right); + return root; + } + + TreeNode* buildTree(vector& preorder, vector& inorder) { + int n = preorder.size(); + for (int i = 0; i < n; ++i) { + index[inorder[i]] = i; + } + return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1); + } +}; diff --git "a/2224020157/chapter_2/7.27N\345\217\211\346\240\221\347\232\204\345\205\210\346\240\271\351\201\215\345\216\206.cpp" "b/2224020157/chapter_2/7.27N\345\217\211\346\240\221\347\232\204\345\205\210\346\240\271\351\201\215\345\216\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..afd14c11479652ce9c5ff438b948c9575dbcf901 --- /dev/null +++ "b/2224020157/chapter_2/7.27N\345\217\211\346\240\221\347\232\204\345\205\210\346\240\271\351\201\215\345\216\206.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + void helper(const Node* root, vector & res) { + if (root == nullptr) { + return; + } + res.emplace_back(root->val); + for (auto & ch : root->children) { + helper(ch, res); + } + } + + vector preorder(Node* root) { + vector res; + helper(root, res); + return res; + } +}; diff --git "a/2224020157/chapter_2/7.3\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" "b/2224020157/chapter_2/7.3\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..133ae27326ce82efe9fcf18f4cf5ae3d2b4e5847 --- /dev/null +++ "b/2224020157/chapter_2/7.3\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + void helper(const Node* root, vector & res) { + if (root == nullptr) { + return; + } + for (auto & ch : root->children) { + helper(ch, res); + } + res.emplace_back(root->val); + } + + vector postorder(Node* root) { + vector res; + helper(root, res); + return res; + } +}; diff --git "a/2224020157/chapter_2/7.4\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206.cpp" "b/2224020157/chapter_2/7.4\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e9eb21c6be6fec68de23a2944acc9965be4ce65f --- /dev/null +++ "b/2224020157/chapter_2/7.4\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206.cpp" @@ -0,0 +1,28 @@ +class Solution { +public: + vector> levelOrder(Node* root) { + if (!root) { + return {}; + } + + vector> ans; + queue q; + q.push(root); + + while (!q.empty()) { + int cnt = q.size(); + vector level; + for (int i = 0; i < cnt; ++i) { + Node* cur = q.front(); + q.pop(); + level.push_back(cur->val); + for (Node* child: cur->children) { + q.push(child); + } + } + ans.push_back(move(level)); + } + + return ans; + } +}; diff --git "a/2224020157/chapter_2/7.8\344\272\214\345\217\211\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/2224020157/chapter_2/7.8\344\272\214\345\217\211\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 0000000000000000000000000000000000000000..3838601415b7f62d6bf0fe142bde0b85b74ddbac --- /dev/null +++ "b/2224020157/chapter_2/7.8\344\272\214\345\217\211\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,35 @@ +class Solution { +public: + vector getPath(TreeNode* root, TreeNode* target) { + vector path; + TreeNode* node = root; + while (node != target) { + path.push_back(node); + if (target->val < node->val) { + node = node->left; + } + else { + node = node->right; + } + } + path.push_back(node); + return path; + } + + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + vector path_p = getPath(root, p); + vector path_q = getPath(root, q); + TreeNode* ancestor; + for (int i = 0; i < path_p.size() && i < path_q.size(); ++i) { + if (path_p[i] == path_q[i]) { + ancestor = path_p[i]; + } + else { + break; + } + } + return ancestor; + } +}; + + diff --git "a/2224020157/chapter_2/8.12\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/2224020157/chapter_2/8.12\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 0000000000000000000000000000000000000000..47ee460a505e0bdf91b9865d7e5a5b5ef76fa9ba --- /dev/null +++ "b/2224020157/chapter_2/8.12\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,67 @@ +class DisjointSetUnion { +private: + vector f, rank; + int n; + +public: + DisjointSetUnion(int _n) { + n = _n; + rank.resize(n, 1); + f.resize(n); + for (int i = 0; i < n; i++) { + f[i] = i; + } + } + + int find(int x) { + return f[x] == x ? x : f[x] = find(f[x]); + } + + int unionSet(int x, int y) { + int fx = find(x), fy = find(y); + if (fx == fy) { + return false; + } + if (rank[fx] < rank[fy]) { + swap(fx, fy); + } + rank[fx] += rank[fy]; + f[fy] = fx; + return true; + } +}; + +struct Edge { + int len, x, y; + Edge(int len, int x, int y) : len(len), x(x), y(y) { + } +}; + +class Solution { +public: + int minCostConnectPoints(vector>& points) { + auto dist = [&](int x, int y) -> int { + return abs(points[x][0] - points[y][0]) + abs(points[x][1] - points[y][1]); + }; + int n = points.size(); + DisjointSetUnion dsu(n); + vector edges; + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + edges.emplace_back(dist(i, j), i, j); + } + } + sort(edges.begin(), edges.end(), [](Edge a, Edge b) -> int { return a.len < b.len; }); + int ret = 0, num = 1; + for (auto& [len, x, y] : edges) { + if (dsu.unionSet(x, y)) { + ret += len; + num++; + if (num == n) { + break; + } + } + } + return ret; + } +}; diff --git "a/2224020157/chapter_2/8.17\350\257\276\347\250\213\350\241\250.cpp" "b/2224020157/chapter_2/8.17\350\257\276\347\250\213\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c2eaea2b9ee1e634031c4547c6465d99350c0d89 --- /dev/null +++ "b/2224020157/chapter_2/8.17\350\257\276\347\250\213\350\241\250.cpp" @@ -0,0 +1,38 @@ +class Solution { +private: + vector> edges; + vector visited; + bool valid = true; + +public: + void dfs(int u) { + visited[u] = 1; + for (int v: edges[u]) { + if (visited[v] == 0) { + dfs(v); + if (!valid) { + return; + } + } + else if (visited[v] == 1) { + valid = false; + return; + } + } + visited[u] = 2; + } + + bool canFinish(int numCourses, vector>& prerequisites) { + edges.resize(numCourses); + visited.resize(numCourses); + for (const auto& info: prerequisites) { + edges[info[1]].push_back(info[0]); + } + for (int i = 0; i < numCourses && valid; ++i) { + if (!visited[i]) { + dfs(i); + } + } + return valid; + } +}; diff --git "a/2224020157/chapter_2/8.1\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" "b/2224020157/chapter_2/8.1\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e7dfee4179ae2ca4b854e45e56de6ed3d5f9383b --- /dev/null +++ "b/2224020157/chapter_2/8.1\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + int findJudge(int n, vector>& trust) { + vector inDegrees(n + 1); + vector outDegrees(n + 1); + for (auto& edge : trust) { + int x = edge[0], y = edge[1]; + ++inDegrees[y]; + ++outDegrees[x]; + } + for (int i = 1; i <= n; ++i) { + if (inDegrees[i] == n - 1 && outDegrees[i] == 0) { + return i; + } + } + return -1; + } +}; diff --git "a/2224020157/chapter_2/8.5\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" "b/2224020157/chapter_2/8.5\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..f7a798c47229f34b0b95b81b02033e572973aa8a --- /dev/null +++ "b/2224020157/chapter_2/8.5\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.cpp" @@ -0,0 +1,38 @@ +class Solution { +private: + static constexpr int UNCOLORED = 0; + static constexpr int RED = 1; + static constexpr int GREEN = 2; + vector color; + bool valid; + +public: + void dfs(int node, int c, const vector>& graph) { + color[node] = c; + int cNei = (c == RED ? GREEN : RED); + for (int neighbor: graph[node]) { + if (color[neighbor] == UNCOLORED) { + dfs(neighbor, cNei, graph); + if (!valid) { + return; + } + } + else if (color[neighbor] != cNei) { + valid = false; + return; + } + } + } + + bool isBipartite(vector>& graph) { + int n = graph.size(); + valid = true; + color.assign(n, UNCOLORED); + for (int i = 0; i < n && valid; ++i) { + if (color[i] == UNCOLORED) { + dfs(i, RED, graph); + } + } + return valid; + } +}; diff --git "a/2224020157/chapter_2/8.7\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" "b/2224020157/chapter_2/8.7\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" new file mode 100644 index 0000000000000000000000000000000000000000..474b20e4e1cafb00ebb8fc577e3f330f32965e3a --- /dev/null +++ "b/2224020157/chapter_2/8.7\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" @@ -0,0 +1,33 @@ +class Solution { +public: + int shortestPathBinaryMatrix(vector>& grid) { + if (grid[0][0] == 1) { + return -1; + } + int n = grid.size(); + vector> dist(n, vector(n, INT_MAX)); + queue> q; + q.push({0, 0}); + dist[0][0] = 1; + while (!q.empty()) { + auto [x, y] = q.front(); + q.pop(); + if (x == n - 1 && y == n - 1) { + return dist[x][y]; + } + for (int dx = -1; dx <= 1; dx++) { + for (int dy = -1; dy <= 1; dy++) { + if (x + dx < 0 || x + dx >= n || y + dy < 0 || y + dy >= n) { // 越界 + continue; + } + if (grid[x + dx][y + dy] == 1 || dist[x + dx][y + dy] <= dist[x][y] + 1) { // 单元格值不为 0 或已被访问 + continue; + } + dist[x + dx][y + dy] = dist[x][y] + 1; + q.push({x + dx, y + dy}); + } + } + } + return -1; + } +}; diff --git "a/2224020157/chapter_2/9.13\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/2224020157/chapter_2/9.13\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 0000000000000000000000000000000000000000..329f48eea0c3f270e96e10f4902791fbd456a33f --- /dev/null +++ "b/2224020157/chapter_2/9.13\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,91 @@ +#include +#include +#define maxlen 50 +typedef struct tree{ + struct tree *lchild,*rchild,*parent; + char data; +}CStree,*CTree; +typedef struct{ + CStree *Zx[maxlen]; + int top; +}Z_list; +CStree *Q[maxlen]; +CStree *Ctree(){ + int front=1; + int rear=0; + char ch; + CStree *T,*S;//T:根节点;S:创建 + T=NULL; + ch=getchar(); + while(ch!='#'){//#终止符 + S=NULL; + if(ch!='@'){//@虚指针 + S=(CTree)malloc(sizeof(CStree)); + S->data=ch; + S->lchild=NULL; + S->rchild=NULL; + } + rear++;Q[rear]=S; + if(rear==1){ + T=S; + }else{ + if(S!=NULL && Q[front]!=NULL){ + if(rear%2==0){ + Q[front]->lchild=Q[rear]; + }else{ + Q[front]->rchild=Q[rear]; + } + Q[rear]->parent=Q[front]; + } + if(rear%2==1) front++; + } + ch=getchar(); + } + getchar(); + return T; +} +CStree *C_Z(char a){ + int i=1; + for(;idata==a){ + return Q[i]; + } + } +} +Z_list *find_par(Z_list *z, CStree *T){ + while(T->parent!=NULL){ + z->top++; + z->Zx[z->top]=T->parent; + T=T->parent; + } + return z; +} +void Z_X(CStree *T){ + Z_list *z1=(Z_list*)malloc(sizeof(Z_list));z1->top=-1; + Z_list *z2=(Z_list*)malloc(sizeof(Z_list));z2->top=-1; + CStree *L1=(CTree)malloc(sizeof(CStree)); + CStree *L2=(CTree)malloc(sizeof(CStree)); + char a1,a2;//输入两个节点 + scanf("%c %c",&a1,&a2); + getchar(); + L1=C_Z(a1);//查找a1节点 + L2=C_Z(a2);//查找a2节点 + z1=find_par(z1,L1);//建立节点1的祖先栈 + z2=find_par(z2,L2);//建立节点2的祖先栈 + while((z1->Zx[z1->top])==(z2->Zx[z2->top])){ + if(z1->top==0 || z2->top==0 ){ + break; + } + if((z1->Zx[z1->top-1]->data)!=(z2->Zx[z2->top-1]->data)) + break; + z1->top--; + z2->top--; + } + printf("公共祖先:%c\n",z1->Zx[z1->top]->data); +} +int main(){ + CStree *T; + T=Ctree(); + Z_X(T); + return 0; +} diff --git "a/2224020157/chapter_2/9.16\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" "b/2224020157/chapter_2/9.16\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2c17c2efc9ae5efbdac46ab1cbe71e2d35433c61 --- /dev/null +++ "b/2224020157/chapter_2/9.16\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + int height(TreeNode* root) { + if (root == NULL) { + return 0; + } else { + return max(height(root->left), height(root->right)) + 1; + } + } + + bool isBalanced(TreeNode* root) { + if (root == NULL) { + return true; + } else { + return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right); + } + } +}; diff --git "a/2224020157/chapter_2/9.19\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" "b/2224020157/chapter_2/9.19\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..cdf05e8bff7dff2a51ab94be13ac6692bd21dae0 --- /dev/null +++ "b/2224020157/chapter_2/9.19\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" @@ -0,0 +1,12 @@ +class Solution { +public: + int thirdMax(vector &nums) { + sort(nums.begin(), nums.end(), greater<>()); + for (int i = 1, diff = 1; i < nums.size(); ++i) { + if (nums[i] != nums[i - 1] && ++diff == 3) { // 此时 nums[i] 就是第三大的数 + return nums[i]; + } + } + return nums[0]; + } +}; diff --git "a/2224020157/chapter_2/9.20\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" "b/2224020157/chapter_2/9.20\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bf2b9e5164729b7e9981f473356e586b3c663112 --- /dev/null +++ "b/2224020157/chapter_2/9.20\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" @@ -0,0 +1,42 @@ +class MyHashSet { +private: + vector> data; + static const int base = 769; + static int hash(int key) { + return key % base; + } +public: + /** Initialize your data structure here. */ + MyHashSet(): data(base) {} + + void add(int key) { + int h = hash(key); + for (auto it = data[h].begin(); it != data[h].end(); it++) { + if ((*it) == key) { + return; + } + } + data[h].push_back(key); + } + + void remove(int key) { + int h = hash(key); + for (auto it = data[h].begin(); it != data[h].end(); it++) { + if ((*it) == key) { + data[h].erase(it); + return; + } + } + } + + /** Returns true if this set contains the specified element */ + bool contains(int key) { + int h = hash(key); + for (auto it = data[h].begin(); it != data[h].end(); it++) { + if ((*it) == key) { + return true; + } + } + return false; + } +}; diff --git "a/2224020157/chapter_2/9.22\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/2224020157/chapter_2/9.22\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 0000000000000000000000000000000000000000..29054f1b43befd4a9b9f484e6094b4a37b63dc57 --- /dev/null +++ "b/2224020157/chapter_2/9.22\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,21 @@ +class Solution { +public: + int quickselect(vector &nums, int l, int r, int k) { + if (l == r) + return nums[k]; + int partition = nums[l], i = l - 1, j = r + 1; + while (i < j) { + do i++; while (nums[i] < partition); + do j--; while (nums[j] > partition); + if (i < j) + swap(nums[i], nums[j]); + } + if (k <= j)return quickselect(nums, l, j, k); + else return quickselect(nums, j + 1, r, k); + } + + int findKthLargest(vector &nums, int k) { + int n = nums.size(); + return quickselect(nums, 0, n - 1, n - k); + } +}; diff --git "a/2224020157/chapter_2/9.23\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" "b/2224020157/chapter_2/9.23\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" new file mode 100644 index 0000000000000000000000000000000000000000..0c2249f3d77f3c450bc4d8d9ef811c90d11cdbff --- /dev/null +++ "b/2224020157/chapter_2/9.23\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" @@ -0,0 +1,49 @@ + +class RandomizedSet { +private: + unordered_map hash;//哈希实现删除 + vector v;//动态数组实现插入和随机访问 +public: + /** Initialize your data structure here. */ + RandomizedSet() { + + } + + /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ + //当元素 val 不存在时,向集合中插入该项。 + bool insert(int val) { + if(hash.find(val) != hash.end()) return false; //如果集合中已经存在val,返回false, + v.push_back(val);//否则插入到数组末尾 + hash[val] = v.size() - 1;// + return true; + } + + /** Removes a value from the set. Returns true if the set contained the specified element. */ + //元素 val 存在时,从集合中移除该项。 + bool remove(int val) { + if(hash.find(val) == hash.end()) return false;//如果集合中不存在val,返回false + int lastPos = v.size() - 1;//数组最后一个元素位置 + int valPos = hash[val];//将被删除值和数组最后一位进行交换 + v[valPos] = v[lastPos]; + v.pop_back();//删除 + hash[v[valPos]] = valPos;//被交换的值下标发生变化,需要更新 + hash.erase(val); //哈希表中删除val的项 + return true; + } + + /** Get a random element from the set. */ + //随机返回现有集合中的一项。每个元素应该有相同的概率被返回。 + int getRandom() { + int size = v.size(); + int pos = rand() % size;//对下标产生随机数 + return v[pos];//数组可以根据下表返回 + } +}; + +/** + * Your RandomizedSet object will be instantiated and called as such: + * RandomizedSet* obj = new RandomizedSet(); + * bool param_1 = obj->insert(val); + * bool param_2 = obj->remove(val); + * int param_3 = obj->getRandom(); + */ diff --git "a/2224020157/chapter_2/9.2\344\272\214\345\210\206\346\237\245\346\211\276.cpp" "b/2224020157/chapter_2/9.2\344\272\214\345\210\206\346\237\245\346\211\276.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8c071d971e796b431fddc398214970b55791ebc0 --- /dev/null +++ "b/2224020157/chapter_2/9.2\344\272\214\345\210\206\346\237\245\346\211\276.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + int search(vector& nums, int target) { + int left = 0, right = nums.size() - 1; + while(left <= right){ + int mid = (right - left) / 2 + left; + int num = nums[mid]; + if (num == target) { + return mid; + } else if (num > target) { + right = mid - 1; + } else { + left = mid + 1; + } + } + return -1; + } +}; diff --git "a/2224020157/chapter_2/9.8\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" "b/2224020157/chapter_2/9.8\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" new file mode 100644 index 0000000000000000000000000000000000000000..1019554fbb8a36c1ff22012a1cf58dc30b41783e --- /dev/null +++ "b/2224020157/chapter_2/9.8\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" @@ -0,0 +1,53 @@ + +#include +#include +using namespace std; + +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#define min(a,b) (((a) < (b)) ? (a) : (b)) + +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + int n = nums1.size(); + int m = nums2.size(); + + if (n > m) //保证数组1一定最短 + { + return findMedianSortedArrays(nums2, nums1); + } + + // Ci 为第i个数组的割,比如C1为2时表示第1个数组只有2个元素。LMaxi为第i个数组割后的左元素。RMini为第i个数组割后的右元素。 + int LMax1, LMax2, RMin1, RMin2, c1, c2, lo = 0, hi = 2 * n; //我们目前是虚拟加了'#'所以数组1是2*n长度 + + while (lo <= hi) //二分 + { + c1 = (lo + hi) / 2; //c1是二分的结果 + c2 = m + n - c1; + + LMax1 = (c1 == 0) ? INT_MIN : nums1[(c1 - 1) / 2]; + RMin1 = (c1 == 2 * n) ? INT_MAX : nums1[c1 / 2]; + LMax2 = (c2 == 0) ? INT_MIN : nums2[(c2 - 1) / 2]; + RMin2 = (c2 == 2 * m) ? INT_MAX : nums2[c2 / 2]; + + if (LMax1 > RMin2) + hi = c1 - 1; + else if (LMax2 > RMin1) + lo = c1 + 1; + else + break; + } + return (max(LMax1, LMax2) + min(RMin1, RMin2)) / 2.0; + } +}; + + +int main(int argc, char *argv[]) +{ + vector nums1 = { 2,3, 5 }; + vector nums2 = { 1,4,7, 9 }; + + Solution solution; + double ret = solution.findMedianSortedArrays(nums1, nums2); + return 0; +}