From 20da5696b6848d538c48801cf61c39bf0a62e7f5 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:04:01 +0000 Subject: [PATCH 01/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...5\346\225\264\346\225\260\345\222\214.cpp" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\2321\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" diff --git "a/2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\2321\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" "b/2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\2321\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" new file mode 100644 index 00000000..94ab40d0 --- /dev/null +++ "b/2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\2321\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" @@ -0,0 +1,33 @@ +#include +#include +#include +//求1+2+.....+n +long add1(long n) +{ + long i,sum=0; + for(i=1;i<=n;i++) + { + sum+=i; + return sum; + } +} +Viod AddTime1(long n) +{ + clock_t t; + long sum; + t=clock(); + sum=add1(n); + t=clock()-t; + printf("方法1:\n"); + printf(" 结果:1~%d之和:%d\n",n,sum); + printf(" 用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); +} +int main() +{ + int n; + print("n(大于1000000):"); + sacnf("%d",&n); + if(n<1000000)return 0; + AddTime1(n); + return 1; +} \ No newline at end of file -- Gitee From 5e548905339d95d4f4e9fc56da639601e4f04a47 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:06:18 +0000 Subject: [PATCH 02/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...0\347\232\204\344\270\252\346\225\260.cpp" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\232\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" diff --git "a/2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\232\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\232\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" new file mode 100644 index 00000000..249fb3d4 --- /dev/null +++ "b/2209040062/chapter1/\350\257\276\345\240\202\344\275\234\344\270\232\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" @@ -0,0 +1,33 @@ +#include +#include +#include +bool prime1(long n) +{ + long i; + for(i=2;i Date: Fri, 8 Dec 2023 08:07:49 +0000 Subject: [PATCH 03/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...215\345\244\215\345\205\203\347\264\240.cpp" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 "2209040062/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" diff --git "a/2209040062/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" "b/2209040062/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..92e2d2da --- /dev/null +++ "b/2209040062/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" @@ -0,0 +1,17 @@ +struct ListNode* deleteDuplicates(struct ListNode* head){ + struct ListNode* p=malloc(sizeof(struct ListNode)); + if(!head)return head;//非空链表才会执行下边 + p=head;//p指针指向头,返回的是头也是p + while(head)//遍历直到他为空 + { + if(head->next&&head->val==head->next->val)//本结点和下一个结点相同则跳过它 + { + head->next=head->next->next; + + } + else + head=head->next;//如果不想等则我继续下一个结点 + } + + return p; +} \ No newline at end of file -- Gitee From d6d79d7642c8daafe252e0ac0d56c8faef95c798 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:08:27 +0000 Subject: [PATCH 04/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...17\215\350\275\254\351\223\276\350\241\250.cpp" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 "2209040062/chapter2/\345\217\215\350\275\254\351\223\276\350\241\250.cpp" diff --git "a/2209040062/chapter2/\345\217\215\350\275\254\351\223\276\350\241\250.cpp" "b/2209040062/chapter2/\345\217\215\350\275\254\351\223\276\350\241\250.cpp" new file mode 100644 index 00000000..c658ab2c --- /dev/null +++ "b/2209040062/chapter2/\345\217\215\350\275\254\351\223\276\350\241\250.cpp" @@ -0,0 +1,14 @@ +struct ListNode* reverseList(struct ListNode* head){ + struct ListNode*cur=head; + struct ListNode*newhead=NULL; + while(cur) + { + struct ListNode* temp=cur->next; + //头插 + cur->next = newhead; + newhead = cur; + //迭代 + cur = temp; + } + return newhead; +} \ No newline at end of file -- Gitee From bf074b2db7ce1a7e1e7098d95ba6d178a2ed9296 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:09:22 +0000 Subject: [PATCH 05/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...0\347\232\204\350\277\220\347\256\227.cpp" | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 "2209040062/chapter2/ \345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" diff --git "a/2209040062/chapter2/ \345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" "b/2209040062/chapter2/ \345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" new file mode 100644 index 00000000..00329ce9 --- /dev/null +++ "b/2209040062/chapter2/ \345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" @@ -0,0 +1,225 @@ +#include +#include +#define MAX 100 //多项式最多项数 +typedef struct +{ + double coef; //系数 + int exp; //指数 +}PolyArray; //存放多项式的数组类型 + +typedef struct pnode +{ + double coef; //系数 + int exp; //指数 + struct pnode* next; +}PolyNode; //声明多项式单链表结点类型 + +void CreatePolyR(PolyNode*& L, PolyArray a[], int n) //尾插法建表 +{ + PolyNode* s, * r; + int i; + L = (PolyNode*)malloc(sizeof(PolyNode)); + L->next = NULL; + r = L; //r始终指向尾结点,开始时指向头结点 + for (i = 0; i < n; i++) + { + s = (PolyNode*)malloc(sizeof(PolyNode)); //创建新结点 + s->coef = a[i].coef; + s->exp = a[i].exp; + r->next = s; //将结点s插入到结点r之后 + r = s; + } + r->next = NULL; //尾结点next域置为NULL +} + +void DispPoly(PolyNode* L) //输出多项式单链表 +{ + PolyNode* p = L->next; + while (p != NULL) + { + if (p->coef > 0) printf("+");//系数为正,用+连接 + if (p->exp == 0) printf("%g", p->coef); //指数为0,只输出系数 + else if (p->exp == 1) printf("%gx", p->coef); //指数为1 + else printf("%gx^%d", p->coef, p->exp); //指数不为1 + p = p->next; + } + printf("\n"); +} + +void DestoryPoly(PolyNode*& L) //销毁多项式单链表 +{ + PolyNode* pre = L, * p = pre->next; + while (p != NULL) + { + free(pre); + pre = p; + p = pre->next; + } + free(pre); +} + +void Sort(PolyNode*& L) //将多项式单链表按指数递减排序 +{ + PolyNode* p = L->next, * pre, * q; + if (p != NULL) //L有一个或以上的数据结点 + { + q = p->next; //q保存p结点的后继节点 + p->next = NULL; //构造只含一个数据结点的有序表 + p = q; + while (p != NULL) //扫描原L中余下的数据结点 + { + q = p->next; //q保存p结点的后继节点 + pre = L; + while (pre->next != NULL && pre->next->exp > p->exp) + pre = pre->next; //在有序表中找插入节点p的前驱结点pre + p->next = pre->next; //将结点p插入到结点pre之后 + pre->next = p; + p = q; //扫描原单链表余下的结点 + } + } +} + +void Add(PolyNode* ha, PolyNode* hb, PolyNode*& hc) //ha和hb相加得到hc +{ + PolyNode* pa = ha->next, * pb = hb->next, * s, * r; + double c; + hc = (PolyNode*)malloc(sizeof(PolyNode)); + r = hc; //r指向尾结点,初始时指向头结点 + while (pa != NULL && pb != NULL) //pa、pb均没有扫描完 + { + if (pa->exp > pb->exp) //将指数较大的pa结点复制到hc中 + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pa->exp; + s->coef = pa->coef; + r->next = s; + r = s; + pa = pa->next; + } + else if(pa->exp < pb->exp) //将指数较大的pb结点复制到hc中 + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pb->exp; + s->coef = pb->coef; + r->next = s; + r = s; + pb = pb->next; + } + else //pa、pb结点的指数相等时 + { + c = pa->coef + pb->coef; //求两个结点的系数和c + if (c != 0) //若系数和不为0时创建新结点 + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pa->exp; + s->coef = c; + r->next = s; + r = s; + } + pa = pa->next; //pa后移一个结点 + pb = pb->next; //pb后移一个结点 + } + } + if (pb != NULL) pa = pb; //复制余下的结点 + while (pa != NULL) + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pa->exp; + s->coef = pa->coef; + r->next = s; + r = s; + pa = pa->next; + } + r->next = NULL; //尾结点next设置为空 +} + +void Sub(PolyNode* ha, PolyNode* hb, PolyNode*& hd) //ha和hb相加得到hc +{ + PolyNode* pa = ha->next, * pb = hb->next, * s, * r; + double c; + hd = (PolyNode*)malloc(sizeof(PolyNode)); + r = hd; //r指向尾结点,初始时指向头结点 + while (pa != NULL && pb != NULL) //pa、pb均没有扫描完 + { + if (pa->exp > pb->exp) //将指数较大的pa结点复制到hc中 + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pa->exp; + s->coef = pa->coef; + r->next = s; + r = s; + pa = pa->next; + } + else if (pa->exp < pb->exp) //将指数较大的pb结点复制到hc中 + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pb->exp; + s->coef = 0-pb->coef; + r->next = s; + r = s; + pb = pb->next; + } + else //pa、pb结点的指数相等时 + { + c = pa->coef - pb->coef; //求两个结点的系数和c + if (c != 0) //若系数相减不为0时创建新结点 + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pa->exp; + s->coef = c; + r->next = s; + r = s; + } + pa = pa->next; //pa后移一个结点 + pb = pb->next; //pb后移一个结点 + } + } + if (pb != NULL) pa = pb; //复制余下的结点 + while (pa != NULL) + { + s = (PolyNode*)malloc(sizeof(PolyNode)); + s->exp = pa->exp; + s->coef = pa->coef; + r->next = s; + r = s; + pa = pa->next; + } + r->next = NULL; //尾结点next设置为空 +} + +int main() +{ + PolyNode* ha, * hb, * hc, * hd; + PolyArray a[] = { {3,1},{2,0},{1,3},{4,4} }; + PolyArray b[] = { {3,4},{2,3},{1,0},{5,2},{ 3,6} }; + CreatePolyR(ha, a, 4); + CreatePolyR(hb, b, 5); + printf("原多项式A:"); + DispPoly(ha); + printf("\n"); + printf("原多项式B:"); + DispPoly(hb); + printf("\n"); + Sort(ha); + Sort(hb); + printf("有序多项式A:"); + DispPoly(ha); + printf("\n"); + printf("有序多项式B:"); + DispPoly(hb); + printf("\n"); + + Add(ha, hb, hc); + printf("多项式相加:"); + DispPoly(hc); + printf("\n"); + + Sub(ha, hb, hd); + printf("多项式相减:"); + DispPoly(hd); + printf("\n"); + /*DestoryPoly(ha); + DestoryPoly(hb); + DestoryPoly(hc);*/ + return 0; +} \ No newline at end of file -- Gitee From e49d5669919509734e0127d06eb06c4cc1bc7198 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:10:19 +0000 Subject: [PATCH 06/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...1\345\237\272\345\207\206\345\210\206.cpp" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 "2209040062/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\206.cpp" diff --git "a/2209040062/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\206.cpp" "b/2209040062/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\206.cpp" new file mode 100644 index 00000000..2acc2eb8 --- /dev/null +++ "b/2209040062/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\206.cpp" @@ -0,0 +1,95 @@ +#include +#include +#include + +typedef int ElemType; +typedef struct LNode +{ + ElemType data; + struct LNode* next; +}LinkNode; + +bool DivideList(LinkNode*& L, ElemType x); //L为传入的链表头结点,x为传入的基准 +bool InitList(LinkNode*& L); //链表产生 +bool DispList(LinkNode* L); //链表输出 + +int main(int argc, const char* argv[]) +{ + LinkNode* L = NULL; + int x; + bool flag = false; + InitList(L); + printf("请输入基准。\n"); + scanf("%d", &x); + flag = DivideList(L, x); + if (flag) + printf("算法执行成功。\n"); + else + printf("算法执行失败。\n"); + DispList(L); + return 0; +} + +bool InitList(LinkNode*& L) //建立单链表 +{ + while (!L) { + L = (LinkNode*)malloc(sizeof(LNode)); + } + L->next = NULL; + int i, n; + LinkNode* p = NULL, * q = NULL; + q = L; + printf("请输入数据规模:\n"); + scanf("%d", &n); + printf("请输入数据:\n"); + for (i = 0; i < n; i++) { + while (!p) { + p = (LinkNode*)malloc(sizeof(LNode)); + } + scanf("%d", &p->data); + q->next = p; + q = p; + p = q->next = NULL; + } + return true; +} +bool DispList(LinkNode* L) //输出单链表 +{ + LinkNode* p = L->next; + while (p) { + printf("\t%d", p->data); + p = p->next; + } + printf("\n"); + return true; +} +bool DivideList(LinkNode*& L, ElemType x) //L为传入的链表头结点,x为传入的基准 +{ + if (L->next==NULL) { + printf("单链表不存在。\n"); + return false; + } + LinkNode* p = NULL, * q = NULL, * r = NULL; //p为工作指针,q为小于基准的数的头结点,r为尾指针 + p = L; + //直到申请成功,因为malloc可能申请失败 + while (!q) { + q = (LinkNode*)malloc(sizeof(LinkNode)); + } + r = q; //r指向小于基准的链表的尾巴 + //p从第一个有效结点开始扫描整个链表,直到p后无结点为空 + while (p->next) { + if (p->next->data < x) { + r->next = p->next; //将p接到尾巴后面 + r=r->next; //尾巴后移 + p->next= p->next->next; //将该结点从L中截取出去 + r->next = NULL; //将尾巴与原链表之间的关系切断 + } + else + p = p->next; //p后移 + } + //将小于基准的链表接到大于等于基准的链表的前面 + r->next = L->next; //小于基准的链表的尾巴与大于等于基准的链表相连 + L->next = q->next; //将整体接到头结点后 + free(q); //释放小于基准的链表的头结点 + return true; +} \ No newline at end of file -- Gitee From 0c9fe305880e5b10a61fab265ee0d1e8657276e5 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:11:42 +0000 Subject: [PATCH 07/12] tex Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...1\347\256\227\345\231\250\342\205\241.cpp" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "2209040062/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" diff --git "a/2209040062/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" "b/2209040062/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" new file mode 100644 index 00000000..dbf23ea6 --- /dev/null +++ "b/2209040062/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" @@ -0,0 +1,48 @@ +class Solution { +public: + int calculate(string s) { + stack operands; //存储表达式中的数值 + stack operators; //存储表达式中的运算符 + int index = 0; + s.erase(remove(s.begin(), s.end(), ' '), s.end()); + while(index < s.size()){ + if(isdigit(s[index])){ + int digit = 0; + for(;index < s.size() && isdigit(s[index]);index++) + digit = 10 * digit + (s[index] - '0'); + operands.push(digit); + } else { + while(operators.size() && !priority(s[index],operators.top())) + calc(operands,operators); + operators.push(s[index]); + index++; + } + } + while(operators.size()) + calc(operands,operators); + return operands.top(); + } + + void calc(stack& operands,stack& operators){ + int result; + int rhs = operands.top();operands.pop();//右操作数 + int lhs = operands.top();operands.pop();//左操作数 + switch(operators.top()){ + case '+':result = lhs + rhs; break; + case '-':result = lhs - rhs; break; + case '*':result = lhs * rhs; break; + case '/':result = lhs / rhs; break; + default: ; + } + operators.pop(); //计算完成后,该运算符要弹栈 + operands.push(result); //将新计算出来的结果入栈 + return; + } + bool priority(int a,int b){ + if(a == '+' || a == '-') + return false; + else if(a=='*' || a=='/') + return (b=='+' || b == '-'); + return false; + } +}; \ No newline at end of file -- Gitee From f942d8eabbcc22a662a8b119f2d705b5b38f83e7 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:12:31 +0000 Subject: [PATCH 08/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- .../\346\234\200\345\260\217\346\240\210.cpp" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "2209040062/chapter3/\346\234\200\345\260\217\346\240\210.cpp" diff --git "a/2209040062/chapter3/\346\234\200\345\260\217\346\240\210.cpp" "b/2209040062/chapter3/\346\234\200\345\260\217\346\240\210.cpp" new file mode 100644 index 00000000..532509b1 --- /dev/null +++ "b/2209040062/chapter3/\346\234\200\345\260\217\346\240\210.cpp" @@ -0,0 +1,41 @@ +#include +class MinStack { +public: + stack st; + stack minStack; + + /** 构造函数清空栈容器 */ + MinStack() { + while(!st.empty()) { + st.pop(); + } + while(!minStack.empty()) { + minStack.pop(); + } + /* 初始化最小栈的栈顶元素为最大值为了防止top访问空指针报错 */ + minStack.push(INT_MAX); + } + + void push(int x) { + st.push(x); + /* 比较最小栈的栈顶的值和当前值val的大小,将最小值押入最小栈也就是记录了当前st栈的最小值为栈顶元素 */ + int minVal = std::min(minStack.top(), x); + /* 将最小值押入最小栈 */ + minStack.push(minVal); + } + + void pop() { + /* 弹出两个栈的栈顶元素 */ + st.pop(); + minStack.pop(); + } + + int top() { + return st.top(); + } + + int min() { + /* 取最小栈的栈顶元素就是此时st栈的最小值 */ + return minStack.top(); + } +}; \ No newline at end of file -- Gitee From 4af810ec79070b6c6cfa2f91ec42013e741bc9f3 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:13:15 +0000 Subject: [PATCH 09/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...0\347\232\204\346\213\254\345\217\267.cpp" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "2209040062/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" diff --git "a/2209040062/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040062/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" new file mode 100644 index 00000000..4c34b0e9 --- /dev/null +++ "b/2209040062/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" @@ -0,0 +1,42 @@ +#include +let isValid = function(s) { + let stack = [], length = s.length; + if(length % 2) return false; + for(let item of s){ + switch(item){ + case "{": + case "[": + case "(": + stack.push(item); + break; + case "}": + if(stack.pop() !== "{") return false; + break; + case "]": + if(stack.pop() !== "[") return false; + break; + case ")": + if(stack.pop() !== "(") return false; + break; + } + } + return !stack.length; +}; + +// 解法二 +var isValid = function(s) { + s = s.split(''); + let sl = s.length; + if (sl % 2) return false; + let map = new Map([[')', '('], [']', '['], ['}', '{']]); + let stack = []; + for(let i of s){ + if (map.get(i)) { + if (stack[stack.length - 1] !== map.get(i)) return false; + else stack.pop(); + } else { + stack.push(i); + } + } + return !stack.length; +}; \ No newline at end of file -- Gitee From dd742f085bbfd8c4027b388b164714e0102363ce Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:14:12 +0000 Subject: [PATCH 10/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...6\347\216\260\351\230\237\345\210\227.cpp" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "2209040062/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" diff --git "a/2209040062/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" "b/2209040062/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" new file mode 100644 index 00000000..df73e5c4 --- /dev/null +++ "b/2209040062/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" @@ -0,0 +1,81 @@ +typedef struct { + int* stk; + int stkSize; + int stkCapacity; +} Stack; + +Stack* stackCreate(int cpacity) { + Stack* ret = malloc(sizeof(Stack)); + ret->stk = malloc(sizeof(int) * cpacity); + ret->stkSize = 0; + ret->stkCapacity = cpacity; + return ret; +} + +void stackPush(Stack* obj, int x) { + obj->stk[obj->stkSize++] = x; +} + +void stackPop(Stack* obj) { + obj->stkSize--; +} + +int stackTop(Stack* obj) { + return obj->stk[obj->stkSize - 1]; +} + +bool stackEmpty(Stack* obj) { + return obj->stkSize == 0; +} + +void stackFree(Stack* obj) { + free(obj->stk); +} + +typedef struct { + Stack* inStack; + Stack* outStack; +} MyQueue; + +MyQueue* myQueueCreate() { + MyQueue* ret = malloc(sizeof(MyQueue)); + ret->inStack = stackCreate(100); + ret->outStack = stackCreate(100); + return ret; +} + +void in2out(MyQueue* obj) { + while (!stackEmpty(obj->inStack)) { + stackPush(obj->outStack, stackTop(obj->inStack)); + stackPop(obj->inStack); + } +} + +void myQueuePush(MyQueue* obj, int x) { + stackPush(obj->inStack, x); +} + +int myQueuePop(MyQueue* obj) { + if (stackEmpty(obj->outStack)) { + in2out(obj); + } + int x = stackTop(obj->outStack); + stackPop(obj->outStack); + return x; +} + +int myQueuePeek(MyQueue* obj) { + if (stackEmpty(obj->outStack)) { + in2out(obj); + } + return stackTop(obj->outStack); +} + +bool myQueueEmpty(MyQueue* obj) { + return stackEmpty(obj->inStack) && stackEmpty(obj->outStack); +} + +void myQueueFree(MyQueue* obj) { + stackFree(obj->inStack); + stackFree(obj->outStack); +} \ No newline at end of file -- Gitee From 482e567e58f689b2133312563c5ae00f71337664 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:15:29 +0000 Subject: [PATCH 11/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...7\345\256\236\347\216\260\346\240\210.cpp" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 "2209040062/chapter3/ \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" diff --git "a/2209040062/chapter3/ \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2209040062/chapter3/ \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" new file mode 100644 index 00000000..3c2f54f7 --- /dev/null +++ "b/2209040062/chapter3/ \347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" @@ -0,0 +1,98 @@ +#define LEN 20 +typedef struct queue { + int *data; + int head; + int rear; + int size; +} Queue; + +typedef struct { + Queue *queue1, *queue2; +} MyStack; + +Queue *initQueue(int k) { + Queue *obj = (Queue *)malloc(sizeof(Queue)); + obj->data = (int *)malloc(k * sizeof(int)); + obj->head = -1; + obj->rear = -1; + obj->size = k; + return obj; +} + +void enQueue(Queue *obj, int e) { + if (obj->head == -1) { + obj->head = 0; + } + obj->rear = (obj->rear + 1) % obj->size; + obj->data[obj->rear] = e; +} + +int deQueue(Queue *obj) { + int a = obj->data[obj->head]; + if (obj->head == obj->rear) { + obj->rear = -1; + obj->head = -1; + return a; + } + obj->head = (obj->head + 1) % obj->size; + return a; +} + +int isEmpty(Queue *obj) { + return obj->head == -1; +} + +MyStack *myStackCreate() { + MyStack *obj = (MyStack *)malloc(sizeof(MyStack)); + obj->queue1 = initQueue(LEN); + obj->queue2 = initQueue(LEN); + return obj; +} + +void myStackPush(MyStack *obj, int x) { + if (isEmpty(obj->queue1)) { + enQueue(obj->queue2, x); + } else { + enQueue(obj->queue1, x); + } +} + +int myStackPop(MyStack *obj) { + if (isEmpty(obj->queue1)) { + while (obj->queue2->head != obj->queue2->rear) { + enQueue(obj->queue1, deQueue(obj->queue2)); + } + return deQueue(obj->queue2); + } + while (obj->queue1->head != obj->queue1->rear) { + enQueue(obj->queue2, deQueue(obj->queue1)); + } + return deQueue(obj->queue1); +} + +int myStackTop(MyStack *obj) { + if (isEmpty(obj->queue1)) { + return obj->queue2->data[obj->queue2->rear]; + } + return obj->queue1->data[obj->queue1->rear]; +} + +bool myStackEmpty(MyStack *obj) { + if (obj->queue1->head == -1 && obj->queue2->head == -1) { + return true; + } + return false; +} + +void myStackFree(MyStack *obj) { + free(obj->queue1->data); + obj->queue1->data = NULL; + free(obj->queue1); + obj->queue1 = NULL; + free(obj->queue2->data); + obj->queue2->data = NULL; + free(obj->queue2); + obj->queue2 = NULL; + free(obj); + obj = NULL; +} \ No newline at end of file -- Gitee From 3a4278518efe351b172335574a2d5b392dfc9d06 Mon Sep 17 00:00:00 2001 From: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> Date: Fri, 8 Dec 2023 08:16:07 +0000 Subject: [PATCH 12/12] text Signed-off-by: jdz <13453644+jiang-dingzhuo@user.noreply.gitee.com> --- ...6\345\274\217\346\261\202\345\200\274.cpp" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "2209040062/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" diff --git "a/2209040062/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040062/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" new file mode 100644 index 00000000..cae9fe41 --- /dev/null +++ "b/2209040062/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" @@ -0,0 +1,64 @@ +#include +int myCompare(char *p) +{ + if(strlen(p)>1) + { + return -1; + } + int res=-1; + for(int i=0;i<4;i++) + { + if(*p==set[i]) + { + res=i; + return res; + } + } + return res; +} + +int result(int x1,int x2,char k) +{ + if(k=='+') + { + return x2+x1; + } + else if(k=='-') + { + return x2-x1; + } + else if(k=='*') + { + return x2*x1; + } + else if(k=='/'&&x1!=0) + { + + return x2/x1; + } + else return -1; +} +int evalRPN(char ** tokens, int tokensSize){ + int* stack=malloc(sizeof(int)*tokensSize); + int top=0; + for(int i=0;i