diff --git "a/2203030044/chapter_1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2203030044/chapter_1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..f3181473a6bc0b0839eb8657c44a44a6b81e8391 --- /dev/null +++ "b/2203030044/chapter_1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" @@ -0,0 +1,19 @@ +int* twoSum(int* nums, int numsSize, int target, int* returnSize) +{ +int i=0,j=0; +int *a=(int*)malloc(sizeof(int)*2); +for(i=0;i +#include +#include +long i,sum=0; +long add1(long n) +{ + for (i=1;i<=n;i++) + sum=sum+i; + return sum; +} + void addtime1(long n) + { + clock_t t; + long sum; + t=clock(); + sum=add1(n); + printf("方法1:\n"); + printf("结果:1~%ld之和:%ld\n",n,sum); + printf("用时:%lf\n",((float)t)/CLOCKS_PER_SEC); + } + long add2(long n) + { + return n*(n+1)/2; + + } + void addtime2(long n) + { + clock_t t; + long sum; + t=clock(); + sum=add2(n); + t=clock()-t; + printf("方法:\n"); + printf("结果:1~%ld之和:%ld\n",n,sum); + printf("用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); + } + int main() + { + int n; + printf("n(大于1000000):"); + scanf("%d",&n); + if(n<1000000) return 0; + addtime1(n); + addtime2(n); + return 1; + } \ No newline at end of file diff --git "a/2203030044/chapter_1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2203030044/chapter_1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bc291682ae5f6999f7063c5f0ea58d57e1570396 --- /dev/null +++ "b/2203030044/chapter_1/\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 topKFrequent(vector& nums, int k) { + unordered_mapm; + for (int i = 0; i < nums.size(); i++) { + m[nums[i]]++; + } + struct cmp { + bool operator()(pair& p1, pair& p2) { + return p1.second > p2.second; + } + }; + priority_queue, vector >, cmp>q; + for (auto &a : m) { + q.push(a); + if (q.size() > k) { + q.pop(); + } + } + + vectoranswer; + while (!q.empty()) { + answer.push_back(q.top().first); + q.pop(); + } + return answer; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_10/\345\256\236\351\252\21412.cpp" "b/2203030044/chapter_10/\345\256\236\351\252\21412.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2974de9685c6cda044a9c3f05a1b96ddaf0302a9 --- /dev/null +++ "b/2203030044/chapter_10/\345\256\236\351\252\21412.cpp" @@ -0,0 +1,31 @@ +#include +using namespace std; + +typedef struct student { + char name[20]; + int age, score; +}student; + +student s[105]; + +bool compare(student s1, student s2) { + if (s1.score != s2.score) + return s1.score > s2.score; + else if (strcmp(s1.name, s2.name) != 0) + return strcmp(s1.name, s2.name) < 0; + else + return s1.age < s2.age; +} + +int main() { + int n; + cin >> n; + for (int i = 0; i < n; ++i) { + cin >> s[i].name >> s[i].age >> s[i].score; + } + sort(s, s + n, compare); + for (int i = 0; i < n; ++i) { + cout << s[i].name << ' ' << s[i].age << ' ' << s[i].score << endl; + } + return 0; +} diff --git "a/2203030044/chapter_10/\345\256\236\351\252\2144.cpp" "b/2203030044/chapter_10/\345\256\236\351\252\2144.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0842d504de13739fd46cf643bfa3826d035a00bd --- /dev/null +++ "b/2203030044/chapter_10/\345\256\236\351\252\2144.cpp" @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +vectorarr = { 5,4,3,6,1,2 }; + +void bubblesort(int n){ + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + swap(arr[j], arr[j + 1]); + } + } + } +} +int main() { + int length = arr.size(); + cout << "原先的顺序为:"; + for (int i = 0; i < length; i++) { + cout << arr[i] << ' '; + } + cout << endl; + bubblesort(length); + cout << "排序后的顺序为:"; + for (int i = 0; i < length; i++) { + cout << arr[i] << ' '; + } + return 0; +} diff --git "a/2203030044/chapter_10/\345\256\236\351\252\2145.cpp" "b/2203030044/chapter_10/\345\256\236\351\252\2145.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..f3b5574a593bccb591bf336ffab309fd2c26d855 --- /dev/null +++ "b/2203030044/chapter_10/\345\256\236\351\252\2145.cpp" @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +vectorarr = { 5,4,3,6,1,2 }; + +void quicksort(int left,int right){ + if (left >= right) { + return; + } + int i = left, j = right, base = arr[left]; + while (i < j) { + while (arr[j] >= base && i < j) { + j--; + } + while (arr[i] <= base && i < j) { + i++; + } + if (i < j) { + swap(arr[i], arr[j]); + } + } + arr[left] = arr[i]; + arr[i] = base; + quicksort(left, i - 1); + quicksort(i + 1, right); +} + +int main() { + int length = arr.size(); + cout << "原先的顺序为:"; + for (int i = 0; i < length; i++) { + cout << arr[i] << ' '; + } + cout << endl; + quicksort(0, length - 1); + cout << "排序后的顺序为:"; + for (int i = 0; i < length; i++) { + cout << arr[i] << ' '; + } + return 0; +} diff --git "a/2203030044/chapter_10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.cpp" "b/2203030044/chapter_10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e4e3553de1623c1b6f8a4f7c52a1d3f831e83b45 --- /dev/null +++ "b/2203030044/chapter_10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.cpp" @@ -0,0 +1,28 @@ +class Solution { +public: + vector sortArrayByParityII(vector& nums) { + vectorarr; + //4 2 5 7 + //5 2 4 7 + int indexa = 0;//奇数 + int indexb = 0;//偶数 + for (int i = 0; i < nums.size(); i++) { + if (!(i % 2) || i == 0) { + while (nums[indexb] % 2) { + indexb++; + } + arr.push_back(nums[indexb]); + indexb++; + + } + if (i % 2) { + while (!(nums[indexa] % 2)) { + indexa++; + } + arr.push_back(nums[indexa]); + indexa++; + } + } + return arr; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" "b/2203030044/chapter_10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..81ced4fe1d2d68b5da66151646f694c3a4135d90 --- /dev/null +++ "b/2203030044/chapter_10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,47 @@ +class Solution { +public: + ListNode* sortList(ListNode* head) { + if (!head || !head->next) { return head; } + ListNode* slow = head;//慢指针 + ListNode* fast = head;//快指针 + ListNode* precious = head; + while (fast && fast->next) { + precious = slow; + slow = slow->next; + fast = fast->next->next; + } + precious->next = NULL;//断开 + return merge(sortList(head), sortList(slow)); + } + // 或者可以这样写,无precious版本 + //ListNode* sortList(ListNode* head) { + // if (!head || !head->next) { return head; } + // ListNode* slow = head;//慢指针 + // ListNode* fast = head->next;//快指针 + // while (fast && fast->next) { + // slow = slow->next; + // fast = fast->next->next; + // } + // ListNode* right = slow->next; + // slow->next = NULL; + // return merge(sortList(head), sortList(right)); + //} + ListNode* merge(ListNode* list1, ListNode* list2) {// 合并两个链表模板 + if (list1 == NULL) { + return list2; + } + if (list2 == NULL) { + return list1; + } + + if (list1->val < list2->val) { + list1->next = merge(list1->next, list2); + return list1; + } + else { + list2->next = merge(list1, list2->next); + return list2; + } + } + +}; \ No newline at end of file diff --git "a/2203030044/chapter_10/\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/2203030044/chapter_10/\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..48097e13c3d4cc9a9f8f5ac7a70708eda4b8dd4a --- /dev/null +++ "b/2203030044/chapter_10/\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,28 @@ +class Solution { +public: + vector> kClosest(vector>& points, int k) { + struct cmp { + bool operator()(vector& a, vector& b) { + return pow(a[0], 2) + pow(a[1], 2) < pow(b[0], 2) + pow(b[1], 2); + } + }; + + priority_queue, vector>, cmp>q; + + for (int i = 0; i < points.size(); i++) { + q.push(points[i]); + if(q.size()>k){ + q.pop(); + } + } + + vector>answer; + + while(!q.empty()){ + answer.push_back(q.top()); + q.pop(); + } + + return answer; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_10/\346\261\202\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" "b/2203030044/chapter_10/\346\261\202\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0c38e132bd20744356b880088ec08e3e022c327f --- /dev/null +++ "b/2203030044/chapter_10/\346\261\202\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" @@ -0,0 +1,11 @@ +class Solution { +public: + vector smallestK(vector& arr, int k) { + vectoranswer; + sort(arr.begin(),arr.end()); + for(int i=0;i& indices) { + char arr[101]; + for (int i = 0; i < indices.size(); i++) { + arr[indices[i]] = s[i]; + } + string a; + for (int i = 0; i < indices.size(); i++) { + a += arr[i]; + } + return a; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" "b/2203030044/chapter_2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..a9800a92ed42d89006e04484b61717ff0fe3d396 --- /dev/null +++ "b/2203030044/chapter_2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" @@ -0,0 +1,9 @@ +struct ListNode* swapPairs(struct ListNode* head) { + if (head == NULL || head->next == NULL) { + return head; + } + struct ListNode* newHead = head->next; + head->next = swapPairs(newHead->next); + newHead->next = head; + return newHead; +} diff --git "a/2203030044/chapter_2/\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/2203030044/chapter_2/\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 0000000000000000000000000000000000000000..4ab020ff869d7296e7d62a5766a9d9937e1e1ebd --- /dev/null +++ "b/2203030044/chapter_2/\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,22 @@ +struct ListNode* deleteDuplicates(struct ListNode* head) { + if (!head) { + return head; + } + + struct ListNode* dummy = malloc(sizeof(struct ListNode)); + dummy->next = head; + + struct 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/2203030044/chapter_2/\345\217\215\350\275\254\351\223\276.cpp" "b/2203030044/chapter_2/\345\217\215\350\275\254\351\223\276.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..4748ab84ffc63ee4deb7c938cd138fcbb22c4797 --- /dev/null +++ "b/2203030044/chapter_2/\345\217\215\350\275\254\351\223\276.cpp" @@ -0,0 +1,48 @@ +void reverseLinkedList(struct ListNode *head) { + // 也可以使用递归反转一个链表 + struct ListNode *pre = NULL; + struct ListNode *cur = head; + + while (cur != NULL) { + struct ListNode *next = cur->next; + cur->next = pre; + pre = cur; + cur = next; + } +} + +struct ListNode *reverseBetween(struct ListNode *head, int left, int right) { + // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论 + struct ListNode *dummyNode = malloc(sizeof(struct ListNode)); + dummyNode->val = -1; + dummyNode->next = head; + + struct ListNode *pre = dummyNode; + // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点 + // 建议写在 for 循环里,语义清晰 + for (int i = 0; i < left - 1; i++) { + pre = pre->next; + } + + // 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点 + struct ListNode *rightNode = pre; + for (int i = 0; i < right - left + 1; i++) { + rightNode = rightNode->next; + } + + // 第 3 步:切断出一个子链表(截取链表) + struct ListNode *leftNode = pre->next; + struct ListNode *curr = rightNode->next; + + // 注意:切断链接 + pre->next = NULL; + rightNode->next = NULL; + + // 第 4 步:同第 206 题,反转链表的子区间 + reverseLinkedList(leftNode); + + // 第 5 步:接回到原来的链表中 + pre->next = rightNode; + leftNode->next = curr; + return dummyNode->next; +} \ No newline at end of file diff --git "a/2203030044/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.cpp" "b/2203030044/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.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8a6b7ed71aaf0e79903089e9c773ef4c0cd589aa --- /dev/null +++ "b/2203030044/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.cpp" @@ -0,0 +1,9 @@ +void merge(int* a, int nums1Size, int m, int* b, int nums2Size, int n) { + int i = m - 1, j = n - 1, k = n + m - 1; + while (i >= 0 && j >= 0) { + a[k--] = a[i] > b[j] ? a[i--] : b[j--]; + } + if (i == -1) { + while (j >= 0) a[k--] = b[j--]; + } +} \ No newline at end of file diff --git "a/2203030044/chapter_2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" "b/2203030044/chapter_2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..18d6b52c2f56f3787193da7d3b4eaca06df25211 --- /dev/null +++ "b/2203030044/chapter_2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" @@ -0,0 +1,31 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + + +struct ListNode* removeElements(struct ListNode* head, int val){ + struct ListNode* node=head; + if(head==NULL) return head; + while(node->next!=NULL) + { + if(node->val==val) + { + head=node->next; + node=head; + } + else if(node->next->val==val) + { + node->next=node->next->next; + } + else node=node->next; + } + if(head->val==val) + { + head=NULL; + } + return head; +} diff --git "a/2203030044/chapter_3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250.cpp" "b/2203030044/chapter_3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b659d6e30c2303c07237588c9fa16a4b540777a9 --- /dev/null +++ "b/2203030044/chapter_3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250.cpp" @@ -0,0 +1,65 @@ +class Solution { +public: + int calculate(string s) { + stackstk; + int num = 0; + char sign='+'; + bool first = false; + for (int i = 0; i < s.length(); i++) { + if (s[i] == ' ') { continue; } + if (isdigit(s[i])) { + num = num * 10 + (s[i] - '0'); + } + if (!isdigit(s[i])) { + if (first) { + if (sign == '+') { + stk.push(num); + } + else if (sign == '-') { + stk.push(-num); + } + else if (sign == '*') { + num = num * stk.top(); + stk.pop(); + stk.push(num); + } + else { + num = stk.top() / num; + stk.pop(); + stk.push(num); + } + sign = s[i]; + num = 0; + } + else { + stk.push(num); + num = 0; + sign = s[i]; + first = true; + } + } + } + if (sign == '+') { + stk.push(num); + } + else if (sign == '-') { + stk.push(-num); + } + else if (sign == '*') { + num = num * stk.top(); + stk.pop(); + stk.push(num); + } + else if (sign == '/') { + num = stk.top() / num; + stk.pop(); + stk.push(num); + } + int answer = 0; + while (!stk.empty()) { + answer += stk.top(); + stk.pop(); + } + return answer; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_3/\346\234\200\345\260\217\346\240\210.cpp" "b/2203030044/chapter_3/\346\234\200\345\260\217\346\240\210.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bc422fe838c813eb3a3b78c2052d8f312e9ee1a3 --- /dev/null +++ "b/2203030044/chapter_3/\346\234\200\345\260\217\346\240\210.cpp" @@ -0,0 +1,28 @@ +class MinStack { +public: + stackstk; + stackminstk; + int minnum = INT_MAX; + MinStack() { + + } + void push(int val) { + if (!minstk.empty()) { minnum = min(minstk.top(), val); } + else { minnum = INT_MAX; minnum = min(minnum, val); } + stk.push(val); + minstk.push(minnum); + } + + void pop() { + stk.pop(); + minstk.pop(); + } + + int top() { + return stk.top(); + } + + int getMin() { + return minstk.top(); + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2203030044/chapter_3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e928806b453cac76ea9533330e82a77fffe0714f --- /dev/null +++ "b/2203030044/chapter_3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + bool isValid(string s) { + stackstk; + if (s.length() % 2) { return false; } + for (int i = 0; i < s.length(); i++) { + if (s[i] == '[') { stk.push(']'); } + else if (s[i] == '(') { stk.push(')'); } + else if (s[i] == '{') { stk.push('}'); } + else if (stk.empty() || s[i] != stk.top()) { return false; } + else { stk.pop(); } + } + if (!stk.empty()) { return false; } + else { return true; } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" "b/2203030044/chapter_3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bde43a47c2aefa807094ce0ba7795b3f5ee1a6dc --- /dev/null +++ "b/2203030044/chapter_3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" @@ -0,0 +1,45 @@ +class MyQueue { +public: + stacka; + stackb; + int cnt = 0; + void push(int x) { + a.push(x); + cnt++; + } + + int pop() { + for (int i = 1; i < cnt; i++) { + b.push(a.top()); + a.pop(); + } + int answer = a.top(); + a.pop(); + cnt--; + for (int i = 1; i <= cnt; i++) { + a.push(b.top()); + b.pop(); + } + return answer; + } + + int peek() { + for (int i = 1; i < cnt; i++) { + b.push(a.top()); + a.pop(); + } + int answer = a.top(); + b.push(a.top()); + a.pop(); + for (int i = 1; i <= cnt; i++) { + a.push(b.top()); + b.pop(); + } + return answer; + } + + bool empty() { + if (a.empty()) { return true; } + else { return false; } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2203030044/chapter_3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ff92fcdd9d291e7143e9ae168fe0725667b684d7 --- /dev/null +++ "b/2203030044/chapter_3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" @@ -0,0 +1,45 @@ +class MyStack { +public: + queuea; + queueb; + int cnt = 0; + void push(int x) { + a.push(x); + cnt++; + } + + int pop() { + for (int i = 1; i < cnt; i++) { + b.push(a.front()); + a.pop(); + } + int answer = a.front(); + a.pop(); + cnt--; + for (int i = 1; i <= cnt; i++) { + a.push(b.front()); + b.pop(); + } + return answer; + } + + int top() { + for (int i = 1; i < cnt; i++) { + b.push(a.front()); + a.pop(); + } + int answer = a.front(); + b.push(a.front()); + a.pop(); + for (int i = 1; i <= cnt; i++) { + a.push(b.front()); + b.pop(); + } + return answer; + } + + bool empty() { + if (a.empty()) { return true; } + else { return false; } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_3/\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/2203030044/chapter_3/\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 0000000000000000000000000000000000000000..8ed532be497d6338ff7cda24ed7e1f4e3586ed88 --- /dev/null +++ "b/2203030044/chapter_3/\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,41 @@ +class Solution { +public: + int evalRPN(vector& tokens) { + stackstk; + int temp = 0; + for (int i = 0; i < tokens.size(); i++) { + if (tokens[i] == "+") { + temp = stk.top(); + stk.pop(); + temp += stk.top(); + stk.pop(); + stk.push(temp); + } + else if (tokens[i] == "-") { + temp = stk.top(); + stk.pop(); + temp = stk.top() - temp; + stk.pop(); + stk.push(temp); + } + else if (tokens[i] == "*") { + temp = stk.top(); + stk.pop(); + temp *= stk.top(); + stk.pop(); + stk.push(temp); + } + else if (tokens[i] == "/") { + temp = stk.top(); + stk.pop(); + temp = stk.top() / temp; + stk.pop(); + stk.push(temp); + } + else { + stk.push(stoi(tokens[i])); + } + } + return stk.top(); + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_4/\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.cpp" "b/2203030044/chapter_4/\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..3333028180ab904c8d13a1974019782bf1a16491 --- /dev/null +++ "b/2203030044/chapter_4/\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + int strStr(string haystack, string needle) { + int answerindex = -1; + if (needle.length() > haystack.length()) { return answerindex; } + for (int i = 0; i <= haystack.length() - needle.length(); i++) { + int index = 0; + int j = i; + if (haystack[j] == needle[index]) { + while (haystack[j] == needle[index]) { + if (index == needle.length() - 1 && haystack[j] == needle[index]) { + answerindex = i; + return answerindex; + } + index++; + j++; + } + } + } + return answerindex; + } +}; diff --git "a/2203030044/chapter_4/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" "b/2203030044/chapter_4/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d04fb0e28d083e1d46fcb7152396515c8f8074d5 --- /dev/null +++ "b/2203030044/chapter_4/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + string longestCommonPrefix(vector& strs) { + string a; + bool judge = true; + string demo = strs[0]; + for (int i = 0; i < demo.length(); i++) { + for (int j = 0; j < strs.size(); j++) { + if (demo[i] != strs[j][i]) { judge = false; } + } + if (judge == true) { a += demo[i]; } + else { break; } + } + return a; + } +}; diff --git "a/2203030044/chapter_4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" "b/2203030044/chapter_4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..3333028180ab904c8d13a1974019782bf1a16491 --- /dev/null +++ "b/2203030044/chapter_4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + int strStr(string haystack, string needle) { + int answerindex = -1; + if (needle.length() > haystack.length()) { return answerindex; } + for (int i = 0; i <= haystack.length() - needle.length(); i++) { + int index = 0; + int j = i; + if (haystack[j] == needle[index]) { + while (haystack[j] == needle[index]) { + if (index == needle.length() - 1 && haystack[j] == needle[index]) { + answerindex = i; + return answerindex; + } + index++; + j++; + } + } + } + return answerindex; + } +}; diff --git "a/2203030044/chapter_4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" "b/2203030044/chapter_4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8241bc4bc3c20eb8deff5a992c97309147b68b4d --- /dev/null +++ "b/2203030044/chapter_4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" @@ -0,0 +1,17 @@ +class Solution { +public: + bool isPalindrome(string s) { + string a; + for (int i = 0; i < s.length(); i++) { + if (s[i] >= 'A' && s[i] <= 'Z' || s[i] >= 'a' && s[i] <= 'z' || s[i] >= '0' && s[i] <= '9') { + a += tolower(s[i]); + } + } + string newa; + for (int i = a.length() - 1; i >= 0; i--) { + newa += a[i]; + } + if (a == newa) { return true; } + else { return false; } + } +}; diff --git "a/2203030044/chapter_5/P234\345\233\236\346\226\207\351\223\276\350\241\250.cpp" "b/2203030044/chapter_5/P234\345\233\236\346\226\207\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5102a55d839a73bd6d6ec24d27a30782ee3c4948 --- /dev/null +++ "b/2203030044/chapter_5/P234\345\233\236\346\226\207\351\223\276\350\241\250.cpp" @@ -0,0 +1,14 @@ +class Solution { +public: + bool isPalindrome(ListNode* head) { + vectorarr; + while(head != NULL) { + arr.push_back(head->val); + head = head->next; + } + for (int i = 0, j = arr.size() - 1; i < j; i++, j--) { + if (arr[i] != arr[j]) { return false; } + } + return true; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_5/P509\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" "b/2203030044/chapter_5/P509\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7746b1ccc264a75751e0665f3eda198952c996ef --- /dev/null +++ "b/2203030044/chapter_5/P509\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" @@ -0,0 +1,9 @@ +class Solution { +public: + int fib(int n) { + if (n == 0) { return 0; } + if (n == 1) { return 1; } + if (n == 2) { return 1; } + return fib(n - 1) + fib(n - 2); + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_5/\345\256\236\351\252\214\344\270\200.cpp" "b/2203030044/chapter_5/\345\256\236\351\252\214\344\270\200.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..45d42cadd0d6dded4bcb7d4d4c29226b7aff2294 --- /dev/null +++ "b/2203030044/chapter_5/\345\256\236\351\252\214\344\270\200.cpp" @@ -0,0 +1,23 @@ +#include +int m = 0; +void Move(int n, char A, char B, char C) +{ + m++; + if (n == 1) { + printf("%c -> %c\n", A, C); + } + else { + Move(n - 1, A, C, B); + printf("%c -> %c\n", A, C); + Move(n - 1, B, A, C); + } +} + +int main() +{ + int n; + printf("请输入盘子数:"); + scanf_s("%d", &n); + Move(n, 'A', 'B', 'C'); + printf("移动次数:%d次", m); +} \ No newline at end of file diff --git "a/2203030044/chapter_5/\345\256\236\351\252\214\344\270\203.cpp" "b/2203030044/chapter_5/\345\256\236\351\252\214\344\270\203.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..fc49622bd55a1a98b4812300bec168d6039b9093 --- /dev/null +++ "b/2203030044/chapter_5/\345\256\236\351\252\214\344\270\203.cpp" @@ -0,0 +1,44 @@ +#include +using namespace std; +typedef long long ll; +int n; +int a[100]; +int b[100]; +int c[100]; +int d[100]; +int total; +void getin() { + cin >> n; +} +void dfs(int i) { + if (i > n) { + total++; + if (total <= 3) { + for (int q = 1; q <= n; q++) { + cout << a[q] << ' '; + } + cout << endl; + } + return; + } + for (int j = 1; j <= n; j++) { + if (b[j] == 0 && c[i + j] == 0 && d[i - j + n] == 0) { + a[i] = j; + b[j] = 1; + c[i + j] = 1; + d[i - j + n] = 1; + dfs(i + 1); + b[j] = 0; + c[i + j] = 0; + d[i - j + n] = 0; + } + } +} +int main() { + std::ios::sync_with_stdio(false); + std::cin.tie(nullptr); + getin(); + dfs(1); + cout << total; + return 0; +} diff --git "a/2203030044/chapter_5/\345\256\236\351\252\214\344\270\211.cpp" "b/2203030044/chapter_5/\345\256\236\351\252\214\344\270\211.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..45d42cadd0d6dded4bcb7d4d4c29226b7aff2294 --- /dev/null +++ "b/2203030044/chapter_5/\345\256\236\351\252\214\344\270\211.cpp" @@ -0,0 +1,23 @@ +#include +int m = 0; +void Move(int n, char A, char B, char C) +{ + m++; + if (n == 1) { + printf("%c -> %c\n", A, C); + } + else { + Move(n - 1, A, C, B); + printf("%c -> %c\n", A, C); + Move(n - 1, B, A, C); + } +} + +int main() +{ + int n; + printf("请输入盘子数:"); + scanf_s("%d", &n); + Move(n, 'A', 'B', 'C'); + printf("移动次数:%d次", m); +} \ No newline at end of file diff --git "a/2203030044/chapter_6/P1572\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/2203030044/chapter_6/P1572\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..b1af871d07c4b9cf02741cd1dd12978074106de8 --- /dev/null +++ "b/2203030044/chapter_6/P1572\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,16 @@ +class Solution { +public: + int diagonalSum(vector >& mat) { + int sum = 0; + for (int i = 0, j = 0; i < mat.size() && j < mat.size(); i++, j++) { + sum += mat[i][j]; + } + for (int i = 0, j = mat.size() - 1; i < mat.size() && j>=0; i++, j--) { + sum += mat[i][j]; + } + if (mat.size() % 2) { + sum -= mat[(mat.size() - 1) / 2][(mat.size() - 1) / 2]; + } + return sum; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_6/P283\347\247\273\345\212\250\351\233\266.cpp" "b/2203030044/chapter_6/P283\347\247\273\345\212\250\351\233\266.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5f99d7f312cd296a5c28ba4bf96bba89d6517c57 --- /dev/null +++ "b/2203030044/chapter_6/P283\347\247\273\345\212\250\351\233\266.cpp" @@ -0,0 +1,15 @@ +class Solution { +public: + void moveZeroes(vector& nums) { + for (int i = 0; i < nums.size(); i++) { + if (nums[i] == 0) { + for (int j = i + 1; j < nums.size(); j++) { + if (nums[j] != 0) { + swap(nums[i], nums[j]); + break; + } + } + } + } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_6/\345\256\236\351\252\2141.cpp" "b/2203030044/chapter_6/\345\256\236\351\252\2141.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c08d6a9512f52f8bb93a0ee80251b4d9487d9a96 --- /dev/null +++ "b/2203030044/chapter_6/\345\256\236\351\252\2141.cpp" @@ -0,0 +1,237 @@ +#include +#include +#include +#include +#define false 0 +#define true 1 +#define max 100 +typedef struct { + int value; //value为具体数值 + int row,col; //row为行,col为列 +}Array; +typedef struct { + Array data[max+1]; + int rows,cols,nums; //nums为非零元素个数 +}Arrays; +//创建稀疏矩阵 +int InitArray(Arrays *L,int rows,int cols,int nums){ + int i,j; + L->nums=nums; + L->cols=cols; + L->rows=rows; + assert(L->numsnums; i++) { + scanf("%d %d %d", &L->data[i].row, &L->data[i].col, &L->data[i].value); + } + printf("创建成功!\n"); + return true; + } +//遍历输出稀疏矩阵 三元组形式 +void bianli1(Arrays *L) { + printf("-------------三元组形式形式遍历输出:\n"); + for (int i = 1; i <= L->rows; i++) { + printf("%d行%d列%d \n",L->data[i].row,L->data[i].col,L->data[i].value); + } +} + +//遍历输出稀疏矩阵 矩阵形式 +void bianli2(Arrays *L) { + int flag = 1; //flag代表 + for (int i = 1; i <= L->rows; i++) { + for (int j = 1; j <=L->cols; j++) { + if (L->data[flag].value!=0&&L->data[flag].row==i&&L->data[flag].col==j) { + printf(" %d", L->data[flag].value); + flag++; + } + else { + printf(" 0"); + } + } + printf("\n"); + } +} +//矩阵普通转置(按列) +int Transform(Arrays a,Arrays *b){ // //a为原矩阵,b为转置后的矩阵 + b->cols=a.cols; + b->nums=a.nums; + b->rows=a.rows; + if(b->nums>0){ + int j=1; + for(int k=1;k<=a.cols;k++){ + for(int i=1;i<=a.nums;i++){ //进入非零元素循环 + if(a.data[i].col==k){ //如果在每一个列中有非零元素,则进行转置操作 + b->data[j].row=a.data[i].col; //j代表表B的非零元素个数值,它从1开始。 + b->data[j].col=a.data[i].row; + b->data[j].value=a.data[i].value; + j++; + if(j>a.nums) return false; + } + } + } + } +} +//矩阵快速转置 +void FastTransform(Arrays *a,Arrays *b){ //a为原矩阵,b为转置后的矩阵 + int num[max],position[max]; + int col,i,k,m; + b->cols=a->cols; + b->nums=a->nums; + b->rows=a->rows; + if(b->nums>0){ + for(col=1;col<=a->cols;col++){ //首先将num数组的数值都赋值为0 + num[col]=0; + } + for(i=1;i<=a->nums;i++){ //再遍历非零元素,把非零元素的列所在的num数组进行++操作 + num[a->data[i].col]++; + } + position[1]=1; //第一个非零元素的position值定义为1 + for(col=2;col<=a->cols;col++){ + position[col]=position[col-1]+num[col-1]; //前一列非零元素的起始位置加非零元素的个数等于后一列的起始位置 + } + for(k=1;k<=a->nums;k++){ //对非零元素进行遍历并依次转置 + col=a->data[k].col; + m=position[col]; + b->data[m].row=a->data[k].col; + b->data[m].col=a->data[k].row; + b->data[m].value=a->data[k].value; + position[col]++; + } + } +} +//矩阵加法 +int ADD(Arrays a,Arrays b,Arrays *c){ + int k=1,i,j; //i为a的元素数目,j为b的元素数目,k为c的元素数目 + + //同行同列的才能相加 + if(a.cols!=b.cols||a.rows!=b.rows){ + return false; + } + + c->cols=a.cols; //赋值总行数,总列数 + c->rows=a.rows; + //进行遍历赋值 + for(i=1,j=1;i<=a.nums&&j<=b.nums;){ + + //B的行号大于A直接将A中元素加入C矩阵 + if(a.data[i].rowdata[k].col=a.data[i].col; + c->data[k].row=a.data[i].row; + c->data[k].value=a.data[i].value; + k++; //C元素向后增一位 + i++; //a赋值则a元素向后增一位,如果时b则b元素向后增一位 + } + //B的行号小于A直接将B中元素加入C矩阵 + else if(a.data[i].row>b.data[j].row){ + c->data[k].col=b.data[j].col; + c->data[k].row=b.data[j].row; + c->data[k].value=b.data[j].value; + k++; + j++; + }else{ //行号相同 + + //B的列号小于A直接将B中元素加入C矩阵 + if(a.data[i].col>b.data[j].col) { + c->data[k].col=b.data[j].col; + c->data[k].row=b.data[j].row; + c->data[k].value=b.data[j].value; + k++; + j++; + } + + //B的列号大于A直接将A中元素加入C矩 + else if(a.data[i].coldata[k].col=a.data[i].col; + c->data[k].row=a.data[i].row; + c->data[k].value=a.data[i].value; + k++; + i++; + } + //相等 + else { + + c->data[k].col=a.data[i].col; + c->data[k].row=a.data[i].row; + c->data[k].value=a.data[i].value+b.data[j].value; + k++; + i++; + j++; + } + } + } + while(i<=a.nums){ //B取完A未取完 + + //将A中所剩元素依次加入到C中 + c->data[k].col=a.data[i].col; + c->data[k].row=a.data[i].row; + c->data[k].value=a.data[i].value; + k++; + i++; + } + while(j<=b.nums){ //A取完B未取完 + + //将A中所剩元素依次加入到C中 + c->data[k].col=b.data[j].col; + c->data[k].row=b.data[j].row; + c->data[k].value=b.data[j].value; + k++; + j++; + } +} + + + + +//矩阵减法 +int reduce(Arrays a,Arrays b,Arrays *c){ //调用加法操作,在b矩阵基础上乘 -1 在加 a矩阵 + for(int i=1;i<=b.nums;i++){ + b.data[i].value=b.data[i].value*-1; + } + ADD(a,b,c); +} +//矩阵乘法 +int multipe(Arrays a,Arrays b,Arrays *c){ + int m=1,n=1,k=1; //m为a的元素数目,n为b的元素数目,k为c的元素数目 + if(a.cols!=b.cols||a.rows!=b.rows){ + return false; + } + c->cols=a.cols; + c->rows=a.rows; + while(m<=a.nums&&n<=b.nums){ + if(a.data[m].col==b.data[n].col&&a.data[m].row==b.data[n].row){ + c->data[k].col=a.data[m].col; + c->data[k].row=a.data[m].row; + c->data[k].value=a.data[m].value*b.data[n].value; + } + m++;n++;k++; + } +} +int main(){ + Arrays s; + Arrays s1; + Arrays s2; + Arrays s3; + int row, col, num; + //创建 + printf("请依次输入稀疏矩阵A的行数,列数,非零元个数(用空格隔开):\n"); + scanf("%d %d %d", &row, &col, &num); + InitArray(&s,row,col,num); + bianli2(&s); + printf("请依次输入稀疏矩阵B的行数,列数,非零元个数(用空格隔开):\n"); + scanf("%d %d %d", &row, &col, &num); + InitArray(&s1,row,col,num); + bianli2(&s1); + printf("---------矩阵B的转置为:\n"); + Transform(s1,&s3); + bianli2(&s3); + printf("---------矩阵相乘为:\n"); + multipe(s,s1,&s2); + bianli2(&s2); + printf("---------矩阵相加为:\n"); + ADD(s,s1,&s2); + bianli2(&s2); + printf("---------矩阵相减为:\n"); + reduce(s,s1,&s2); + bianli2(&s2); +} \ No newline at end of file diff --git "a/2203030044/chapter_6/\345\256\236\351\252\214\344\270\211.cpp" "b/2203030044/chapter_6/\345\256\236\351\252\214\344\270\211.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..93d14e39162c02b6b4b6439c410448fec589bc41 --- /dev/null +++ "b/2203030044/chapter_6/\345\256\236\351\252\214\344\270\211.cpp" @@ -0,0 +1,65 @@ +#include +using namespace std; + +int arr[10][10] = { + {1,2,3,4,5}, + {16,17,18,19,6}, + {15,24,25,20,7}, + {14,23,22,21,6}, + {13,12,11,10,9} +}; +bool ifdetect[10][10]; + + +int main() { + int toward = 0;//0东 1南 2西 3北 + int i = 0, j = 0; + while (1) { + if (toward == 0) { + cout << arr[i][j] << " "; + ifdetect[i][j] = true; + if (arr[i][j] == 25) { break; } + if (arr[i][j + 1] == 0 || ifdetect[i][j + 1]) { + toward = 1; + i++; + } + else { + j++; + } + } + else if (toward == 1) { + cout << arr[i][j] << " "; + ifdetect[i][j] = true; + if (arr[i + 1][j] == 0 || ifdetect[i + 1][j]) { + toward = 2; + j--; + } + else { + i++; + } + } + else if (toward == 2) { + cout << arr[i][j] << " "; + ifdetect[i][j] = true; + if (arr[i][j-1] == 0 || ifdetect[i][j-1]) { + toward = 3; + i--; + } + else { + j--; + } + } + else if (toward == 3) { + cout << arr[i][j] << " "; + ifdetect[i][j] = true; + if (arr[i-1][j] == 0 || ifdetect[i -1][j]) { + toward = 0; + j++; + } + else { + i--; + } + } + } + return 0; +} diff --git "a/2203030044/chapter_6/\345\256\236\351\252\214\344\272\224.cpp" "b/2203030044/chapter_6/\345\256\236\351\252\214\344\272\224.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..a1ff44ae477d4baa6b9bc137472a4ff5984ac1e2 --- /dev/null +++ "b/2203030044/chapter_6/\345\256\236\351\252\214\344\272\224.cpp" @@ -0,0 +1,89 @@ +#include +using namespace std; + +#define N 4 +#define M 10 +/* ----------------------输入矩阵a,b n表示矩阵所含的行数------------- */ +static void input(int n, int a[], int b[]) +{ + for(int k=0;k>a[k]; + } + for(int k=0;k>b[k]; + } +} +/*-----------------------返回压缩存储a中a[i][j]的值-----------------------*/ +/** +* 算法思路 +* 对称矩阵M的第i行和第j列的元素的数据存储在一维数组a中的位置k的计算公式: +* 1、当i大于或等于j时,k = (i * (i + 1)) / 2 + j (下三角) +* 2、当i小于j时,k = (j * (j + 1)) / 2 + i (上三角) +* +*/ +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]) +{ + for (int i=0;i>n; + int a[n*(n+1)/2],b[n*(n+1)/2]; + int c1[N][N],c2[N][N]; + input(n,a,b); + cout<<"两个矩阵之和"<& nums) { + int maxcnt = 0; + int cnt = 0; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] == 1) { + cnt++; + maxcnt = max(maxcnt, cnt); + } + else { + cnt = 0; + } + } + return maxcnt; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_6/\351\207\215\345\241\221\347\237\251\351\230\265.cpp" "b/2203030044/chapter_6/\351\207\215\345\241\221\347\237\251\351\230\265.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5f046e8b9622add79213b48bb3ab88ddbfa1d508 --- /dev/null +++ "b/2203030044/chapter_6/\351\207\215\345\241\221\347\237\251\351\230\265.cpp" @@ -0,0 +1,15 @@ +class Solution { +public: + vector> matrixReshape(vector>& mat, int r, int c) { + vector >temp(r, vector(c)); + if (mat.size() * mat[0].size() != r * c) { + return mat; + } + else { + for (int i = 0; i < mat.size() * mat[0].size(); i++) { + temp[i / c][i % c] = mat[i / mat[0].size()][i % mat[0].size()]; + } + return temp; + } + } +}; diff --git "a/2203030044/chapter_7/N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" "b/2203030044/chapter_7/N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8a187c2e5281a1154c03456c54d09d9347c6fea3 --- /dev/null +++ "b/2203030044/chapter_7/N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" @@ -0,0 +1,17 @@ +class Solution { +public: + void dfs(Node*node,vector& arr){ + if(!node){return ;} + arr.push_back(node->val); + for(int i=0;ichildren.size();i++){ + if(node->children[i]){ + dfs(node->children[i],arr); + } + } + } + vector preorder(Node* root) { + vectorarr; + dfs(root,arr); + return arr; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\344\272\214\345\217\211\346\240\221\346\234\200\345\244\247\345\256\275\345\272\246.cpp" "b/2203030044/chapter_7/\344\272\214\345\217\211\346\240\221\346\234\200\345\244\247\345\256\275\345\272\246.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..4fea78f756b786e8f6ff6e016f8466bffbc81d2c --- /dev/null +++ "b/2203030044/chapter_7/\344\272\214\345\217\211\346\240\221\346\234\200\345\244\247\345\256\275\345\272\246.cpp" @@ -0,0 +1,27 @@ +class Solution { +public: + int widthOfBinaryTree(TreeNode* root) { + if(!root){ + return 0; + } + unsigned long long maxwidth=1; + queue> q; + q.push(make_pair(root, 1)); + while (!q.empty()) { + maxwidth = max(maxwidth, q.back().second - q.front().second + 1); + int len = q.size(); + for (int i = 0; i < len; i++) { + auto node = q.front().first; + auto index = q.front().second; + q.pop(); + if (node->left) { + q.push(make_pair(node->left, index * 2)); + } + if(node->right) { + q.push(make_pair(node->right, index * 2 + 1)); + } + } + } + return maxwidth; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\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/2203030044/chapter_7/\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..1f22991877bfabd19bab25a7ee35942fd5fa248c --- /dev/null +++ "b/2203030044/chapter_7/\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,16 @@ +class Solution { +public: + void dfs(TreeNode* root, vector& answer) { + if (root == NULL) { + return; + } + dfs(root->left, answer); + dfs(root->right, answer); + answer.push_back(root->val); + } + vector postorderTraversal(TreeNode* root) { + vectoranswer; + dfs(root, answer); + return answer; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" "b/2203030044/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ddce43c969e5864c0a328b6401a9dd59209453bb --- /dev/null +++ "b/2203030044/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" @@ -0,0 +1,23 @@ +class Solution { +public: + vector> levelOrder(TreeNode* root) { + vector> answer; + queueq; + q.push(root); + if (root == NULL) { return answer; } + + while (!q.empty()) { + vectortemp; + int len=q.size(); + for (int i = 0; i < len; i++) { + root = q.front(); + q.pop(); + temp.push_back(root->val); + if (root->left) { q.push(root->left); } + if (root->right) { q.push(root->right); } + } + answer.push_back(temp); + } + return answer; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\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/2203030044/chapter_7/\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..d259b928ab355f20940ae950dc31cb9e664f7897 --- /dev/null +++ "b/2203030044/chapter_7/\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,7 @@ +class Solution { +public: + int maxDepth(TreeNode* root) { + if(root==NULL)return 0; + return max(maxDepth(root->left),maxDepth(root->right))+1; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\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/2203030044/chapter_7/\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..fe33b3e84ac0c9049068525f85561fbaba18f242 --- /dev/null +++ "b/2203030044/chapter_7/\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,14 @@ +class Solution { +public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + if(root==NULL||root==p||root==q){ + return root; + }else{ + TreeNode*left=lowestCommonAncestor(root->left,p,q); + TreeNode*right=lowestCommonAncestor(root->right,p,q); + if(left==NULL){return right;} + if(right==NULL){return left;} + } + return root; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\344\273\216\345\211\215\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/2203030044/chapter_7/\344\273\216\345\211\215\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..9281cb8e316763aa2cc6910dca2282065f46d9db --- /dev/null +++ "b/2203030044/chapter_7/\344\273\216\345\211\215\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,24 @@ +class Solution { +private: + unordered_mapm; +public: + TreeNode* dfs(vector& preorder, vectorinorder, int pl, int pr, int il, int ir) { + if (pl > pr) { + return NULL; + } + int proot = pl; + int iroot = m[preorder[pl]]; + TreeNode* root = new TreeNode(preorder[pl]); + int len = iroot - il; + root->left = dfs(preorder, inorder, proot + 1, proot + len, il, iroot - 1); + root->right = dfs(preorder, inorder, proot + len + 1, pr, iroot + 1, ir); + return root; + } + TreeNode* buildTree(vector& preorder, vector& inorder) { + if (preorder.empty()) { return NULL; } + for (int i = 0; i < preorder.size(); i++) { + m[inorder[i]] = i; + } + return dfs(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1); + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\345\234\250\346\257\217\344\270\252\346\240\221\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" "b/2203030044/chapter_7/\345\234\250\346\257\217\344\270\252\346\240\221\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b82c626d4b10a2257de17982874fc5b325c49f77 --- /dev/null +++ "b/2203030044/chapter_7/\345\234\250\346\257\217\344\270\252\346\240\221\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + vector largestValues(TreeNode* root) { + vectorarr; + queueq; + q.push(root); + if(root==NULL){return {};} + while(!q.empty()){ + int maxnum=INT_MIN; + int len=q.size(); + for(int i=0;ival); + if(root->left){q.push(root->left);} + if(root->right){q.push(root->right);} + } + arr.push_back(maxnum); + } + return arr; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_7/\345\256\236\351\252\21410.cpp" "b/2203030044/chapter_7/\345\256\236\351\252\21410.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0f458e870f79824eb2580b3476518c3ad88ad8b3 --- /dev/null +++ "b/2203030044/chapter_7/\345\256\236\351\252\21410.cpp" @@ -0,0 +1,200 @@ +#include +using namespace std; + +typedef char ElemType; +typedef struct node +{ + ElemType data[15]; //结点的值 + struct node* sons[3]; //指向孩子结点 +}TSonNode; //孩子链存储结构中的结点类型 +typedef struct +{ + char N[15]; + char n[15]; +}arr; +//读取文件内容到数组R中 +void ReadFile(arr 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[], arr R[], int n) +{ + TSonNode* t; + int k, i = 0, j = 0; + t = (TSonNode*)malloc(sizeof(TSonNode)); + strcpy(t->data, str); + for (k = 0; k < 3; k++) + t->sons[k] = NULL; + while (i < n) + { + if (strcmp(R[i].N, str) == 0) + { + t->sons[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; i < 3; i++) + { + DispTree(t->sons[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; i < 3; i++) + { + if (t->sons[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; i < 3; i++) + { + if (t->sons[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; i < 3; i++) + { + if (p->sons[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; i < 3; i++) + { + if (p->sons[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; i < 3; i++) + { + if (p->sons[i] != NULL) + sum += LeafSumOfvalue(p->sons[i]); + else + break; + } + } + } + return sum; +} + +int main() +{ + int n = 0; + TSonNode* t; + arr R[100]; + FILE* fp; + if ((fp = fopen("table.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 < n; i++) + printf("%s %s\n", R[i].N, R[i].n); //输出R数组查看是否读取正确 + printf("\n由数组R创建树T,"); + t = CreateTree(R[0].N, R, n); //创建一颗树 + printf("由括号表示输出树T:\n"); + DispTree(t); + char str[10]; + printf("\n请输入学院名:"); + scanf("%s", str); + printf("\n%s的专业数:%d\n", str, ChildCount(FindNode(t, str))); + printf("%s的班级数:%d\n", str, LeafCount(FindNode(t, str))); + printf("\n请输入学院名:"); + scanf("%s", str); + printf("\n%s的学生数:%d\n", str, LeafSumOfvalue(FindNode(t, str))); + printf("销毁树!\n"); + DestroyTree(t); + return 0; +} + diff --git "a/2203030044/chapter_7/\345\256\236\351\252\2145.cpp" "b/2203030044/chapter_7/\345\256\236\351\252\2145.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7a6d153a10c98a58b9d1d7dc6b8c7a374b493da1 --- /dev/null +++ "b/2203030044/chapter_7/\345\256\236\351\252\2145.cpp" @@ -0,0 +1,83 @@ +#include +using namespace std; + +typedef struct {//哈夫曼树的节点 + string data; + int parent; + int left; + int right; + int value; +}treenode; + +typedef struct {//哈夫曼编码的值 + string s; +}treecode; + +void createTree(treenode arr[], int n) {//构造哈夫曼树 + for (int i = 0; i < 2 * n - 1; i++) {//初始化这些节点的parent和child + arr[i].parent = arr[i].left = arr[i].right = -1; + } + for (int i = n; i < 2 * n - 1; i++) { + int min1 = INT_MAX, min2 = INT_MAX;//左节点<右节点 + int l = -1, r = -1; + for (int j = 0; j <= i - 1; j++) {//选择两个最小的节点 + if (arr[j].parent == -1) {//如果这个结点没有用过 + if (arr[j].value < min1) {//如果比左节点小 + min2 = min1; + r = l; + min1 = arr[j].value; + l = j; + } + else if (arr[j].value < min2) {//如果比右节点小 + min2 = arr[j].value; + r = j; + } + } + } + arr[i].value = arr[l].value + arr[r].value; + arr[i].left = l, arr[i].right = r; + arr[l].parent = i, arr[r].parent = i; + } +} + +void createCode(treenode arr[],treecode code[], int n) { + for (int i = 0; i < n; i++) {//从叶子节点开始 + int childIndex = i; + int parentIndex = arr[i].parent;//当前节点的父节点位置 + while (parentIndex != -1) {//未到根节点 + if (arr[parentIndex].left == childIndex) {//如果当前节点的值是左节点 + code[i].s += "0"; + } + else {//如果当前节点的值是右节点 + code[i].s += "1"; + } + childIndex = parentIndex; + parentIndex = arr[parentIndex].parent; + } + } +} + +int main() { + vectorstr = {"The","of","a","to","and","in","that","he","is","at","on","for","His","are","be"}; + int valuearr[] = { 1192,677,541,518,462,450,242,195,190,181,174,157,138,124,123 }; + int len = 15; + treenode arr[100]; + treecode code[100]; + for (int i = 0; i < len; i++) { + arr[i].data = str[i]; + arr[i].value = valuearr[i]; + } + createTree(arr, len); + createCode(arr, code, len); + + int sumpath = 0; + int sumvalue = 0; + cout << "哈夫曼编码为:" << endl; + for (int i = 0; i < len; i++) { + cout << arr[i].data << '\t' << code[i].s << endl; + sumvalue += arr[i].value; + sumpath += arr[i].value * code[i].s.length(); + } + cout << "平均长度为:" << 1.0 * sumpath / sumvalue; + return 0; +} diff --git "a/2203030044/chapter_7/\345\256\236\351\252\2147.cpp" "b/2203030044/chapter_7/\345\256\236\351\252\2147.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..40ff88143c90d733aa80281f70552b44435dd368 --- /dev/null +++ "b/2203030044/chapter_7/\345\256\236\351\252\2147.cpp" @@ -0,0 +1,165 @@ +#include +using namespace std; + +typedef struct node { + char data; + struct node* left; + struct node* right; +}treenode; + +void createtree(treenode* root, string s) {//建立二叉树 + root->data = s[0]; + int toward = 0; + stackarr;//用队列存放元素 + arr.push(root); + for (int i = 1; i < s.size(); i++) { + if (s[i] == '(') { + toward = 1; + } + else if (s[i] == ')') { + arr.pop(); + } + else if (s[i] == ',') { + toward = 2; + if (arr.size() != 1 && s[i - 1] != '(') { + arr.pop(); + } + } + else { + treenode* newnode = new treenode; + newnode->left = newnode->right = NULL; + if (toward == 1) {//左孩子 + arr.top()->left = newnode; + newnode->data = s[i]; + arr.push(newnode); + } + else {//右孩子 + arr.top()->right = newnode; + newnode->data = s[i]; + arr.push(newnode); + } + } + } +} + +void preSearch(treenode* node, char arr[],int index,char maxpath[],int &maxlen) {//前序遍历 + arr[index] = node->data; + if (!node->left && !node->right) {//到了叶子结点 + for (int i = index; i > 1; i--) { + cout << arr[i] << "->"; + } + cout << arr[1] << endl; + if (index > maxlen) {//长度更长 + for (int i = 1; i <= index; i++) { + maxpath[i] = arr[i]; + } + maxlen = index; + } + return; + } + if (node->left) { + preSearch(node->left, arr, index + 1, maxpath, maxlen); + } + if (node->right) { + preSearch(node->right, arr, index + 1, maxpath, maxlen); + } +} + +void afterSearch(treenode* node) { + treenode* arr[100]; + int index = -1; + treenode* cur = node; + treenode* pre = NULL; + bool judge = false; + do{ + while (cur) { + index++; + arr[index] = cur; + cur = cur->left; + } + pre = NULL; + judge = true; + while (index > -1 && judge) { + cur = arr[index]; + if (cur->right == pre) { + if (cur->left == NULL && cur->right == NULL) { + for (int i = index; i > 0; i--) { + cout << arr[i]->data << "->"; + } + cout << arr[0]->data << endl; + } + index--; + pre = cur; + } + else { + cur = cur->right; + judge = false; + } + } + } while (index > -1); +} +void lawyerSearch(treenode* root) +{ + struct newnode + { + treenode* node; + int parent; + }Qu[100]; + int front, rear, p; + front = rear = -1; + rear++; + Qu[rear].node = root; + Qu[rear].parent = -1; + while (front < rear) + { + front++; + root = Qu[front].node; + if (root->left == NULL && root->right == NULL) + { + p = front; + while (Qu[p].parent != -1) + { + cout << Qu[p].node->data << "->"; + p = Qu[p].parent; + } + printf("%c\n", Qu[p].node->data); + } + if (root->left != NULL) + { + rear++; + Qu[rear].node = root->left; + Qu[rear].parent = front; + } + if (root->right != NULL) + { + rear++; + Qu[rear].node = root->right; + Qu[rear].parent = front; + } + } +} +int main() { + string tree = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"; + treenode* root = new treenode; + createtree(root, tree);//建树 + char arr[100]; + char maxpath[100]; + int maxlen = 0; + cout << "前序遍历的路径为:" << endl; + preSearch(root, arr, 1, maxpath, maxlen); + + cout << "最长的路径为:" << endl; + for (int i = maxlen; i > 1; i--) { + cout << maxpath[i] << "->"; + } + cout << maxpath[1] << endl << endl; + + cout << "后序遍历的路径为:" << endl; + afterSearch(root); + cout << endl; + + cout << "层次遍历的路径为:" << endl; + lawyerSearch(root); + return 0; +} + diff --git "a/2203030044/chapter_7/\345\256\236\351\252\214\344\270\200.cpp" "b/2203030044/chapter_7/\345\256\236\351\252\214\344\270\200.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..6e446e512a2bcdc57336a33fb1abe0eb8826e946 --- /dev/null +++ "b/2203030044/chapter_7/\345\256\236\351\252\214\344\270\200.cpp" @@ -0,0 +1,104 @@ +#include +using namespace std; + +typedef struct node { + char data; + struct node* left; + struct node* right; +}treenode; + +void createtree(treenode* root, string s) {//建立二叉树 + root->data = s[0]; + int toward = 0; + stackarr;//用队列存放元素 + arr.push(root); + for (int i = 1; i < s.size(); i++) { + if (s[i] == '(') { + toward = 1; + } + else if (s[i] == ')') { + arr.pop(); + } + else if (s[i] == ',') { + toward = 2; + if (arr.size() != 1 && s[i - 1] != '(') { + arr.pop(); + } + } + else { + treenode* newnode = new treenode; + newnode->left = newnode->right = NULL; + if (toward == 1) {//左孩子 + arr.top()->left = newnode; + newnode->data = s[i]; + arr.push(newnode); + } + else {//右孩子 + arr.top()->right = newnode; + newnode->data = s[i]; + arr.push(newnode); + } + } + } +} + +void putouttree(treenode* root) //输出二叉树 +{ + if (root != NULL) + { + printf("%c", root->data); + if (root->left != NULL || root->right != NULL) + { + printf("("); + putouttree(root->left); + if (root->right != NULL) + printf(","); + putouttree(root->right); + printf(")"); + } + } +} + +treenode* searchChild(treenode* root,char target) {//层次遍历寻找节点 + queueq; + q.push(root); + while (!q.empty()) { + int length = q.size(); + for (int i = 0; i < length; i++) { + root = q.front(); + q.pop(); + if (root->data == target) { return root; } + if (root->left) { q.push(root->left); } + if (root->right) { q.push(root->right); } + } + } +} + + +int depth(treenode* node) {//求深度 + if (node == NULL) { return 0; } + return max(depth(node->left), depth(node->right)) + 1; +} + +void devastateTree(treenode* node) {//销毁树 + if (node != NULL) { + devastateTree(node->left); + devastateTree(node->right); + free(node); + } +} + +int main() { + string tree = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"; + treenode* root = new treenode; + createtree(root, tree);//建树 + cout << "输出树:"; + putouttree(root);//输出树 + cout << endl; + cout << "H节点的左孩子为:" << searchChild(root, 'H')->left->data << endl; + cout << "H节点的右孩子为:" << searchChild(root, 'H')->right->data << endl; + cout << "二叉树的高度为:" << depth(root); + devastateTree(root); + return 0; +} + diff --git "a/2203030044/chapter_7/\350\267\257\345\276\204\346\200\273\345\222\214.cpp" "b/2203030044/chapter_7/\350\267\257\345\276\204\346\200\273\345\222\214.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e6c9cd6f496dd1eb2a7a2764f9d861ec5f777224 --- /dev/null +++ "b/2203030044/chapter_7/\350\267\257\345\276\204\346\200\273\345\222\214.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + void dfs(TreeNode* node,int sum,int target,bool& judge) { + if (node == NULL) { return ; } + sum += node->val; + if (sum == target && !node->left && !node->right) { judge = true; } + if (node->left) { dfs(node->left, sum, target, judge); } + if (node->right) { dfs(node->right, sum, target, judge); } + } + bool hasPathSum(TreeNode* root, int targetSum) { + if (!root) { return false; } + bool judge = false; + dfs(root, 0, targetSum, judge); + return judge; + } +}; diff --git "a/2203030044/chapter_8/\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/2203030044/chapter_8/\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..116fe5f26a9c6165fe486e2492dab8ef360c0402 --- /dev/null +++ "b/2203030044/chapter_8/\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,41 @@ +class Solution { +private: + int dirs[8][2] = { {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1} }; +public: + int shortestPathBinaryMatrix(vector>& grid) { + //由4个方向变为了8个方向 + int n = (int)grid.size(); + + //grid[0][0] 和 grid[n - 1][n - 1]两个点为1 必不存在这样的路径 + if (grid[0][0] == 1 || grid[n - 1][n - 1] == 1) return -1; + + queue> q;//辅助队列 + q.emplace(pair(0, 0));//起始源点 + + //这里为什么要初始化为1,因为题目中的路径是包含源点的,1 还可以记录为当前BFS的第一层 + //依次类推第二次应该为2... 这样不仅能区分该坐标有没有被访问,还能记录坐标的层数! + grid[0][0] = 1; + + while (!q.empty()) { + //出队列 + auto [x, y] = q.front(); + q.pop(); + + //8个方向的移动 + for (auto& dir : dirs) { + int nx = x + dir[0], ny = y + dir[1];//得到下一个坐标点 + + //下一个坐标不越界且值为0 + if (nx >= 0 && nx < n && ny >= 0 && ny < n + && grid[nx][ny] == 0) { + + //同时该坐标0变为对应的层数 + grid[nx][ny] = grid[x][y] + 1; + q.emplace(pair(nx, ny));//加入队列 + } + } + } + + //如果grid[n - 1][n - 1]还是0,说明不存在这样的路径,否则返回层数即为最短路径 + return grid[n - 1][n - 1] == 0 ? -1 : grid[n - 1][n - 1]; + } diff --git "a/2203030044/chapter_8/\345\256\236\351\252\21414.cpp" "b/2203030044/chapter_8/\345\256\236\351\252\21414.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..41eb6744f709bccfe5bb1530017a3ac1d5f547c0 --- /dev/null +++ "b/2203030044/chapter_8/\345\256\236\351\252\21414.cpp" @@ -0,0 +1,198 @@ +#include +#include + +#define MAX_SIZE 100 +#define M 4 // 行数 +#define N 4 // 列数 + +/*----------------------------以下定义邻接表类型---------------------------*/ +typedef struct ANode // 边的结点结构类型 +{ + int i, j; // 该边的终点位置(i,j) + struct ANode *nextarc; // 指向下一条边的指针 +}ArcNode; + +typedef struct Vnode // 邻接表头结点的类型 +{ + ArcNode *firstarc; // 指向第一个相邻点 +}VNode; + +typedef struct +{ + VNode adjlist[M + 2][N + 2]; // 邻接表头结点数组 +}ALGraph; // 图的邻接表类型 + +typedef struct +{ + int i; // 当前方块的行号 + int j; // 当前方块的列号 +}Box; + +typedef struct +{ + Box data[MAX_SIZE]; + int length; // 路径长度 +}PathType; // 定义路径类型 + +int cnt = 0; +int visited[M + 2][N + 2] = {0}; + +/*-------------------由迷宫数组mg建立对应的邻接表G--------------------*/ +static void CreateAdj(ALGraph *&G, int mg[][N + 2]) +{ + int i, j; + int di; + int i1, j1; + ArcNode *p; + + G = (ALGraph *)malloc(sizeof(ALGraph)); + for(i = 0; i < M + 2; i++) // 给邻接表中所有头结点的指针域设置初值 + { + for(j = 0; j < N + 2; j++) + { + G->adjlist[i][j].firstarc = NULL; + } + } + + for(i = 1; i <= M; i++) // 检查mg中每个元素 + { + for(j = 1; j <= N; j++) + { + if(mg[i][j] == 0) + { + di = 0; + while(di < 4) + { + switch(di) + { + case 0: // 向上走 + i1 = i - 1; + j1 = j; + break; + case 1: // 向右走 + i1 = i; + j1 = j + 1; + break; + case 2: // 向下走 + i1 = i + 1; + j1 = j; + break; + case 3: // 向左走 + i1 = i; + j1 = j - 1; + break; + } + if(mg[i1][j1] == 0) // (i1,j1)为可走方块 + { + p = (ArcNode *)malloc(sizeof(ArcNode)); // 创建一个结点p + p->i = i1; + p->j = j1; + p->nextarc = G->adjlist[i][j].firstarc; // 将p结点链到链表后 + G->adjlist[i][j].firstarc = p; + } + di++; + } + } + } + } +} + +/*-----------------------输出邻接表G--------------------------*/ +static void DispAdj(ALGraph *G) +{ + int i, j; + ArcNode *p; + + for(i = 0; i < M + 2; i++) + { + for(j = 0; j < N + 2; j++) + { + printf(" [%d,%d]: ", i, j); + p = G->adjlist[i][j].firstarc; + while(p != NULL) + { + printf("(%d,%d)", p->i, p->j); + p = p->nextarc; + } + printf("\n"); + } + } +} + +/*---------------------销毁邻接表--------------------------*/ +static void DestroyAdj(ALGraph *&G) +{ + int i, j; + ArcNode *pre, *p; + + for(i = 0; i < M + 2; i++) + { + for(j = 0; j < N + 2; j++) + { + pre = G->adjlist[i][j].firstarc; + if(pre != NULL) + { + p = pre->nextarc; + while(p != NULL) + { + free(pre); + pre = p; + p = p->nextarc; + } + free(pre); + } + } + } + free(G); +} + +/*--------------在图G中采用DFS算法求(xi,yi)到(xe,ye)的所有路径-------------*/ +// path数组记录访问过的顶点序列,当找到出口时输出path中的访问序列 +static void FindPath(ALGraph *G, int xi, int yi, int xe, int ye, PathType path) +{ + ArcNode *p; + + visited[xi][yi] = 1; // 置已访问标记 + path.data[path.length].i = xi; // 设置起始方块的行号 + path.data[path.length].j = yi; // 设置起始方块的列号 + path.length++; + + if((xi == xe) && (yi == ye)) // 走到迷宫的出口(M,N)时,输出迷宫路径 + { + printf(" 迷宫路径%d: ", ++cnt); + for(int k = 0; k < path.length; k++) + { + printf("(%d,%d)", path.data[k].i, path.data[k].j); // 输出迷宫的行号和列号 + } + printf("\n"); + } + p = G->adjlist[xi][yi].firstarc; + while(p != NULL) + { + if(visited[p->i][p->j] == 0) // 若(p->i,p->j)方块未访问,则递归访问它 + FindPath(G, p->i, p->j, xe, ye, path); + p = p->nextarc; + } + visited[xi][yi] = 0; // 取消访问标记 +} + +int main(void) +{ + ALGraph *G; + int mg[M + 2][N + 2] = { // 一个迷宫:其四周加上均为1的外框 + {1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 1}, + {1, 0, 1, 0, 0, 1}, {1, 0, 0, 0, 1, 1}, + {1, 1, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1} + }; + + CreateAdj(G, mg); + printf("迷宫对应的邻接表:\n"); + DispAdj(G); // 输出邻接表 + PathType path; + path.length = 0; + printf("所有的迷宫路径:\n"); + FindPath(G, 1, 1, M, N, path); + DestroyAdj(G); + + return 0; +} \ No newline at end of file diff --git "a/2203030044/chapter_8/\345\256\236\351\252\2148.cpp" "b/2203030044/chapter_8/\345\256\236\351\252\2148.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c4b5ece6175619d01a1f82dbc3f207a5168045d9 --- /dev/null +++ "b/2203030044/chapter_8/\345\256\236\351\252\2148.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; +} diff --git "a/2203030044/chapter_8/\345\256\236\351\252\214\344\270\200.cpp" "b/2203030044/chapter_8/\345\256\236\351\252\214\344\270\200.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..04e1449d61ed114e19b319092a3485ef8285a1a1 --- /dev/null +++ "b/2203030044/chapter_8/\345\256\236\351\252\214\344\270\200.cpp" @@ -0,0 +1,142 @@ +#include +#include +#include +#define INF 32767 +#define MAXV 100 +using namespace std; +typedef char InfoType; + +typedef struct +{ + int no; + InfoType info; +}VertexType; +typedef struct +{ + int edges[MAXV][MAXV]; + int n,e; + VertexType vexs[MAXV]; +}MatGraph; + +typedef struct ANode +{ + int adjvex; + struct ANode *nextarc; + int weight; +}ArcNode; +typedef struct Vnode +{ + InfoType info; + int count; + ArcNode *firstarc; +}Vnode; +typedef struct +{ + Vnode adjlist[MAXV]; + int n,e; +}AdjGraph; + +void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e) +{ + int i,j; + g.n=n;g.e=e; + for(i=0;iadjlist[i].firstarc=NULL; + for(i=0;i=0;j--) + if(A[i][j]!=0&&A[i][j]!=INF) + { + p=(ArcNode*)malloc(sizeof(ArcNode)); + p->adjvex=j; + p->weight=A[i][j]; + p->nextarc=G->adjlist[i].firstarc; + G->adjlist[i].firstarc=p; + } + G->n=n;G->e=n; +} +void DispAdj(AdjGraph *G) +{ + ArcNode *p; + for(int i=0;in;i++) + { + p=G->adjlist[i].firstarc; + cout<adjvex<<"["<weight<<"]->"; + p=p->nextarc; + } + cout<<"^"<n;i++) + { + pre=G->adjlist[i].firstarc; + if(pre!=NULL) + { + p=pre->nextarc; + while(p!=NULL) + { + free(pre); + pre=p;p-p->nextarc; + } + free(pre); + } + } + free(G); +} + + +int main() +{ + MatGraph g; + AdjGraph *G; + int A[MAXV][MAXV]= + { + {0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,INF,9}, + {INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0} + }; + int n=6,e=10; + CreateMat(g,A,n,e); + cout<<"邻接矩阵为:"<>& trust) { + for (int i = 0; i < trust.size(); i++) { + arr[trust[i][0]]++; + brr[trust[i][1]]++; + } + int cnt = 0; + int index = 0; + for (int i = 1; i <= n; i++) { + if (arr[i] == 0 && brr[i] == n - 1) { + cnt++; + index = i; + } + } + if (cnt == 1) { return index; } + else { return -1; } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_8/\350\257\276\347\250\213\350\241\250.cpp" "b/2203030044/chapter_8/\350\257\276\347\250\213\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..29e9584b2da35267ddd421757a00e5320a346198 --- /dev/null +++ "b/2203030044/chapter_8/\350\257\276\347\250\213\350\241\250.cpp" @@ -0,0 +1,33 @@ +class Solution { +public: + bool canFinish(int n, vector>& edges) { + vector> g(n); + vector deg(n); + for (auto e : edges) { + int a = e[1], b = e[0]; + g[a].push_back(b); + deg[b] ++; + } + + queue q; + for (int i = 0; i < n; i ++) { + if (deg[i] == 0) { + q.push(i); + } + } + + int cnt = 0; + while (q.size()) { + auto t = q.front(); + q.pop(); + cnt ++; + for (auto v : g[t]) { + -- deg[v]; + if (deg[v] == 0) { + q.push(v); + } + } + } + return cnt == n; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_8/\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/2203030044/chapter_8/\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..6aaf60bad08f6a9d116443e7d47c3f5de078ef31 --- /dev/null +++ "b/2203030044/chapter_8/\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,30 @@ +#define INF 0x3f3f3f3f + +class Solution { +public: + int minCostConnectPoints(vector>& points) { + int n = points.size(); + int ans = 0; + vector vis(n); + vector min_dist(n, INF); + min_dist[0] = 0; + for (int i = 0; i < n; ++i) { + int u = -1; + int gmin = INF; + for (int j = 0; j < n; ++j) { + if (!vis[j] && min_dist[j] <= gmin) { + gmin = min_dist[j]; + u = j; + } + } + ans += gmin; + vis[u] = true; + for (int j = 0; j < n; ++j) + if (!vis[j]) { + int new_dist = abs(points[j][0] - points[u][0]) + abs(points[j][1] - points[u][1]); + min_dist[j] = min(min_dist[j], new_dist); + } + } + return ans; + } +}; diff --git "a/2203030044/chapter_9/\344\272\214\345\210\206\346\237\245\346\211\276.cpp" "b/2203030044/chapter_9/\344\272\214\345\210\206\346\237\245\346\211\276.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5afeca1cab529e6f74a96143756f462a3879940b --- /dev/null +++ "b/2203030044/chapter_9/\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 l = 0, r = nums.size() - 1; + while (l <= r) { + int mid = (r-l) / 2+l; + if (nums[mid] < target) { + l = mid + 1; + } + else if(nums[mid] == target){ + return mid; + }else{ + r=mid-1; + } + } + return -1; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_9/\344\272\214\345\217\211\346\220\234\347\264\242\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/2203030044/chapter_9/\344\272\214\345\217\211\346\220\234\347\264\242\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..1415b56b902b8363c99ccab7935b17868fa43561 --- /dev/null +++ "b/2203030044/chapter_9/\344\272\214\345\217\211\346\220\234\347\264\242\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,13 @@ +class Solution { +public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + if(root==NULL||root==p||root==q){ + return root; + } + TreeNode *left=lowestCommonAncestor(root->left,p,q); + TreeNode *right=lowestCommonAncestor(root->right,p,q); + if(left==NULL){return right;} + if(right==NULL){return left;} + return root; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_9/\345\256\236\351\252\2142.cpp" "b/2203030044/chapter_9/\345\256\236\351\252\2142.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2ccb67769e13d3b8b518dca22c164e1b896117f1 --- /dev/null +++ "b/2203030044/chapter_9/\345\256\236\351\252\2142.cpp" @@ -0,0 +1,25 @@ +#include +using namespace std; + +vectorarr = { 1,2,3,4,5,6,7,8,9,10 }; + +int halffind(int target) { + int l = 0, r = arr.size() - 1; + while (l <= r) { + int mid = (r - l) / 2 + l; + if (arr[mid] > target) { + r = mid - 1; + } + else if (arr[mid] < target){ + l = mid + 1; + } + else { + return mid; + } + + } +} +int main() { + cout << halffind(9); + return 0; +} diff --git "a/2203030044/chapter_9/\345\256\236\351\252\2149.cpp" "b/2203030044/chapter_9/\345\256\236\351\252\2149.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d2ccc0c6413c4af898b1978aa354c0d0c0874743 --- /dev/null +++ "b/2203030044/chapter_9/\345\256\236\351\252\2149.cpp" @@ -0,0 +1,75 @@ +#include +#include +#include + + +#define MAX_WORD (100) + +typedef struct tnode +{ + char ch; // 字符 + int cnt; // 出现次数 + struct tnode *lchild; // 左指针 + struct tnode *rchild; // 右指针 +}BSTNode; // 结点类型 + +/*-----------------采用递归方式向二叉排序树bst中插入一个字符----------------*/ +static void create_bst(BSTNode *&bt, char ch) // 指针的引用 +{ + if(bt == NULL) // bt为空,则建立一个新结点 + { + bt = (BSTNode *)malloc(sizeof(BSTNode)); + bt->ch = ch; + bt->cnt = 1; + bt->lchild = bt->rchild = NULL; + } + else if(ch == bt->ch) + bt->cnt++; + else if(ch < bt->ch) + create_bst(bt->lchild, ch); + else + create_bst(bt->rchild, ch); +} + +/*-----------------中序遍历二叉排序树bst----------------*/ +static void inorder(BSTNode *bt) +{ + if(bt != NULL) + { + inorder(bt->lchild); // 中序遍历左子树 + printf(" %c(%d)\n", bt->ch, bt->cnt); // 访问根结点 + inorder(bt->rchild); // 中序遍历右子树 + } +} + +/*-----------------销毁二叉排序树bst----------------*/ +static void destroy_bst(BSTNode *bt) +{ + if(bt != NULL) + { + destroy_bst(bt->lchild); + destroy_bst(bt->rchild); + free(bt); + } +} + +int main(int argc, char *argv[]) +{ + BSTNode *bt = NULL; + int i = 0; + char str[MAX_WORD]; + + printf("输入字符串:"); + gets(str); + while(str[i] != '\0') + { + create_bst(bt, str[i]); + i++; + } + printf("字符及出现次数:\n"); + inorder(bt); + printf("\n"); + destroy_bst(bt); + + return 0; +} diff --git "a/2203030044/chapter_9/\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/2203030044/chapter_9/\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..ac7fa194f6416e53d0286155683e63bfe999d70c --- /dev/null +++ "b/2203030044/chapter_9/\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,40 @@ +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + int left = (nums1.size() + nums2.size() + 1) / 2; + int right = (nums1.size() + nums2.size() + 2) / 2; + return (findmid(nums1, nums2, 0, 0, left) + findmid(nums1, nums2, 0, 0, right)) / 2; + } + + double findmid(vector& arr1, vector& arr2, int i, int j, int k) { + if (i >= arr1.size()) { + return arr2[j + k - 1]; + } + if (j >= arr2.size()) { + return arr1[i + k - 1]; + } + if (k == 1) { return min(arr1[i], arr2[j]); } + + int mid1 = 0; + int mid2 = 0; + if (i + k / 2 - 1 < arr1.size()) { + mid1 = arr1[i + k / 2 - 1]; + } + else { + mid1 = INT_MAX; + } + if (j + k / 2 - 1 < arr2.size()) { + mid2 = arr2[j + k / 2 - 1]; + } + else { + mid2 = INT_MAX; + } + + if (mid1 < mid2) { + return findmid(arr1, arr2, i + k / 2, j, k - k / 2); + } + else { + return findmid(arr1, arr2, i, j + k / 2, k - k / 2); + } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_9/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" "b/2203030044/chapter_9/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..4caa7ffe10ae32c125f63d8647cf0d1357e1c980 --- /dev/null +++ "b/2203030044/chapter_9/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" @@ -0,0 +1,28 @@ +class Solution { + public: + bool isBalanced(TreeNode* root) { + if (getdepth(root) != -1) { + return true; + } else { + return false; + } + } + int getdepth(TreeNode* node) { + if (node == NULL) { + return 0; + } + int leftlength = getdepth(node->left); + int rightlength = getdepth(node->right); + if (leftlength == -1) { + return -1; + } + if (rightlength == -1) { + return -1; + } + if (abs(leftlength - rightlength) > 1) { + return -1; + } else { + return max(leftlength, rightlength) + 1; + } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_9/\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/2203030044/chapter_9/\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..a41e04f3c3a9834c4660e6189f68389ff0c85360 --- /dev/null +++ "b/2203030044/chapter_9/\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,13 @@ +class Solution { +public: + int findKthLargest(vector& nums, int k) { + priority_queue, greater >q; + for (int i = 0; i < nums.size(); i++) { + q.push(nums[i]); + if (q.size() > k) { + q.pop(); + } + } + return q.top(); + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_9/\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\276\227\345\205\203\347\264\240.cpp" "b/2203030044/chapter_9/\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\276\227\345\205\203\347\264\240.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..deaaa3ad7e4ad7db037390de947ec5aa8c22e89e --- /dev/null +++ "b/2203030044/chapter_9/\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\276\227\345\205\203\347\264\240.cpp" @@ -0,0 +1,36 @@ +class RandomizedSet { +public: + unordered_mapm;//value->index + int index = 1; + vectorarr; + RandomizedSet() { + arr.push_back(0); + } + + bool insert(int val) { + if (m[val]) { return false; } + else { + m[val] = index; + index++; + arr.push_back(val); + return true; + } + } + + bool remove(int val) { + if (!m[val]) { return false; } + else { + m[arr.back()] = m[val]; + arr[m[val]] = arr.back(); + arr.pop_back(); + m.erase(val); + index--; + return true; + } + } + + int getRandom() { + int answer = arr[rand() % (index-1)+1]; + return answer; + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_9/\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" "b/2203030044/chapter_9/\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..cf56414de3d8674280769b06d83f97f788f74508 --- /dev/null +++ "b/2203030044/chapter_9/\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" @@ -0,0 +1,28 @@ +class Solution { +public: + int thirdMax(vector& nums) { + long max1 = LONG_MIN; + long max2 = LONG_MIN; + long max3 = LONG_MIN; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] == max1 || nums[i] == max2 || nums[i] == max3) { continue; } + if (max1 < nums[i]) { + max3 = max2; + max2 = max1; + max1 = nums[i]; + } + else if (nums[i]max2) { + max3 = max2; + max2 = nums[i]; + } + else if (nums[i]max3) { + max3 = nums[i]; + } + } + if(max3==LONG_MIN){ + return max1; + }else{ + return max3; + } + } +}; \ No newline at end of file diff --git "a/2203030044/chapter_9/\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" "b/2203030044/chapter_9/\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..87539123fd9fe4605ba913e6bda6592ddc48d679 --- /dev/null +++ "b/2203030044/chapter_9/\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" @@ -0,0 +1,23 @@ +class MyHashSet { +public: + vectorarr; + MyHashSet() { + + } + void add(int key) { + arr.push_back(key); + } + + void remove(int key) { + for (int i = 0; i < arr.size(); i++) { + if (arr[i] == key) { arr.erase(arr.begin() + i); i--; } + } + } + + bool contains(int key) { + for (int i = 0; i < arr.size(); i++) { + if (arr[i] == key) { return true;} + } + return false; + } +}; \ No newline at end of file