diff --git "a/2209040074/chapter 1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214" "b/2209040074/chapter 1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214" new file mode 100644 index 0000000000000000000000000000000000000000..ffd8e00e007c5bfc913b87dff938b8749d15a45e --- /dev/null +++ "b/2209040074/chapter 1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214" @@ -0,0 +1,16 @@ +class Solution { +public: + int sumOddLengthSubarrays(vector& arr) { + int sum = 0; + int n = arr.size(); + for (int start = 0; start < n; start++) { + for (int length = 1; start + length <= n; length += 2) { + int end = start + length - 1; + for (int i = start; i <= end; i++) { + sum += arr[i]; + } + } + } + return sum; + } +}; \ No newline at end of file diff --git "a/2209040074/chapter 1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214" "b/2209040074/chapter 1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214" new file mode 100644 index 0000000000000000000000000000000000000000..c20a2922e2778e90b62fe70c9cca16259726db74 --- /dev/null +++ "b/2209040074/chapter 1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214" @@ -0,0 +1,9 @@ +#include +#include +long add1(long n) +{ + long i,sum=0; + for(i=1;i<=n;i++) + sum+=i; + return sum; +} \ No newline at end of file diff --git "a/2209040074/chapter 1/\346\261\202\347\264\240\346\225\260\344\270\252\346\225\260" "b/2209040074/chapter 1/\346\261\202\347\264\240\346\225\260\344\270\252\346\225\260" new file mode 100644 index 0000000000000000000000000000000000000000..b9f53cd8e79bf6235ae010b84b9d4b8ce3c84350 --- /dev/null +++ "b/2209040074/chapter 1/\346\261\202\347\264\240\346\225\260\344\270\252\346\225\260" @@ -0,0 +1,23 @@ +#include +#include + +int main() +{ + int a, b, n, i, k = 0, f; + scanf("%d %d", &a, &b); + for (n = a; n <= b; n++) +{ + f = 1; + for (i = 2; i <= sqrt(n); i++) + if (n % i == 0) { + f = 0; + break; + } + if (f == 1) + k++; + if (n == b) + printf("%d", k); + +} + +} \ No newline at end of file diff --git a/2209040074/chapter 10/lc1528 b/2209040074/chapter 10/lc1528 new file mode 100644 index 0000000000000000000000000000000000000000..49e9eeb80f0b090c76ebc9f14f6812676e475091 --- /dev/null +++ b/2209040074/chapter 10/lc1528 @@ -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; + } +}; \ No newline at end of file diff --git a/2209040074/chapter 2/lc24 b/2209040074/chapter 2/lc24 new file mode 100644 index 0000000000000000000000000000000000000000..7d39648fec611f9bcf34701c2c921f09549542de --- /dev/null +++ b/2209040074/chapter 2/lc24 @@ -0,0 +1,12 @@ +class Solution { +public: + ListNode* swapPairs(ListNode* head) { + if (head == nullptr || head->next == nullptr) { + return head; + } + ListNode* newHead = head->next; + head->next = swapPairs(newHead->next); + newHead->next = head; + return newHead; + } +}; \ No newline at end of file diff --git a/2209040074/chapter 2/lc82 b/2209040074/chapter 2/lc82 new file mode 100644 index 0000000000000000000000000000000000000000..e25fba7cb798584d06fe60244db207b691657a86 --- /dev/null +++ b/2209040074/chapter 2/lc82 @@ -0,0 +1,25 @@ +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + if (!head) { + return head; + } + + ListNode* dummy = new ListNode(0, head); + + ListNode* cur = dummy; + while (cur->next && cur->next->next) { + if (cur->next->val == cur->next->next->val) { + int x = cur->next->val; + while (cur->next && cur->next->val == x) { + cur->next = cur->next->next; + } + } + else { + cur = cur->next; + } + } + + return dummy->next; + } +}; \ No newline at end of file diff --git a/2209040074/chapter 2/lc92 b/2209040074/chapter 2/lc92 new file mode 100644 index 0000000000000000000000000000000000000000..003b785e04f733dba39072c4999d237c1d82b217 --- /dev/null +++ b/2209040074/chapter 2/lc92 @@ -0,0 +1,42 @@ +class Solution { +private: + void reverseLinkedList(ListNode *head) { + ListNode *pre = nullptr; + ListNode *cur = head; + + while (cur != nullptr) { + ListNode *next = cur->next; + cur->next = pre; + pre = cur; + cur = next; + } + } + +public: + ListNode *reverseBetween(ListNode *head, int left, int right) { + ListNode *dummyNode = new ListNode(-1); + dummyNode->next = head; + + ListNode *pre = dummyNode; + for (int i = 0; i < left - 1; i++) { + pre = pre->next; + } + + ListNode *rightNode = pre; + for (int i = 0; i < right - left + 1; i++) { + rightNode = rightNode->next; + } + + ListNode *leftNode = pre->next; + ListNode *curr = rightNode->next; + + pre->next = nullptr; + rightNode->next = nullptr; + + reverseLinkedList(leftNode); + + pre->next = rightNode; + leftNode->next = curr; + return dummyNode->next; + } +}; \ No newline at end of file diff --git "a/2209040074/chapter 2/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204" "b/2209040074/chapter 2/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204" new file mode 100644 index 0000000000000000000000000000000000000000..488b9613056a1f7a17eee5fb334ddcf2645f60d8 --- /dev/null +++ "b/2209040074/chapter 2/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204" @@ -0,0 +1,11 @@ +void merge(vector& nums1, int m, vector& nums2, int n) { + int i = nums1.size() - 1; + m--; + n--; + while (n >= 0) { + while (m >= 0 && nums1[m] > nums2[n]) { + swap(nums1[i--], nums1[m--]); + } + swap(nums1[i--], nums2[n--]); + } +} \ No newline at end of file diff --git "a/2209040074/chapter 2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225" "b/2209040074/chapter 2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225" new file mode 100644 index 0000000000000000000000000000000000000000..176c63f0af8a348084188422ede4c0ac251232b2 --- /dev/null +++ "b/2209040074/chapter 2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225" @@ -0,0 +1,159 @@ +#include +#include +typedef char ElemType; +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LinkNode; +void InitList(LinkNode *&L)//初始化单链表 1) +{ + L=(LinkNode *)malloc(sizeof(LinkNode)); + L->next=NULL; +} +bool ListInsert(LinkNode *&L,int i,ElemType e)//插入数据元素 2) +{ + int j=0; + LinkNode *p=L,*s; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + s=(LinkNode *)malloc(sizeof(LinkNode)); + s->data=e; + s->next=p->next; + p->next=s; + return true; + } +} +void DispList(LinkNode *L)//输出线性表 3) 9) 11) +{ + LinkNode *p=L->next; + while(p!=NULL) + { + printf("%c",p->data); + p=p->next; + } + printf("\n"); +} +int ListLength(LinkNode *L)//求线性表的长度 4) +{ + int n=0; + LinkNode *p=L; + while (p->next!=NULL) + { + n++; + p=p->next; + } + return (n); +} +bool ListEmpty(LinkNode *L)//判断单链表是否为空5) +{ + return (L->next==NULL); +} +bool GetElem(LinkNode *L,int i,ElemType &e)//某个数据元素值6) +{ + int j=0; + LinkNode *p=L; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + e=p->data; + return true; + } +} +int LocateElem(LinkNode *L,ElemType e)//元素的位置7) +{ + int i=1; + LinkNode *p=L->next; + while(p!=NULL&&p->data!=e) + { + p=p->next; + i++; + } + if(p==NULL) + return (0); + else + return (i); +} +bool ListDelete(LinkNode *&L,int i,ElemType &e) +{ + int j=0; + LinkNode *p=L,*q; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + q=p->next; + if(q==NULL) + return false; + e=q->data; + p->next=q->next; + free(q); + return true; + } +} +void DestroyList(LinkNode *&L)//释放单链表12) +{ + LinkNode *pre=L,*p=L->next; + while(p!=NULL) + { + free(pre); + pre=p; + p=pre->next; + } + free(pre); +} +int main() +{ + LinkNode *h; + ElemType e; + printf("单链表的基本运算如下:\n"); + printf("(1)初始化单链表h.\n"); + InitList(h); + printf("(2)依次采用尾插法插入a,b,c,d,e元素.\n"); + ListInsert(h,1,'a'); + ListInsert(h,2,'b'); + ListInsert(h,3,'c'); + ListInsert(h,4,'d'); + ListInsert(h,5,'e'); + printf("(3)输出单链表h:"); + DispList(h); + printf("(4)输出单链表h的长度:%d\n",ListLength(h)); + printf("(5)判断单链表h是否为空:%s\n",(ListEmpty(h)?"空":"非空")); + GetElem(h,3,e); + printf("(6)输出单链表h的第3个元素:%c\n",e); + printf("(7)输出元素a的位置:%d\n",LocateElem(h,'a')); + printf("(8)在第4个元素位置上插入f元素.\n"); + ListInsert(h,4,'f'); + printf("(9)输出单链表h:"); + DispList(h); + printf("(10)删除单链表h的第3个元素.\n"); + ListDelete(h,3,e); + printf("(11)输出单链表h:"); + DispList(h); + printf("(12)释放单链表h\n"); + DestroyList(h); +} \ No newline at end of file diff --git "a/2209040074/chapter 2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206" "b/2209040074/chapter 2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206" new file mode 100644 index 0000000000000000000000000000000000000000..2acc2eb8c696914125a8e459b17e34a358c9ff48 --- /dev/null +++ "b/2209040074/chapter 2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206" @@ -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 diff --git "a/2209040074/chapter 2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240" "b/2209040074/chapter 2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240" new file mode 100644 index 0000000000000000000000000000000000000000..649d20e4251acb80c00c7baf8d2516c87e0c3059 --- /dev/null +++ "b/2209040074/chapter 2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240" @@ -0,0 +1,9 @@ +public class Solution { + public ListNode RemoveElements(ListNode head, int val) { + if (head == null) { + return head; + } + head.next = RemoveElements(head.next, val); + return head.val == val ? head.next : head; + } +} \ No newline at end of file diff --git a/2209040074/chapter 3/lc155 b/2209040074/chapter 3/lc155 new file mode 100644 index 0000000000000000000000000000000000000000..b5e744075e7221764d15b2158ebdf68b8f1dca65 --- /dev/null +++ b/2209040074/chapter 3/lc155 @@ -0,0 +1,26 @@ +class MinStack { + stack x_stack; + stack min_stack; +public: + MinStack() { + min_stack.push(INT_MAX); + } + + void push(int x) { + x_stack.push(x); + min_stack.push(min(min_stack.top(), x)); + } + + void pop() { + x_stack.pop(); + min_stack.pop(); + } + + int top() { + return x_stack.top(); + } + + int getMin() { + return min_stack.top(); + } +}; \ No newline at end of file diff --git a/2209040074/chapter 3/lc20 b/2209040074/chapter 3/lc20 new file mode 100644 index 0000000000000000000000000000000000000000..3d9588b011c846715699fa9411ee08130d1187cc --- /dev/null +++ b/2209040074/chapter 3/lc20 @@ -0,0 +1,28 @@ +class Solution { +public: + bool isValid(string s) { + int n = s.size(); + if (n % 2 == 1) { + return false; + } + + unordered_map pairs = { + {')', '('}, + {']', '['}, + {'}', '{'} + }; + stack stk; + for (char ch: s) { + if (pairs.count(ch)) { + if (stk.empty() || stk.top() != pairs[ch]) { + return false; + } + stk.pop(); + } + else { + stk.push(ch); + } + } + return stk.empty(); + } +}; diff --git a/2209040074/chapter 4/lc125 b/2209040074/chapter 4/lc125 new file mode 100644 index 0000000000000000000000000000000000000000..205a0ea14b568707521a5ffca3b3fb80148a12e4 --- /dev/null +++ b/2209040074/chapter 4/lc125 @@ -0,0 +1,13 @@ +class Solution { +public: + bool isPalindrome(string s) { + string sgood; + for (char ch: s) { + if (isalnum(ch)) { + sgood += tolower(ch); + } + } + string sgood_rev(sgood.rbegin(), sgood.rend()); + return sgood == sgood_rev; + } +}; \ No newline at end of file diff --git a/2209040074/chapter 5/lc509 b/2209040074/chapter 5/lc509 new file mode 100644 index 0000000000000000000000000000000000000000..e480bc4ca65e459f1c9a4d923c97c8e6b5473918 --- /dev/null +++ b/2209040074/chapter 5/lc509 @@ -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; + } +}; \ No newline at end of file diff --git a/2209040074/chapter 6/lc485 b/2209040074/chapter 6/lc485 new file mode 100644 index 0000000000000000000000000000000000000000..98da5756d7f709fe7d13c871217ce1b6579961e4 --- /dev/null +++ b/2209040074/chapter 6/lc485 @@ -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; + } +}; \ No newline at end of file diff --git a/2209040074/chapter 7/lc145 b/2209040074/chapter 7/lc145 new file mode 100644 index 0000000000000000000000000000000000000000..edc1192f63c97a284646c4c171e8660af89ba595 --- /dev/null +++ b/2209040074/chapter 7/lc145 @@ -0,0 +1,17 @@ +class Solution { +public: + void postorder(TreeNode *root, vector &res) { + if (root == nullptr) { + return; + } + postorder(root->left, res); + postorder(root->right, res); + res.push_back(root->val); + } + + vector postorderTraversal(TreeNode *root) { + vector res; + postorder(root, res); + return res; + } +}; diff --git a/2209040074/chapter 8/lc997 b/2209040074/chapter 8/lc997 new file mode 100644 index 0000000000000000000000000000000000000000..634fcff02106efdffad26c193000d64464f82973 --- /dev/null +++ b/2209040074/chapter 8/lc997 @@ -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; + } +}; \ No newline at end of file diff --git a/2209040074/chapter 9/lc704 b/2209040074/chapter 9/lc704 new file mode 100644 index 0000000000000000000000000000000000000000..d9d306ea59d99540590566bcf63d9281b20edf9d --- /dev/null +++ b/2209040074/chapter 9/lc704 @@ -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; + } +}; \ No newline at end of file