diff --git "a/2209040080/cahpter 10/lc\346\216\222\345\272\217\351\223\276\350\241\250.cpp" "b/2209040080/cahpter 10/lc\346\216\222\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0cc0cde396d69db1db9424b7a2d914a573e2e13c --- /dev/null +++ "b/2209040080/cahpter 10/lc\346\216\222\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,57 @@ +class Solution { +public: + ListNode* sortList(ListNode* head) { + int len=0; + ListNode *ptr=head; + while(ptr){ + len++; + ptr=ptr->next; + } + ListNode dummy(0,head); + for(int subLen=1;subLennext;++i){ + cur=cur->next; + } + ListNode *l2=cur->next; + cur->next=nullptr; + cur=l2; + for(int i=1;inext;++i){ + cur=cur->next; + } + ListNode *next=nullptr; + if(cur){ + next=cur->next; + cur->next=nullptr; + } + auto p=mergeList(l1,l2); + prev->next=p.first; + prev=p.second; + cur=next; + } + } + return dummy.next; + } +private: + pair mergeList(ListNode *l1,ListNode *l2){ + ListNode dummy,*ptr=&dummy; + while(l1&&l2){ + if(l1->valval){ + ptr->next=l1; + l1=l1->next; + } + else{ + ptr->next=l2; + l2=l2->next; + } + ptr=ptr->next; + } + ptr->next=l1?l1:l2; + while(ptr->next){ + ptr=ptr->next; + } + return {dummy.next,ptr}; + } +}; \ No newline at end of file diff --git "a/2209040080/cahpter 7/lc N \345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" "b/2209040080/cahpter 7/lc 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..e4e5bd4f251badf74609a34fffa5daee37a986e3 --- /dev/null +++ "b/2209040080/cahpter 7/lc 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,18 @@ +class Solution { +public: + vector preorder(Node* root) { + vector res; + stack st; + if(root == nullptr) return res; + st.push(root); + while(!st.empty()){ + Node* node = st.top(); + st.pop(); + res.push_back(node->val); + for(int i = node->children.size() - 1; i >= 0; --i){ + st.push(node->children[i]); + } + } + return res; + } +}; \ No newline at end of file diff --git "a/2209040080/cahpter 7/lc \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/2209040080/cahpter 7/lc \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..2f566995bb8e5b55946c1c35443af4d71b6f6172 --- /dev/null +++ "b/2209040080/cahpter 7/lc \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,33 @@ +class Solution { +public: + int i = 0; + vector res; + bool DFS(TreeNode* root, vector& voyage) { + if (!root) { + return true; + } + if (root->val!=voyage[i]) { + return false; + } + i++; + + if (DFS(root->left, voyage) && DFS(root->right, voyage)) { + return true; + } + if (DFS(root->right, voyage) && DFS(root->left, voyage)) { + res.push_back(root->val); + return true; + } + return false; + } + + vector flipMatchVoyage(TreeNode* root, vector& voyage) { + if(DFS(root, voyage)) { + return res; + } + res.erase(res.begin(),res.end()); + res.push_back(-1); + return res; + } +}; + diff --git "a/2209040080/cahpter 7/lc\344\272\214\345\217\211\346\240\221\346\234\200\345\244\247\345\256\275\345\272\246.cpp" "b/2209040080/cahpter 7/lc\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..f34cfb8f66eb4fabf0ebe7ced880363aa82008f0 --- /dev/null +++ "b/2209040080/cahpter 7/lc\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,32 @@ +class Solution { +public: + int widthOfBinaryTree(TreeNode* root) { + int maxw=0; + queue que; + que.emplace(root); + root->val=0; + while(!que.empty()) { + int size=que.size(); + int left=que.front()->val; + int right; + bool flag=true; + long long shift=0; + while(size--) { + TreeNode *temp=que.front();que.pop(); + if(size==0) right=temp->val; + if(temp->left) { + if(flag) {shift=left*2;flag=false;} + temp->left->val=temp->val-shift+temp->val; + que.emplace(temp->left); + } + if(temp->right) { + if(flag) {shift=left*2+1;flag=false;} + temp->right->val=temp->val+1-shift+temp->val; + que.emplace(temp->right); + } + } + maxw=max(maxw,right-left+1); + } + return maxw; + } +}; \ No newline at end of file diff --git "a/2209040080/cahpter 7/lc\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/2209040080/cahpter 7/lc\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..09c6e0f449098f8a80056f3191e85a6109d2e011 --- /dev/null +++ "b/2209040080/cahpter 7/lc\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,26 @@ +class Solution { +public: + vector postorderTraversal(TreeNode *root) { + if (!root) return {}; + vector vec; + stack stk; + TreeNode *prev = nullptr; + auto node = root; + while (!stk.empty() || node) { + while (node) { + stk.emplace(node); + node = node->left; + } + node = stk.top(); stk.pop(); + if (node->right && node->right != prev) { + stk.emplace(node); + node = node->right; + } else { + vec.emplace_back(node->val); + prev = node; + node = nullptr; + } + } + return vec; + } +}; \ No newline at end of file diff --git "a/2209040080/cahpter 7/lc\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/2209040080/cahpter 7/lc\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..a399507bdf790700c75f3adf1249e6c2c5f95e9f --- /dev/null +++ "b/2209040080/cahpter 7/lc\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,21 @@ +class Solution { +public: + vector> levelOrder(TreeNode* root) { + vector> res; + queue queue; + if (root != nullptr) queue.push(root); + while (!queue.empty()) { + int n = queue.size(); + vector level; + for (int i = 0; i < n; ++i) { + TreeNode* node = queue.front(); + queue.pop(); + level.push_back(node->val); + if (node->left != nullptr) queue.push(node->left); + if (node->right != nullptr) queue.push(node->right); + } + res.push_back(level); + } + return res; + } +}; \ No newline at end of file diff --git "a/2209040080/cahpter 7/lc\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/2209040080/cahpter 7/lc\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..38140e6ae4ee67432ad724ee026230c410d6519d --- /dev/null +++ "b/2209040080/cahpter 7/lc\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,6 @@ +class Solution { +public: + int maxDepth(TreeNode* root) { + return root?max(maxDepth(root->left),maxDepth(root->right))+1:0; + } +}; \ No newline at end of file diff --git "a/2209040080/cahpter 7/lc\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/2209040080/cahpter 7/lc\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..696338226cef7a29f3a5dbe5eb5c05e7a06cb8a2 --- /dev/null +++ "b/2209040080/cahpter 7/lc\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,22 @@ +class Solution { +public: + unordered_map mid; + TreeNode* build(vector& preorder,vector&inorder,int begin1,int end1,int begin2,int end2,int n){ + if(end1+1-begin1==0) + return nullptr; + TreeNode *node=new TreeNode(preorder[begin1]); + int index=mid[preorder[begin1]]; + int leftLen=index-begin2; + int rightLen=end2-index; + node->left=build(preorder,inorder,begin1+1,begin1+leftLen,begin2,index-1,n); + node->right=build(preorder,inorder,begin1+leftLen+1,end1,index+1,end2,n); + return node; + } + TreeNode* buildTree(vector& preorder, vector& inorder) { + int n1=preorder.size(); + int n2=inorder.size(); + for(int i=0;i>& grid) { + n = grid.size(); + if (grid[0][0] == 1 || grid[n - 1][n - 1] == 1) return -1; + + closedList.resize(n * n, nullptr); + + priority_queue pq; + Node* startNode = new Node(0, 0, 0, 0, heuristic(0, 0)); + pq.push(*startNode); + closedList[0] = startNode; + while (!pq.empty()) { + Node node = pq.top(); + pq.pop(); + + if (node.x == n - 1 && node.y == n - 1) + return node.g + 1; + + getNeighbors(pq, grid, node); + } + + return -1; + } + +private: + vector> dirs = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, + {-1, 1}, {1, 1}, {-1, -1}, {1, -1} }; + + struct Node { + int key; + int x; + int y; + int g; + int h; + Node(int key, int x, int y, int g, int h) : key(key), x(x), y(y), g(g), h(h) { } + friend bool operator< (const Node& lhs, const Node& rhs) { + return (lhs.g + lhs.h) > (rhs.g + rhs.h); + } + }; + int n; + vector closedList; + int heuristic(int x, int y) { + int dx = n - 1 - x; + int dy = n - 1 - y; + return (dx + dy) - min(dx, dy); + } + + + void getNeighbors(priority_queue& pq, const vector>& grid, const Node& node) { + for (auto&& dir : dirs) { + int newX = node.x + dir[0], newY = node.y + dir[1]; + if (newX >= 0 && newX < n && newY >= 0 && newY < n && grid[newX][newY] == 0) { + int key = newX * n + newY; + + if (closedList[key] == nullptr) { + Node* neighbor = new Node(key, newX, newY, node.g + 1, heuristic(newX, newY)); + pq.push(*neighbor); + closedList[key] = neighbor; + } + + else if (closedList[key]->g > node.g + 1) { + closedList[key]->g = node.g + 1; + pq.push(*closedList[key]); + } + } + } + } +}; \ No newline at end of file diff --git "a/2209040080/cahpter 9/lc\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" "b/2209040080/cahpter 9/lc\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2c6d80c8f85989fa52aa4fdd2aa32768b502fa0d --- /dev/null +++ "b/2209040080/cahpter 9/lc\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.cpp" @@ -0,0 +1,15 @@ +class Solution { +public: + bool isBalanced(TreeNode* root) { + return recur(root) != -1; + } +private: + int recur(TreeNode* root) { + if (root == nullptr) return 0; + int left = recur(root->left); + if (left == -1) return -1; + int right = recur(root->right); + if (right == -1) return -1; + return abs(left - right) < 2 ? max(left, right) + 1 : -1; + } +}; \ No newline at end of file diff --git "a/2209040080/chapter 4/lc\345\256\236\347\216\260strStr().cpp." "b/2209040080/chapter 4/lc\345\256\236\347\216\260strStr().cpp." new file mode 100644 index 0000000000000000000000000000000000000000..fb55b90cb7219746b1924f0eaa7aa401131f9810 --- /dev/null +++ "b/2209040080/chapter 4/lc\345\256\236\347\216\260strStr().cpp." @@ -0,0 +1,29 @@ +#include +#define NULL 0 + +using namespace std; + +char *strStr(const char *str1, const char *str2) { + if (*str2) { + while (*str1) { + for (int i = 0; *(str1 + i) == *(str2 + i); i++) { + if (!*(str2 + i + 1)) { + return (char *) str1; + } + } + str1++; + } + return NULL; + } else { + return (char *) str1; + } +} + +int main() { + char str1[20] = "453456789123456789"; + char str2[6] = "34567"; + char *str = strStr(str1, str2); + cout << str << endl; + return 0; +} + diff --git "a/2209040080/chapter 4/lc\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp." "b/2209040080/chapter 4/lc\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..e1781d9faa7dc9a0d64eeddb573e6361048106fb --- /dev/null +++ "b/2209040080/chapter 4/lc\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,21 @@ +class Solution { +public: + bool repeatedSubstringPattern(string s) { + int j = 0; + int n = s.size(); + vector next(n); + for(int i = 1; i < n; i++){ + while(s[i] != s[j] && j > 0){ + j = next[j - 1]; + } + if(s[i] == s[j]){ + j++; + } + next[i] = j; + } + if(next[n - 1] * 2 >= n && n % (n - next[n - 1]) == 0) + return 1; + else + return 0; + } +}; \ No newline at end of file diff --git "a/2209040080/chapter 4/lc\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" "b/2209040080/chapter 4/lc\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..211c88105c597505475037f4b8f1d99a02797dc5 --- /dev/null +++ "b/2209040080/chapter 4/lc\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" @@ -0,0 +1,44 @@ +class Solution { +public: + bool isPalindrome(string s) { + for (int i = s.size() - 1; i >= 0; i--) { + if (!isValid(s[i])) { + s.erase(i, 1); + } + else if (isUpper(s[i])) { + s[i] += 32; + } + } + int left = 0, right = s.size() - 1; + while (left < right) { + if (s[left++] != s[right--]) { + return false; + } + } + return true; + } + +private: + bool isValid(char ch) { + if (ch >= '0' && ch <= '9') { + return true; + } + else if (ch >= 'a' && ch <= 'z') { + return true; + } + else if (ch >= 'A' && ch <= 'Z') { + return true; + } + else { + return false; + } + } + bool isUpper(char ch) { + if (ch >= 'A' && ch <= 'Z') { + return true; + } + else { + return false; + } + } +}; \ No newline at end of file diff --git "a/2209040080/chapter 5/lc\345\233\236\346\226\207\351\223\276\350\241\250.cpp" "b/2209040080/chapter 5/lc\345\233\236\346\226\207\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..99e7712263efe64ed87bc3c52360221918a75485 --- /dev/null +++ "b/2209040080/chapter 5/lc\345\233\236\346\226\207\351\223\276\350\241\250.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + bool isPalindrome(ListNode* head) { + if (head == nullptr || head->next == nullptr) return true; + ListNode* p = head; + function digui = [&](ListNode* cur) { + if (cur->next == nullptr) { + if (p->val == cur->val) { + p = p->next; + return true; + } + else return false; + } + return digui(cur->next) && cur->val == p->val ? (p = p->next, true) : false; + }; + return digui(head); + } +}; \ No newline at end of file