From e4729cd9abf20b90ddff56328876621d7a185eee Mon Sep 17 00:00:00 2001 From: MingLL Date: Wed, 23 Sep 2020 21:11:25 +0800 Subject: [PATCH 01/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E4=B8=AD=E7=AE=80=E5=8D=95=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\346\225\260\344\271\213\345\222\214.cpp" | 25 ++++++ ...3\350\275\254\346\225\260\347\273\204.cpp" | 79 +++++++++++++++++++ ...1\345\272\217\351\223\276\350\241\250.cpp" | 38 +++++++++ ...4\351\207\215\345\244\215\351\241\271.cpp" | 25 ++++++ ....\347\247\273\345\212\250\351\233\266.cpp" | 39 +++++++++ "Week_01/66.\345\212\240\344\270\200.cpp" | 25 ++++++ ...1\345\272\217\346\225\260\347\273\204.cpp" | 26 ++++++ 7 files changed, 257 insertions(+) create mode 100644 "Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" create mode 100644 "Week_01/189.\346\227\213\350\275\254\346\225\260\347\273\204.cpp" create mode 100644 "Week_01/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.cpp" create mode 100644 "Week_01/26.\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.cpp" create mode 100644 "Week_01/283.\347\247\273\345\212\250\351\233\266.cpp" create mode 100644 "Week_01/66.\345\212\240\344\270\200.cpp" create mode 100644 "Week_01/88.\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" diff --git "a/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" new file mode 100644 index 00000000..6171cfd8 --- /dev/null +++ "b/Week_01/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" @@ -0,0 +1,25 @@ +/* + * @lc app=leetcode.cn id=1 lang=cpp + * + * [1] 两数之和 + */ + +// @lc code=start +class Solution { +public: + vector twoSum(vector& nums, int target) { + map map; + vector res(2); + for(int i =0; i < nums.size(); i++) { + if(map.count(target - nums[i]) >0) { + res[1] = i; + res[0] = map[target -nums[i]]; + break; + } + map[nums[i]] = i; + } + return res; + } +}; +// @lc code=end + diff --git "a/Week_01/189.\346\227\213\350\275\254\346\225\260\347\273\204.cpp" "b/Week_01/189.\346\227\213\350\275\254\346\225\260\347\273\204.cpp" new file mode 100644 index 00000000..6cde2c84 --- /dev/null +++ "b/Week_01/189.\346\227\213\350\275\254\346\225\260\347\273\204.cpp" @@ -0,0 +1,79 @@ +/* + * @lc app=leetcode.cn id=189 lang=cpp + * + * [189] 旋转数组 + */ + +// @lc code=start +class Solution { +public: + void rotate(vector& nums, int k) { + ring_substitution(nums, k); + } +/** + * 暴力破解法。 + * 时间复杂度为O(n*k) + * 空间复杂度为O(1) + * 超时解法 + */ + void violent_solution(vector& nums, int k) { + int size = nums.size(); + for(int i = 0; i < k; i++) { + int num = nums[size -1]; + for(int j = size -1; j >0; j--) { + nums[j] = nums[j -1]; + } + nums[0] = num; + } + } + +/** + * 反转解法 + * 时间复杂度为O(n) + * 空间复杂度为O(1) + */ + void reverse(vector& nums, int k) { + int num = k % nums.size(); + reverse_t(nums, 0, nums.size() -1); + reverse_t(nums, 0, num -1); + reverse_t(nums, num, nums.size() -1); + + } + void reverse_t(vector& nums, int start, int end) { + while(start > end) { + int temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + start++; + end--; + } + } + +/** + * 环形移动 + * 时间复杂度为O(n) + * 空间复杂度为O(1) +*/ + void ring_substitution(vector& nums, int k) { + int size = nums.size(); + k %= size; + //数组中已经遍历过的数字个数 + int count =0; + for(int start = 0; count < size; start++) { + int current = start; + //往前移动的数字 + int prev = nums[start]; + do{ + int next = (current + k) % size; + // 保存被替换的数字 + int temp = nums[next]; + nums[next] = prev; + prev = temp; + current = next; + count++; + }while(start != current); + } + } +}; +// @lc code=end + diff --git "a/Week_01/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.cpp" "b/Week_01/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 00000000..ad1035ae --- /dev/null +++ "b/Week_01/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,38 @@ +/* + * @lc app=leetcode.cn id=21 lang=cpp + * + * [21] 合并两个有序链表 + */ + +// @lc code=start +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode* mergeHead = new ListNode(-1); + ListNode* prev = mergeHead; + while(l1 != nullptr && l2 != nullptr) { + if(l1->val < l2->val) { + prev-> next = l1; + l1 = l1->next; + } else { + prev->next = l2; + l2 = l2->next; + } + prev = prev->next; + } + prev->next = l1 != nullptr ? l1 : l2; + return mergeHead->next; + } +}; +// @lc code=end + diff --git "a/Week_01/26.\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.cpp" "b/Week_01/26.\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.cpp" new file mode 100644 index 00000000..fe7c6ef8 --- /dev/null +++ "b/Week_01/26.\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.cpp" @@ -0,0 +1,25 @@ +/* + * @lc app=leetcode.cn id=26 lang=cpp + * + * [26] 删除排序数组中的重复项 + */ + +// @lc code=start +class Solution { +public: + int removeDuplicates(vector& nums) { + if(nums.size() == 0) { + return 0; + } + int i = 0; + for(int j = 1; j < nums.size(); j++) { + if(nums[i] != nums[j]) { + i++; + } + nums[i] = nums[j]; + } + return i +1; + } +}; +// @lc code=end + diff --git "a/Week_01/283.\347\247\273\345\212\250\351\233\266.cpp" "b/Week_01/283.\347\247\273\345\212\250\351\233\266.cpp" new file mode 100644 index 00000000..0cca3940 --- /dev/null +++ "b/Week_01/283.\347\247\273\345\212\250\351\233\266.cpp" @@ -0,0 +1,39 @@ +/* + * @lc app=leetcode.cn id=283 lang=cpp + * + * [283] 移动零 + */ + +// @lc code=start +class Solution { +public: + void moveZeroes(vector& nums) { + solution2(nums); + } + + void solution1(vector& nums) { + int j =0; + for(int i = 0; i < nums.size(); i++) { + if(nums[i] != 0) { + nums[j] = nums[i]; + if(j != i) { + nums[i] = 0; + } + j++; + } + } + } + + void solution2(vector& nums) { + int size = nums.size(); + for(int i =0, j =0; i < size; i++) { + if (nums[i] != 0) { + swap(nums[i],nums[j]); + j++; + } + } + } + +}; +// @lc code=end + diff --git "a/Week_01/66.\345\212\240\344\270\200.cpp" "b/Week_01/66.\345\212\240\344\270\200.cpp" new file mode 100644 index 00000000..2226dfbd --- /dev/null +++ "b/Week_01/66.\345\212\240\344\270\200.cpp" @@ -0,0 +1,25 @@ +/* + * @lc app=leetcode.cn id=66 lang=cpp + * + * [66] 加一 + */ + +// @lc code=start +class Solution { +public: + vector plusOne(vector& digits) { + int size = digits.size() -1; + for(int i =size ;i >= 0; i--) { + digits[i]++; + digits[i] %=10; + if(digits[i] != 0) { + return digits; + } + } + vector res(digits.size() +1, 0); + res[0] = 1; + return res; + } +}; +// @lc code=end + diff --git "a/Week_01/88.\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/Week_01/88.\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 00000000..cbd4478b --- /dev/null +++ "b/Week_01/88.\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,26 @@ +/* + * @lc app=leetcode.cn id=88 lang=cpp + * + * [88] 合并两个有序数组 + */ + +// @lc code=start +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + int length = m + n -1; + m--,n--; + while (m >=0 && n >=0) { + if(nums1[m] > nums2[n]) { + nums1[length--] = nums1[m--]; + } else { + nums1[length--] = nums2[n--]; + } + } + while(n >=0) { + nums1[length--] = nums2[n--]; + } + } +}; +// @lc code=end + From 96ad2e7060fc45b3d90e0a0e207bcedbb2ca4e96 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 27 Sep 2020 14:37:13 +0800 Subject: [PATCH 02/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AD=A6=E4=B9=A0?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_01/README.md | 71 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/Week_01/README.md b/Week_01/README.md index 50de3041..e4515bf6 100644 --- a/Week_01/README.md +++ b/Week_01/README.md @@ -1 +1,70 @@ -学习笔记 \ No newline at end of file +# 学习笔记 +## ♥启航♥ +   最近在几场大厂的面试中,都被问及到了算法有关方面的知识点,但是自己在上学期间对算法的重视程度真的太低,导致面试的失利。现在决心狠抓算法,对算法的知识要有一个完整的知识体系。要从开始恐惧做题,到最后的爱上做题;从最开始的没有思路,到最后的灵感并发。但是不是短时间内就可以提升的,需要长期的训练。希望在这次的算法训练营中,可以收获算法的知识。 + ## 时间、空间复杂度 +   时间复杂度和空间复杂度是衡量算法好坏的标准,每个程序猿都要学会分析自己代码的时间和空间复杂度,可能不需要复杂的公式推导,但是对于大概的时间和空间复杂度要有一个准确的判断。 + ### 常见的几种时间复杂度 + | | | + |----|-----| + |O(1)|常数复杂度| + |O(log n)|对数复杂度| + |O(n)|线性时间复杂度| + |O(n^2)|平方时间复杂度| + |O(n^3)|立方时间复杂度| + |O(2^n)|指数时间复杂度| + |O(n!) |阶乘时间复杂度| + ### 判断时间复杂度的方法 + - 只关注循环执行次数最多的一段代码 +   大O这种时间复杂度只是表示一种变化趋势,通常会忽视公式中的常量 + - 加法法则:总复杂度等于量级最大的那段代码的复杂度 + - 乘法法则: 嵌套代码的复杂度等于嵌套内外代码复杂度的乘积 + - 对于递归的代码,画出递归树来计算,一般为指数时间复杂度 + ### 判断空间复杂度的方法 + - 看申请的空间大小即 数组的长度、递归的深度 + ## 数组、链表、跳表 + ### 数组 +   一块连续的存储空间,存储同类型的一组元素 + #### 特点 + - 支持随机访问数组内的元素(访问数组内的元素 时间复杂度为O(1)) + - 当删除数组中下标为i的元素时,需要移动n-i-1个元素。(n为数组的长度) + - 当向数组中插入元素的时,需要移动n-i个元素。(n为数组的长度) + ### 链表 +    使用指针链接一些零散的内存空间,存储同一类型的一组元素 + #### 特点 + - 当删除和插入元素的时候只需要三个步骤。 + - 当要访问一个元素的时候必须遍历链表 + #### 种类 + - 单链表 + 删除某个节点的时候,需要遍历链表得到删除的链表的前序元素,在删除元素。 + - 循环单链表 + 尾节点和头节点相连的链表,判断链表是否遍历完毕的条件为当链表是否遍历到头指针 + - 双链表 + 每一个元素都含有指向前序节点的指针和后序节点的指针,可以优化单链表中删除元素时需要遍历得到删除元素的前序节点。LUR就是使用双链表。 + - 循环双链表 + 头指针的前序指针指向尾节点,尾节点的后续指针指向头节点。 + ### 跳表 +   为了加快链表中查找元素的速度,在链表中添加索引。 + #### 特点 + - 在**排序**的链表中,可以添加索引,加快索引速度 + - 结构含有一个向下指针指向真实存储的元素。 + - 可以建立多级索引 + ## 栈、队列、优先队列、双端队列 + ### 栈 +   类似生活中羽毛球桶中拿取羽毛球的方式,只能拿取最后的那个元素。 + #### 特点 + - 先入后出,后入先出。 + #### 特殊结构 + - 最大栈、最小栈 + 快速获得栈中的最小元素和最大元素。维护两个栈,一个栈保存真实数据,一个栈保存最大或最小值。 + - 单调栈 + 栈中的元素按照一定的顺序排列.[例题](https://leetcode-cn.com/problems/largest-rectangle-in-histogram/). + ### 队列 +   类似生活中排队的例子 + #### 特点 + - FIFO(First in First out) + ### 优先队列 +   根据一定规则排序队列。 + + ### 双端队列 +   两边都可以进出元素。 + \ No newline at end of file From 6b84739a66958004c865a4803159a51491680611 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 27 Sep 2020 23:21:04 +0800 Subject: [PATCH 03/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=20=E4=B8=AD=E7=AD=89?= =?UTF-8?q?=20=E5=92=8C=20=E5=9B=B0=E9=9A=BE=E7=9A=84=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....\346\216\245\351\233\250\346\260\264.cpp" | 27 +++++ ...4\347\253\257\351\230\237\345\210\227.cpp" | 101 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 "Week_01/42.\346\216\245\351\233\250\346\260\264.cpp" create mode 100644 "Week_01/641.\350\256\276\350\256\241\345\276\252\347\216\257\345\217\214\347\253\257\351\230\237\345\210\227.cpp" diff --git "a/Week_01/42.\346\216\245\351\233\250\346\260\264.cpp" "b/Week_01/42.\346\216\245\351\233\250\346\260\264.cpp" new file mode 100644 index 00000000..28f9aa3f --- /dev/null +++ "b/Week_01/42.\346\216\245\351\233\250\346\260\264.cpp" @@ -0,0 +1,27 @@ +/* + * @lc app=leetcode.cn id=42 lang=cpp + * + * [42] 接雨水 + */ + +// @lc code=start +class Solution { +public: + int trap(vector& height) { + int left =0, right = height.size() -1; + int max_left = 0, max_right =0; + int res =0; + while (left < right) { + if(height[left] < height[right]) { + height[left] > max_left ? max_left = height[left] : res += max_left - height[left]; + left++; + } else { + height[right] > max_right ? max_right = height[right] : res += max_right - height[right]; + right--; + } + } + return res; + } +}; +// @lc code=end + diff --git "a/Week_01/641.\350\256\276\350\256\241\345\276\252\347\216\257\345\217\214\347\253\257\351\230\237\345\210\227.cpp" "b/Week_01/641.\350\256\276\350\256\241\345\276\252\347\216\257\345\217\214\347\253\257\351\230\237\345\210\227.cpp" new file mode 100644 index 00000000..d2610715 --- /dev/null +++ "b/Week_01/641.\350\256\276\350\256\241\345\276\252\347\216\257\345\217\214\347\253\257\351\230\237\345\210\227.cpp" @@ -0,0 +1,101 @@ +/* + * @lc app=leetcode.cn id=641 lang=cpp + * + * [641] 设计循环双端队列 + */ + +// @lc code=start +class MyCircularDeque { +private: + vector arr; + int front; + int end; + int capacity; +public: + /** Initialize your data structure here. Set the size of the deque to be k. */ + MyCircularDeque(int k) { + capacity = k +1; + arr.assign(capacity, 0); + front =0; + end =0; + } + + /** Adds an item at the front of Deque. Return true if the operation is successful. */ + bool insertFront(int value) { + if(isFull()) { + return false; + } + front = (front -1 + capacity) %capacity; + arr[front] = value; + return true; + } + + /** Adds an item at the rear of Deque. Return true if the operation is successful. */ + bool insertLast(int value) { + if (isFull()) { + return false; + } + arr[end] = value; + end = (end +1) % capacity; + return true; + } + + /** Deletes an item from the front of Deque. Return true if the operation is successful. */ + bool deleteFront() { + if(isEmpty()) { + return false; + } + front = (front +1) % capacity; + return true; + } + + /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ + bool deleteLast() { + if(isEmpty()) { + return false; + } + end = (end -1 + capacity) % capacity; + return true; + } + + /** Get the front item from the deque. */ + int getFront() { + if(isEmpty()) { + return -1; + } + return arr[front]; + } + + /** Get the last item from the deque. */ + int getRear() { + if(isEmpty()) { + return -1; + } + return arr[(end -1 +capacity) %capacity]; + } + + /** Checks whether the circular deque is empty or not. */ + bool isEmpty() { + return front == end; + } + + /** Checks whether the circular deque is full or not. */ + bool isFull() { + return (end +1) % capacity == front; + } +}; + +/** + * Your MyCircularDeque object will be instantiated and called as such: + * MyCircularDeque* obj = new MyCircularDeque(k); + * bool param_1 = obj->insertFront(value); + * bool param_2 = obj->insertLast(value); + * bool param_3 = obj->deleteFront(); + * bool param_4 = obj->deleteLast(); + * int param_5 = obj->getFront(); + * int param_6 = obj->getRear(); + * bool param_7 = obj->isEmpty(); + * bool param_8 = obj->isFull(); + */ +// @lc code=end + From 91d45a63e6e969f4b6990e2451b6f41825151720 Mon Sep 17 00:00:00 2001 From: MingLL Date: Mon, 28 Sep 2020 21:14:16 +0800 Subject: [PATCH 04/23] =?UTF-8?q?"=E5=AE=8C=E6=88=90=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E8=8A=82=E8=AF=BE=E4=BD=9C=E4=B8=9A"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\346\225\260\344\271\213\345\222\214.cpp" | 26 +++++++++++++++++ ...5\345\274\202\344\275\215\350\257\215.cpp" | 28 +++++++++++++++++++ ...5\350\257\215\345\210\206\347\273\204.cpp" | 25 +++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 "Week_02/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" create mode 100644 "Week_02/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" create mode 100644 "Week_02/49.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.cpp" diff --git "a/Week_02/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/Week_02/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" new file mode 100644 index 00000000..8cf7a7e4 --- /dev/null +++ "b/Week_02/1.\344\270\244\346\225\260\344\271\213\345\222\214.cpp" @@ -0,0 +1,26 @@ +/* + * @lc app=leetcode.cn id=1 lang=cpp + * + * [1] 两数之和 + */ + +// @lc code=start +class Solution { +public: + vector twoSum(vector& nums, int target) { + map map; + vector res(2); + for(int i =0; i < nums.size(); i++) { + if(map.count(target - nums[i])>0) { + res[0] = map[target - nums[i]]; + res[1] = i; + break; + } + map[nums[i]] = i; + } + return res; + } + +}; +// @lc code=end + diff --git "a/Week_02/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" "b/Week_02/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" new file mode 100644 index 00000000..c2e04aeb --- /dev/null +++ "b/Week_02/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=242 lang=cpp + * + * [242] 有效的字母异位词 + */ + +// @lc code=start +class Solution { +public: + bool isAnagram(string s, string t) { + if(s.length() != t.length()) { + return false; + } + vector vectors(26,0); + for(int i =0; i< s.length(); i++) { + vectors[s[i] - 'a']++; + vectors[t[i] - 'a']--; + } + for(int a : vectors) { + if(a != 0) { + return false; + } + } + return true; + } +}; +// @lc code=end + diff --git "a/Week_02/49.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.cpp" "b/Week_02/49.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.cpp" new file mode 100644 index 00000000..89e25ffc --- /dev/null +++ "b/Week_02/49.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.cpp" @@ -0,0 +1,25 @@ +/* + * @lc app=leetcode.cn id=49 lang=cpp + * + * [49] 字母异位词分组 + */ + +// @lc code=start +class Solution { +public: + vector> groupAnagrams(vector& strs) { + vector> res; + unordered_map> map; + for(string s :strs) { + string t = s; + sort(t.begin(), t.end()); + map[t].push_back(s); + } + for(auto& a: map) { + res.push_back(a.second); + } + return res; + } +}; +// @lc code=end + From 28a73b9b34499abf61fb17d6b1dc1c9a52f01b60 Mon Sep 17 00:00:00 2001 From: MingLL Date: Tue, 6 Oct 2020 20:03:31 +0800 Subject: [PATCH 05/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\345\272\217\351\201\215\345\216\206.cpp" | 51 ++++++++++++++ "Week_02/263.\344\270\221\346\225\260.cpp" | 19 ++++++ "Week_02/264.\344\270\221\346\225\260-ii.cpp" | 44 ++++++++++++ ...0\351\242\221\345\205\203\347\264\240.cpp" | 28 ++++++++ ...2\345\272\217\351\201\215\345\216\206.cpp" | 53 +++++++++++++++ ...5\345\272\217\351\201\215\345\216\206.cpp" | 68 +++++++++++++++++++ ...5\345\272\217\351\201\215\345\216\206.cpp" | 50 ++++++++++++++ 7 files changed, 313 insertions(+) create mode 100644 "Week_02/144.\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" create mode 100644 "Week_02/263.\344\270\221\346\225\260.cpp" create mode 100644 "Week_02/264.\344\270\221\346\225\260-ii.cpp" create mode 100644 "Week_02/347.\345\211\215-k-\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" create mode 100644 "Week_02/429.n\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" create mode 100644 "Week_02/589.n\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" create mode 100644 "Week_02/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206.cpp" diff --git "a/Week_02/144.\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" "b/Week_02/144.\344\272\214\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 00000000..65745bf5 --- /dev/null +++ "b/Week_02/144.\344\272\214\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,51 @@ +/* + * @lc app=leetcode.cn id=144 lang=cpp + * + * [144] 二叉树的前序遍历 + */ + +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector preorderTraversal(TreeNode* root) { + vector res; + // helper(root, res); + loop(root, res); + return res; + } + // 递归遍历 + void helper(TreeNode* root, vector &res) { + if(root == nullptr) return; + res.push_back(root->val); + helper(root->left, res); + helper(root->right, res); + } + + //循环遍历 + void loop(TreeNode* root, vector &res) { + stack s; + while(!s.empty() || root != nullptr) { + while(root != nullptr) { + res.push_back(root->val); + s.push(root); + root = root ->left; + } + TreeNode *n = s.top(); + s.pop(); + root = n->right; + } + } +}; +// @lc code=end + diff --git "a/Week_02/263.\344\270\221\346\225\260.cpp" "b/Week_02/263.\344\270\221\346\225\260.cpp" new file mode 100644 index 00000000..1a6ee315 --- /dev/null +++ "b/Week_02/263.\344\270\221\346\225\260.cpp" @@ -0,0 +1,19 @@ +/* + * @lc app=leetcode.cn id=263 lang=cpp + * + * [263] 丑数 + */ + +// @lc code=start +class Solution { +public: + bool isUgly(int num) { + if(num <= 0 ) return false; + while(num %2 == 0) num /= 2; + while(num %3 == 0) num /= 3; + while(num %5 == 0) num /=5; + return num == 1; + } +}; +// @lc code=end + diff --git "a/Week_02/264.\344\270\221\346\225\260-ii.cpp" "b/Week_02/264.\344\270\221\346\225\260-ii.cpp" new file mode 100644 index 00000000..e91d8b25 --- /dev/null +++ "b/Week_02/264.\344\270\221\346\225\260-ii.cpp" @@ -0,0 +1,44 @@ +/* + * @lc app=leetcode.cn id=264 lang=cpp + * + * [264] 丑数 II + */ + +// @lc code=start +class Solution { +public: + int nthUglyNumber(int n) { + if(n <= 0) return 0; + int *pUglyNumbers = new int[n]; + pUglyNumbers[0] =1; + int nextUglyIndex = 1; + + int *pMultiply2 = pUglyNumbers; + int *pMultiply3 = pUglyNumbers; + int *pMultiply5 = pUglyNumbers; + + while(nextUglyIndex < n) { + int min = Min(*pMultiply2 *2, *pMultiply3 *3, *pMultiply5 *5); + pUglyNumbers[nextUglyIndex] = min; + while(*pMultiply2 *2 <=pUglyNumbers[nextUglyIndex]) + pMultiply2++; + while(*pMultiply3 *3 <=pUglyNumbers[nextUglyIndex]) + pMultiply3++; + while(*pMultiply5 *5 <=pUglyNumbers[nextUglyIndex]) + pMultiply5++; + + nextUglyIndex++; + } + int ugly = pUglyNumbers[nextUglyIndex -1]; + delete []pUglyNumbers; + return ugly; + } + + int Min(int num1, int num2, int num3) { + int min = (num1 < num2) ? num1 : num2; + return (min < num3) ? min : num3; + } + +}; +// @lc code=end + diff --git "a/Week_02/347.\345\211\215-k-\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" "b/Week_02/347.\345\211\215-k-\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..fcfc29b4 --- /dev/null +++ "b/Week_02/347.\345\211\215-k-\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=347 lang=cpp + * + * [347] 前 K 个高频元素 + */ + +// @lc code=start +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + priority_queue, vector>,greater>> pq; + unordered_map maps; + for(auto num :nums) maps[num]++; + for(auto m :maps) { + pq.push({m.second, m.first}); + while(pq.size()>k) pq.pop(); + } + + vector res; + while(pq.size() > 0) { + res.push_back(pq.top().second); + pq.pop(); + } + return res; + } +}; +// @lc code=end + diff --git "a/Week_02/429.n\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" "b/Week_02/429.n\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 00000000..ab844d55 --- /dev/null +++ "b/Week_02/429.n\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,53 @@ +/* + * @lc app=leetcode.cn id=429 lang=cpp + * + * [429] N叉树的层序遍历 + */ + +// @lc code=start +/* +// Definition for a Node. +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; +*/ + +class Solution { +public: + vector> levelOrder(Node* root) { + vector> res; + if(root == nullptr) return res; + queue q; + q.push(root); + while(!q.empty()) { + int size = q.size(); + vector temp; + for(int i = 0; i children.size(); + for(int j =0; jchildren[j]); + } + temp.push_back(n->val); + } + res.push_back(temp); + } + return res; + } +}; +// @lc code=end + diff --git "a/Week_02/589.n\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" "b/Week_02/589.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 00000000..84668566 --- /dev/null +++ "b/Week_02/589.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,68 @@ +/* + * @lc app=leetcode.cn id=589 lang=cpp + * + * [589] N叉树的前序遍历 + */ + +// @lc code=start +/* +// Definition for a Node. +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; +*/ + +class Solution { +public: + vector preorder(Node* root) { + vector res; + if(root == nullptr) return res; + helper(root, res); + return res; + } + // 递归版本 + void helper(Node* root, vector &res){ + if(root == nullptr) return ; + res.push_back(root->val); + for(int i =0; i < root->children.size(); i++) { + helper(root->children[i], res); + } + } + // 循环版本 + void loop(Node* root, vector &res) { + stack s; + s.push(root); + while(!s.empty()) { + Node* temp = s.top(); + s.pop(); + if(temp != nullptr) { + res.push_back(temp->val); + } else { + continue; + } + if(!temp->children.empty()) { + int size = temp->children.size(); + for(int i =0; i< size; i++) { + s.push(temp->children[i]); + } + } + } + } +}; + + +// @lc code=end + diff --git "a/Week_02/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206.cpp" "b/Week_02/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206.cpp" new file mode 100644 index 00000000..c830c0fd --- /dev/null +++ "b/Week_02/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206.cpp" @@ -0,0 +1,50 @@ +/* + * @lc app=leetcode.cn id=94 lang=cpp + * + * [94] 二叉树的中序遍历 + */ + +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector inorderTraversal(TreeNode* root) { + vector res; + // helper(root, res); + loop(root, res); + return res; + } + // 递归遍历 + void helper(TreeNode *root, vector &res) { + if(root == nullptr) return; + helper(root->left, res); + res.push_back(root->val); + helper(root->right, res); + } + // 循环遍历 + void loop(TreeNode *root, vector &res) { + stack s; + while (!s.empty() || root != nullptr) { + while(root != nullptr) { + s.push(root); + root = root -> left; + } + TreeNode* n = s.top(); + s.pop(); + res.push_back(n->val); + root = n->right; + } + } +}; +// @lc code=end + From 0f271adeec35840cb7968586aeaddacca0d02f0f Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 11 Oct 2020 22:44:49 +0800 Subject: [PATCH 06/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AD=A6=E4=B9=A0?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_02/README.md | 138 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/Week_02/README.md b/Week_02/README.md index 50de3041..416b478e 100644 --- a/Week_02/README.md +++ b/Week_02/README.md @@ -1 +1,137 @@ -学习笔记 \ No newline at end of file +# 学习笔记 +## 哈希表、映射、集合 +### 哈希表、映射 +  哈希表也叫散列表,有的人也称为Hash表,根据关键码值从而直接访问数据的一种数据结构。加快访问数据的速度。 +#### 特点 +- 取值和查找值的时间复杂度为O(1),最坏情况下为O(n)*(ps:当出现冲突的时候,哈希表退化成一个链表)* +- key和value的映射通过散列函数(哈希函数、Hash函数)得到映射关系。存放数据的地方成为哈希表或者散列表 +- 哈希函数中会出现冲突现象既两个value的值对应到一个key上面。使用链表法,线性探测法解决冲突。 +#### 工程应用 +- (LRU Cache)缓存:使用哈希表存储缓存的数据,将查找的速度降为O(1) +- 键值对存储(Redis) +### 集合 +  集合,一组没有重复数据的集合。 +#### 特点 +- 没有重复的数据的集合 +- 底层使用HashMap实现,直接将数据最为key传入HashMap中 +### 相关例题 +- [有效的字母异位词](https://leetcode-cn.com/problems/group-anagrams/submissions/) +  使用HashMap存储每个字母出现的次数,相等就是异位词 +``` + bool isAnagram(string s, string t) { + if(s.length() != t.length()) { + return false; + } + // 使用数组最为Hash表,存储字母出现的个数 + vector vectors(26,0); + for(int i =0; i< s.length(); i++) { + // 第一单词出现则 +1; + vectors[s[i] - 'a']++; + // 第二单词出现则 -1; + vectors[t[i] - 'a']--; + } + // 判断Hash表的数据是不是都是0; 不是则不是异味字符 + for(int a : vectors) { + if(a != 0) { + return false; + } + } + return true; + } +``` +- [字母异位词分组](https://leetcode-cn.com/problems/valid-anagram/description/) +  遍历每一个单词并且排序,将排序后的字符作为key存储字符串 +``` +vector> groupAnagrams(vector& strs) { + vector> res; + map> map; + for(string s :strs) { + string t = s; + sort(t.begin(), t.end()); + map[t].push_back(s); + } + for(auto& a: map) { + res.push_back(a.second); + } + return res; + } +``` +- [两数之和](https://leetcode-cn.com/problems/two-sum/description/) +  将数组的值和下标保存在Hash表中,用target- 遍历数组中的值,若在Hash表里出现过则返回。 +``` +vector twoSum(vector& nums, int target) { + unordered_map map; + for (int i = 0; i < nums.size(); ++i) { + auto it = map.find(target - nums[i]); + if (it != map.end()) { + return {it->second, i}; + } + map[nums[i]] = i; + } + return {}; + } +``` +## 树、二叉树、二叉搜索树 +  优化算法的方式:1、空间换时间。2、升维。二叉树就是将一维的数据结构升为二维的数据结构,并作出一些规则,提升查找速度。 +### 基本数据结构 +``` +// 二叉树 +struct TreeNode { + int val; + TreeNode *left; + TreeNode *righe; +}; +// 树 +struct Tree { + int val; + Vector children; +} +``` +### 知识点 +- 前序遍历(根-左-右) +``` +// 二叉树遍历 +void inorderTraversal(TreeNode* root) { + if(root == null) return; + print("%d",root->val); + inorderTraversal(root->left); + inorderTraversal(root->right); +} +``` +- 中序遍历(左-根-右) +``` +// 二叉树遍历 +void inorderTraversal(TreeNode* root) { + if(root == null) return; + inorderTraversal(root->left); + print("%d",root->val); + inorderTraversal(root->right); +} +``` +- 后序遍历(左-右-根) +``` +// 二叉树遍历 +void inorderTraversal(TreeNode* root) { + if(root == null) return; + inorderTraversal(root->left); + inorderTraversal(root->right); + print("%d",root->val); +} +``` +### 二叉搜素树 +  二叉搜索树,也称二叉排序树、有序二叉树(Ordered Binary Tree)、排序二叉树(Sorted Binary Tree)。 +#### 特点 +- 左子树上所有结点的值均小于它的根结点的值; +- 右子树上所有结点的值均大于它的根结点的值; +- 以此类推:左、右子树也分别为二叉查找树。 (这就是 重复性! +- 中序遍历是有序的 + +## 堆Heap和二叉堆Binary Heap +  可以迅速找到一堆数中的最大值或者最小值的数据结构 +### 特点 +- 找到最大值O(1) +- 删除最大值O(logN) +- 插入元素O(logN)或O(1)*(ps:插入的值是最小的)* +### 相关例题 +- [滑动窗口最大值](https://leetcode-cn.com/problems/sliding-window-maximum/) +  暂时没有理解 From b70ed1b3b8c21dc32ff524a71543202311451fed Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 18 Oct 2020 22:45:06 +0800 Subject: [PATCH 07/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AF=BE=E5=90=8E?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\344\272\214\345\217\211\346\240\221.cpp" | 43 +++++++++++++++++++ ...4\345\205\261\347\245\226\345\205\210.cpp" | 29 +++++++++++++ ....\345\205\250\346\216\222\345\210\227.cpp" | 28 ++++++++++++ ...45\205\250\346\216\222\345\210\227-ii.cpp" | 35 +++++++++++++++ "Week_03/77.\347\273\204\345\220\210.cpp" | 37 ++++++++++++++++ 5 files changed, 172 insertions(+) create mode 100644 "Week_03/105.\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" create mode 100644 "Week_03/236.\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" create mode 100644 "Week_03/46.\345\205\250\346\216\222\345\210\227.cpp" create mode 100644 "Week_03/47.\345\205\250\346\216\222\345\210\227-ii.cpp" create mode 100644 "Week_03/77.\347\273\204\345\220\210.cpp" diff --git "a/Week_03/105.\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/Week_03/105.\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 00000000..b3ee69ee --- /dev/null +++ "b/Week_03/105.\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,43 @@ +/* + * @lc app=leetcode.cn id=105 lang=cpp + * + * [105] 从前序与中序遍历序列构造二叉树 + */ + +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { + unordered_map map; +public: + + + TreeNode* buildTree(vector& preorder, vector& inorder) { + int n = inorder.size(); + for(int i =0; i < n; i++) { + map[inorder[i]] = i; + } + return hepler(preorder, inorder, 0, n -1, 0 , n-1); + } + TreeNode* hepler(const vector& preorder, const vector& inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) { + if(preorder_left > preorder_right) return nullptr; + // 前序遍历的根节点 + int preorder_root = preorder_left; + // 中序遍历的根节点 + int inorder_root = map[preorder[preorder_root]]; + TreeNode* root = new TreeNode(preorder[preorder_root]); + int size_left_subtree = inorder_root - inorder_left; + root->left = hepler(preorder, inorder, preorder_left +1, preorder_left + size_left_subtree, inorder_left, inorder_root -1); + root ->right = hepler(preorder, inorder, preorder_left +size_left_subtree +1, preorder_right, inorder_root +1, inorder_right); + return root; + } +}; +// @lc code=end + diff --git "a/Week_03/236.\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/Week_03/236.\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 00000000..bc219848 --- /dev/null +++ "b/Week_03/236.\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,29 @@ +/* + * @lc app=leetcode.cn id=236 lang=cpp + * + * [236] 二叉树的最近公共祖先 + */ + +// @lc code=start +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + if(root == nullptr || root == p || root == q) return root; + TreeNode* left = lowestCommonAncestor(root->left, p, q); + TreeNode* right = lowestCommonAncestor(root->right, p, q); + if(left == nullptr) return right; + if(right == nullptr) return left; + return root; + } +}; +// @lc code=end + diff --git "a/Week_03/46.\345\205\250\346\216\222\345\210\227.cpp" "b/Week_03/46.\345\205\250\346\216\222\345\210\227.cpp" new file mode 100644 index 00000000..4b14a25f --- /dev/null +++ "b/Week_03/46.\345\205\250\346\216\222\345\210\227.cpp" @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=46 lang=cpp + * + * [46] 全排列 + */ + +// @lc code=start +class Solution { +public: + vector> permute(vector& nums) { + vector> res; + helper(res, nums, 0, nums.size()); + return res; + } + void helper(vector>& res, vector &nums, int level, int len) { + if(level == len) { + res.push_back(nums); + return; + } + for(int i = level; i < nums.size(); i++) { + swap(nums[i], nums[level]); + helper(res, nums, level +1, len); + swap(nums[i], nums[level]); + } + } +}; +// @lc code=end + diff --git "a/Week_03/47.\345\205\250\346\216\222\345\210\227-ii.cpp" "b/Week_03/47.\345\205\250\346\216\222\345\210\227-ii.cpp" new file mode 100644 index 00000000..168be187 --- /dev/null +++ "b/Week_03/47.\345\205\250\346\216\222\345\210\227-ii.cpp" @@ -0,0 +1,35 @@ +/* + * @lc app=leetcode.cn id=47 lang=cpp + * + * [47] 全排列 II + */ + +// @lc code=start +class Solution { +public: + vector> permuteUnique(vector& nums) { + vector via(nums.size()); + vector> res; + vector temp; + sort(nums.begin(), nums.end()); + helper(res, temp, via, nums, 0); + return res; + } + + void helper(vector>& res, vector& temp, vector& via, const vector& nums, int level){ + if(level == nums.size()) { + res.push_back(temp); + return; + } + for(int i =0; i < nums.size(); i++) { + if(via[i] || (i > 0 && nums[i] == nums[i - 1] && !via[i - 1])) continue; + temp.push_back(nums[i]); + via[i] = 1; + helper(res, temp, via, nums, level +1); + temp.pop_back(); + via[i] =0; + } + } +}; +// @lc code=end + diff --git "a/Week_03/77.\347\273\204\345\220\210.cpp" "b/Week_03/77.\347\273\204\345\220\210.cpp" new file mode 100644 index 00000000..e009f7aa --- /dev/null +++ "b/Week_03/77.\347\273\204\345\220\210.cpp" @@ -0,0 +1,37 @@ +/* + * @lc app=leetcode.cn id=77 lang=cpp + * + * [77] 组合 + */ + +// @lc code=start +class Solution { +private: + vector nums; + +public: + vector> combine(int n, int k) { + vector> res; + helper(res, 1, n, k); + return res; + } + void helper(vector>& res,int level, int n, int k) { + // 当获取的数据个数,加上剩下所罕有的数据个数小于要包含的数据个数则证明该解无效,直接返回 + if (nums.size() + (n - level + 1) < k) { + return; + } + // 找到可行解 + if(nums.size() == k) { + res.push_back(nums); + return; + } + //选择当前层的数据 + nums.push_back(level); + helper(res, level +1, n, k); + nums.pop_back(); + // 不选当前层的数据 + helper(res, level +1, n, k); + } +}; +// @lc code=end + From 490ea0a05c20c9fb41c28bbff00dc394c5a2ec1e Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 18 Oct 2020 23:25:08 +0800 Subject: [PATCH 08/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AD=A6=E4=B9=A0?= =?UTF-8?q?=E6=80=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_03/README.md | 115 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/Week_03/README.md b/Week_03/README.md index 50de3041..68f1837d 100644 --- a/Week_03/README.md +++ b/Week_03/README.md @@ -1 +1,114 @@ -学习笔记 \ No newline at end of file +# 学习笔记 +## 范型递归、树递归 +  说起递归永远是程序员学习进程中的一座高山,想要越过需要长时间的学习和练习。递归需要将问题翻译成以一个递推公式,只有深入到最后一个才能推导出来这个问题的解。 +### 递归通用模版 +``` +void recursion(int level, int param) { + // recursion terminator 递归终止条件 + if (level > MAX_LEVEL) { + // process result 最终结果 + return ; + } + + // process current logic 当前层逻辑 + process(level, param); + + // drill down 往下递归 + recursion(level + 1, param); + + // reverse the current level status if needed 清理当前层数据 +} +``` +### 递归经典例题 +``` +// 斐波那契数列 +int Fibon(int n) { + // recursion termiantor + if(n ==0 || n == 1) return 1; + return Fibon(n -1) + Fibon(n -2); +} + +// 爬楼梯问题 可以一层上一节台阶,也可上两层台阶,问上n层台阶有多少种方法。 +// 解法:上3层台阶可以 由上2层台阶和1层台阶的解法相加得到.上N层台阶就由 上N-1层 + N-2层台阶相加得到。 +int climbStairs(int n) { + if(n == 1) return 1; + if(n == 2) return 2; + return climbStairs(n -1) + climbStairs(n -2); +} +``` + +### 递归思考方法 +- 将一个问题分解成重复的一个动作。(ps:这个过程有点困难,需要大量的练习) + - 取某一个元素,获取不取某一个元素。(ps:[子集](https://leetcode-cn.com/problems/subsets/) 、[括号生成问题](https://leetcode-cn.com/problems/generate-parentheses/)) + - 交换数组中两个元素的位置(ps:[全排列](https://leetcode-cn.com/problems/permutations/)) +- 只考虑当前层逻辑,不要想下探后要处理的逻辑 +- 要能找出递推公式的可以找出递推公式 +- **多练习** + +### 树的递归遍历 +- 前序遍历(根-左-右) +``` +void preorder(TreeNode* root , vector &res) { + if(root == null) return; + res.push_back(root); + preorder(root->left, res); + preorder(root->right, res); +} +``` +- 中序遍历(左-根-右) +``` +void inorder(TreeNode* root, vector &res) { + if(root == null) return; + inorder(root->left, res); + res.push_back(root); + inorder(root->right, res); +} +``` + +- 后序遍历(左-右-根) +``` +void postorder(TreeNode* root, vector &res) { + if(root == null) return; + postorder(root->left, res); + postorder(root->right, res); + res.push_back(root); +} +``` + + + +## 分治、回溯 +  分治就是将一个大问题,分解成为一个一个的小问题,最后再将他们的解组合起来,形成问题的最终解。 +  回溯采用试错的思想,它尝试分步的去解决一个问题。在分步解决问题的过程中,当它通过尝试发现现有的分步答案不能得到有效的正确的解答的时候,它将 取消上一步甚至是上几步的计算,再通过其它的可能的分步解答再次尝试寻找问 题的答案。 +### 分治模版 +``` +int divide_conquer(Problem *problem, int params) { + // recursion terminator 终止条件 + if (problem == nullptr) { + process_result + return return_result; + } + + // process current problem 分解成为小问题 + subproblems = split_problem(problem, data) + subresult1 = divide_conquer(subproblem[0], p1) + subresult2 = divide_conquer(subproblem[1], p1) + subresult3 = divide_conquer(subproblem[2], p1) + ... + + // merge 将问题合并 + result = process_result(subresult1, subresult2, subresult3) + // revert the current level status 清理当前层状态。 + + return 0; +} +``` + + +### 思考方式 +- 将问题分解成可以解的小问题(ps: 最难的一步) +- 合并小问题的解形成最终问题的解(ps: 也比较难) +- **多多练习** + +# 总结 +  递归,分治和回溯,现在有一些懵懂的入门,还需要大量的练习去巩固加强,后面会针对这一类的问题去练习,争取可以独立解出来问题。 From 08ca60d920d53ad212ee6283849ea54ed8c63aa4 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 25 Oct 2020 21:07:39 +0800 Subject: [PATCH 09/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\275\263\346\227\266\346\234\272-ii.cpp" | 21 +++++++++ ...5\350\257\215\346\216\245\351\276\231.cpp" | 37 +++++++++++++++ ...4\346\234\200\345\260\217\345\200\274.cpp" | 24 ++++++++++ ...3\345\261\277\346\225\260\351\207\217.cpp" | 37 +++++++++++++++ ...2\345\272\217\346\225\260\347\273\204.cpp" | 25 ++++++++++ ...6\345\217\221\351\245\274\345\271\262.cpp" | 26 ++++++++++ ...4\346\260\264\346\211\276\351\233\266.cpp" | 36 ++++++++++++++ ...0\346\234\272\345\231\250\344\272\272.cpp" | 47 +++++++++++++++++++ 8 files changed, 253 insertions(+) create mode 100644 "Week_04/122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-ii.cpp" create mode 100644 "Week_04/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" create mode 100644 "Week_04/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.cpp" create mode 100644 "Week_04/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" create mode 100644 "Week_04/33.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.cpp" create mode 100644 "Week_04/455.\345\210\206\345\217\221\351\245\274\345\271\262.cpp" create mode 100644 "Week_04/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.cpp" create mode 100644 "Week_04/874.\346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272.cpp" diff --git "a/Week_04/122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-ii.cpp" "b/Week_04/122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-ii.cpp" new file mode 100644 index 00000000..6d3e98a7 --- /dev/null +++ "b/Week_04/122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-ii.cpp" @@ -0,0 +1,21 @@ +/* + * @lc app=leetcode.cn id=122 lang=cpp + * + * [122] 买卖股票的最佳时机 II + */ + +// @lc code=start +class Solution { +public: + int maxProfit(vector& prices) { + int maxPrice =0; + for(int i = 0; i < prices.size()-1; i++) { + if(prices[i] < prices[i +1]) { + maxPrice += prices[i +1] - prices[i]; + } + } + return maxPrice; + } +}; +// @lc code=end + diff --git "a/Week_04/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" "b/Week_04/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" new file mode 100644 index 00000000..53bdf8cb --- /dev/null +++ "b/Week_04/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" @@ -0,0 +1,37 @@ +/* + * @lc app=leetcode.cn id=127 lang=cpp + * + * [127] 单词接龙 + */ + +// @lc code=start +class Solution { +public: + int ladderLength(string beginWord, string endWord, vector& wordList) { + unordered_set set(wordList.begin(), wordList.end()); + queue> queue; + queue.push({beginWord, 1}); + while(!queue.empty()) { + if(queue.front().first == endWord) { + return queue.front().second; + } + string temp = queue.front().first; + int step = queue.front().second; + queue.pop(); + for(int i = 0; i < temp.size(); i++) { + char ch = temp[i]; + for(char c = 'a'; c <= 'z'; c++) { + temp[i] = c; + if(set.find(temp) != set.end()) { + queue.push({temp, step +1}); + set.erase(temp); + } + temp[i] = ch; + } + } + } + return 0; + } +}; +// @lc code=end + diff --git "a/Week_04/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.cpp" "b/Week_04/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.cpp" new file mode 100644 index 00000000..da390641 --- /dev/null +++ "b/Week_04/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.cpp" @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode.cn id=153 lang=cpp + * + * [153] 寻找旋转排序数组中的最小值 + */ + +// @lc code=start +class Solution { +public: + int findMin(vector& nums) { + int left = 0; + int right = nums.size() -1; + while(right > left){ + int mid = left +( right - left )/2; + if(nums[mid] > nums[right]) + left = mid +1; + else + right = mid; + } + return nums[left]; + } +}; +// @lc code=end + diff --git "a/Week_04/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" "b/Week_04/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" new file mode 100644 index 00000000..d8031217 --- /dev/null +++ "b/Week_04/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" @@ -0,0 +1,37 @@ +/* + * @lc app=leetcode.cn id=200 lang=cpp + * + * [200] 岛屿数量 + */ + +// @lc code=start +class Solution { +public: + void dfs(vector>& grid, int row, int col) { + int nr = grid.size(); + int nc = grid[0].size(); + grid[row][col] = 0; + if(row -1 >= 0 && grid[row -1][col] == '1') dfs(grid, row -1, col); + if(row +1 < nr && grid[row +1][col] == '1') dfs(grid, row +1, col); + if(col -1 >= 0 && grid[row][col -1] == '1') dfs(grid, row, col -1); + if(col +1 < nc && grid[row][col +1] == '1') dfs(grid, row, col +1); + } + int numIslands(vector>& grid) { + if(grid.size() <= 0) return 0; + if(grid[0].size() <= 0) return 0; + int nr = grid.size(); + int nc = grid[0].size(); + int res = 0; + for(int i =0; i & nums, int target) { + int left =0, right = nums.size() -1; + while(left < right) { + int mid = left + (right -left) /2; + if(nums[mid] == target) return mid; + if ((nums[0] > target) ^ (nums[0] > nums[mid]) ^ (target > nums[mid])) + left = mid + 1; + else + right = mid; + } + if(nums[left] == target) return left; + return -1; + } +}; +// @lc code=end + diff --git "a/Week_04/455.\345\210\206\345\217\221\351\245\274\345\271\262.cpp" "b/Week_04/455.\345\210\206\345\217\221\351\245\274\345\271\262.cpp" new file mode 100644 index 00000000..cf035aa9 --- /dev/null +++ "b/Week_04/455.\345\210\206\345\217\221\351\245\274\345\271\262.cpp" @@ -0,0 +1,26 @@ +/* + * @lc app=leetcode.cn id=455 lang=cpp + * + * [455] 分发饼干 + */ + +// @lc code=start +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + sort(g.begin(), g.end()); + sort(s.begin(), s.end()); + if(s.size() < 1 || g.size() <1) return 0; + int sIndex = s.size() -1; + int res = 0; + for(int i = g.size() -1; i >=0; i--) { + if(g[i] <= s[sIndex]) { + sIndex--; + res++; + } + } + return res; + } +}; +// @lc code=end + diff --git "a/Week_04/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.cpp" "b/Week_04/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.cpp" new file mode 100644 index 00000000..2f761643 --- /dev/null +++ "b/Week_04/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.cpp" @@ -0,0 +1,36 @@ +/* + * @lc app=leetcode.cn id=860 lang=cpp + * + * [860] 柠檬水找零 + */ + +// @lc code=start +class Solution { +public: + bool lemonadeChange(vector& bills) { + int fiveBills =0, tenBills =0; + for(int bill : bills) { + if(bill == 5) { + fiveBills++; + } else if (bill == 10) { + if(fiveBills < 1) { + return false; + } + fiveBills--; + tenBills++; + } else { + if(tenBills > 0 && fiveBills > 0) { + tenBills--; + fiveBills--; + } else if(fiveBills > 3) { + fiveBills -= 3; + } else { + return false; + } + } + } + return true; + } +}; +// @lc code=end + diff --git "a/Week_04/874.\346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272.cpp" "b/Week_04/874.\346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272.cpp" new file mode 100644 index 00000000..4f8d10c0 --- /dev/null +++ "b/Week_04/874.\346\250\241\346\213\237\350\241\214\350\265\260\346\234\272\345\231\250\344\272\272.cpp" @@ -0,0 +1,47 @@ +/* + * @lc app=leetcode.cn id=874 lang=cpp + * + * [874] 模拟行走机器人 + */ + +// @lc code=start +class Solution { +public: + int robotSim(vector& commands, vector>& obstacles) { + int direx[] = {0,1,0,-1}; + int direy[] = {1,0,-1,0}; + int curx=0,cury=0; + int curdire = 0; + int comLen = commands.size(); + int ans = 0; + set> obstacleSet; + for(int i=0;i Date: Sun, 25 Oct 2020 22:34:37 +0800 Subject: [PATCH 10/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AD=A6=E4=B9=A0?= =?UTF-8?q?=E6=80=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/README.md | 181 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 180 insertions(+), 1 deletion(-) diff --git a/Week_04/README.md b/Week_04/README.md index 50de3041..c61f812f 100644 --- a/Week_04/README.md +++ b/Week_04/README.md @@ -1 +1,180 @@ -学习笔记 \ No newline at end of file +# 学习笔记 +## 深度优先搜索、广度优先搜索 +   遍历是每一个数据结构的基本方法,一维数据结构的遍历非常简单使用循环就可以完成,二维数据的遍历就需要认真的思考一下了。 +### 分类 +- 深度优先遍历(使用递归实现,一直深入找到遍历不下去的,在回溯,遍历完所有的节点) +``` +// 保存已经遍历过的数组 +map visited; +void dfs(Node* root) { + // terminator + if (!root) return ; + + if (visited.count(root->val)) { + // already visited + return ; + } + + visited[root->val] = 1; + + // process current node here. + // ... + for (int i = 0; i < root->children.size(); ++i) { + dfs(root->children[i]); + } + return ; +} +``` + +- 广度优先遍历(使用队列进行遍历, 将节点的儿子都放入队列中,从队列中取元素进行遍历) +``` +void bfs(Node* root) { + map visited; + if(!root) return ; + + queue queueNode; + queueNode.push(root); + + while (!queueNode.empty()) { + Node* node = queueNode.top(); + queueNode.pop(); + if (visited.count(node->val)) continue; + visited[node->val] = 1; + + for (int i = 0; i < node->children.size(); ++i) { + queueNode.push(node->children[i]); + } + } + + return ; +} +``` +### 例题 +- [二叉树的层序遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/) +  套BSF的模版,将结果保存到一个集合当中 +``` +vector> levelOrder(TreeNode* root) { + vector> res; + if(!root) return res; + deque queue; + queue.push_back(root); + while(!queue.empty()) { + int size = queue.size(); + vector temp; + for(int i =0; i < size; i++) { + TreeNode* node = queue.front(); + queue.pop_front(); + temp.push_back(node->val); + if(node->left) { + queue.push_back(node->left); + } + if(node->right) { + queue.push_back(node->right); + } + } + res.push_back(temp); + } + return res; + } +``` + +## 贪心算法 +  贪心算法是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是全局最好或最优的算法。 + +### 例题 +- [买卖股票的最佳时机 II](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/) +   如果某一天的金额比它后一天的小,则在当天买入,并且在明天卖出。遍历所有的,得到结果就是最终结果。 +``` +int maxProfit(vector& prices) { + int maxPrice =0; + for(int i = 0; i < prices.size()-1; i++) { + if(prices[i] < prices[i +1]) { + maxPrice += prices[i +1] - prices[i]; + } + } + return maxPrice; +} +``` +- [柠檬水找零](https://leetcode-cn.com/problems/lemonade-change/description/) +   当收入为10元,查看是否含有5元的零钱, 若收入为20元后,查看是否含有10元和5元的零钱 或者 3个5元零钱。 +``` +bool lemonadeChange(vector& bills) { + int fiveBills =0, tenBills =0; + for(int i =0; i < bills.size(); i++) { + if(bills[i] == 5) { + fiveBills++; + } + if(bills[i] ==10) { + if(fiveBills < 1) { + return false; + } + fiveBills--; + tenBills++; + } + if(bills[i] == 20) { + if(tenBills >0 && fiveBills >0){ + fiveBills--; + tenBills--; + } else if (fiveBills > 3){ + fiveBills -= 3; + }else{ + return false; + } + } + } + return true; +} +``` + + +## 二分查找 +   二分查找是在**有序**集合中,以时间复杂度为O(log n) 更快的查找出来元素。 + +### 使用条件 +- 目标函数的单调性 +- 存在上下界 +- 可以使用索引访问 + +### 代码模版 +``` +int binarySearch(const vector& nums, int target) { + int left = 0, right = (int)nums.size()-1; + + while (left <= right) { + int mid = left + (right - left)/ 2; + if (nums[mid] == target) return mid; + else if (nums[mid] < target) left = mid + 1; + else right = mid - 1; + } + + return -1; +} +``` + + + + +### 课后问题 +  使用二分查找,寻找一个半有序数组 [4, 5, 6, 7, 0, 1, 2] 中间无序的地方。 (ps: 数组升序排序) +``` +int solution(vector& nums) { + int left =0, right =nums.size() -1; + while(left < right) { + int mid = left + (right -left) /2; + if(nums[mid +1] < nums[mid]) { + left = mid +1; + break; + } + // 判断整个数组是否有序 + if(nums[mid] > nums[left] && nums[mid] < nums[right]) { + break; + // 左边是否有序, 有序在右边查找,反之则在左边查找 + } else if(nums[mid] > nums[left]) { + left = mid; + } else { + right = mid; + } + } + return left; +} +``` \ No newline at end of file From e96fa2d5a0283e576ec71f587f0609285e0caa86 Mon Sep 17 00:00:00 2001 From: MingLL Date: Mon, 9 Nov 2020 00:02:46 +0800 Subject: [PATCH 11/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E5=85=AD?= =?UTF-8?q?=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_06/README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/Week_06/README.md b/Week_06/README.md index 50de3041..51372956 100644 --- a/Week_06/README.md +++ b/Week_06/README.md @@ -1 +1,81 @@ -学习笔记 \ No newline at end of file +# 学习笔记 +## 动态规划 +  动态规划比较适合用来解决最优问题,比如求最大值,最小值等等,它可以非常显著的降低时间复杂度,提高代码的执行效率。但是它也是出了名的难学,需要大量的题目练习才能学会。 +### 动态规划与递归,分治的区别和共性 +- 共性:都是要找到重复的子问题 +- 区别: 动态规划:最优子结构、中途可以淘汰次优解。 +### 动态规划的思路 +- 找重复性 +- 定义状态数组 +- 写出DP方程 +### 动态规划例题 +- Fibonacci数列 +   要想知道Fibonacci数列中的第n个数字是多少,就需要知道第n-1的数和第n-2的数。就可以推出dp方程既F(n) = F(n-1) + F(n-2),其中F(1) = 1;F(0) =0;可以根据递归方程写出如下代码 + + ``` + // 递归方式(自顶向下) + int Fibonacci(int n) { + if(n <= 1) return n; + return Fibonacci(n-1) + Fibonacci(n-2) + } + // 动态规划(自底向上) + int Fibonacci(int n) { + int pre = 0; + int next = 1; + for(int i =1; i < n; i++) { + next = pre + next; + pre = next; + } + return next; + } + ``` +- 不同路径 +   要想知道从一个格子走到右下角的格子一共有多少种不同的走法,因为机器人只可以向右走或者向下走两种选择,所以可以推出dp方程既F(i,j) = F(i+1, j) + F(i, j +1);可以根据递归方程写出如下代码 + ``` + // 递归方式(自顶向下) + int uniquePaths(int m, int n) { + return hepler(1,1,m,n); + } + int hepler(int i,int j, int m, int n) { + if(i > m || j > n) return 0; + else if(i == m && j == n) return 1; + else return hepler(i +1, j ,m,n) + hepler(i, j+1,m,n); + } + // 动态规划(自底向上) + int uniquePaths(int m, int n) { + int dp[m][n]; + for(int i =0; i < m; i++) dp[i][0] =1; + for(int i =0; i < n; i++) dp[0][i] =1; + for(int i =1; i < m; i++) { + for(int j =1; j < n; j++) { + dp[i][j] = dp[i -1][j] + dp[i][j -1]; + } + } + return dp[m -1][n -1]; + } + + ``` +- 最长公共子序列 +   在考虑这道题的时候,需要将两个字符串,分别排序在x轴和y轴上枸酱二维数组,找到重复子问题就是,最长子串就为若当前比对的字符相等,则为两个字符串个去掉一个字符的字串 加一,若不同,则为字符串A去掉1个字符或字符串B去掉一个字符,最大的字串。可写代码如下 + ``` + int longestCommonSubsequence(string text1, string text2) { + int m = text1.size(); + int n = text2.size(); + if(m < 1 || n < 1) return 0; + int dp[m +1][n +1]; + memset(dp, 0, sizeof(dp)); + for(int i =1; i <= m; i++) { + for(int j =1; j <= n; j++) { + if(text1[i -1] == text2[j -1]) { + dp[i][j] = dp[i -1][j -1] +1; + } else { + dp[i][j] = max(dp[i][j -1], dp[i -1][j]); + } + } + } + return dp[m][n]; + } + ``` + + + From 9fecd983e3e8f7581e438c9e6850bdb40505cc38 Mon Sep 17 00:00:00 2001 From: MingLL Date: Mon, 9 Nov 2020 00:02:59 +0800 Subject: [PATCH 12/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E5=85=AD?= =?UTF-8?q?=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\345\255\220\345\272\217\345\210\227.cpp" | 29 +++++++++++++++ ...7\350\267\257\345\276\204\345\222\214.cpp" | 25 +++++++++++++ ...3\345\256\266\345\212\253\350\210\215.cpp" | 24 +++++++++++++ ...6\351\222\261\345\205\221\346\215\242.cpp" | 25 +++++++++++++ ...5\345\220\214\350\267\257\345\276\204.cpp" | 23 ++++++++++++ ...45\220\214\350\267\257\345\276\204-ii.cpp" | 36 +++++++++++++++++++ ....\347\210\254\346\245\274\346\242\257.cpp" | 22 ++++++++++++ 7 files changed, 184 insertions(+) create mode 100644 "Week_06/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.cpp" create mode 100644 "Week_06/120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.cpp" create mode 100644 "Week_06/198.\346\211\223\345\256\266\345\212\253\350\210\215.cpp" create mode 100644 "Week_06/322.\351\233\266\351\222\261\345\205\221\346\215\242.cpp" create mode 100644 "Week_06/62.\344\270\215\345\220\214\350\267\257\345\276\204.cpp" create mode 100644 "Week_06/63.\344\270\215\345\220\214\350\267\257\345\276\204-ii.cpp" create mode 100644 "Week_06/70.\347\210\254\346\245\274\346\242\257.cpp" diff --git "a/Week_06/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.cpp" "b/Week_06/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.cpp" new file mode 100644 index 00000000..8261123e --- /dev/null +++ "b/Week_06/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.cpp" @@ -0,0 +1,29 @@ +/* + * @lc app=leetcode.cn id=1143 lang=cpp + * + * [1143] 最长公共子序列 + */ + +// @lc code=start +class Solution { +public: + int longestCommonSubsequence(string text1, string text2) { + int m = text1.size(); + int n = text2.size(); + if(m < 1 || n < 1) return 0; + int dp[m +1][n +1]; + memset(dp, 0, sizeof(dp)); + for(int i =1; i <= m; i++) { + for(int j =1; j <= n; j++) { + if(text1[i -1] == text2[j -1]) { + dp[i][j] = dp[i -1][j -1] +1; + } else { + dp[i][j] = max(dp[i][j -1], dp[i -1][j]); + } + } + } + return dp[m][n]; + } +}; +// @lc code=end + diff --git "a/Week_06/120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.cpp" "b/Week_06/120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.cpp" new file mode 100644 index 00000000..49e612e4 --- /dev/null +++ "b/Week_06/120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.cpp" @@ -0,0 +1,25 @@ +/* + * @lc app=leetcode.cn id=120 lang=cpp + * + * [120] 三角形最小路径和 + */ + +// @lc code=start +class Solution { +public: + int minimumTotal(vector>& triangle) { + int n = triangle.size(); + vector> f(n, vector(n)); + f[0][0] = triangle[0][0]; + for (int i = 1; i < n; ++i) { + f[i][0] = f[i - 1][0] + triangle[i][0]; + for (int j = 1; j < i; ++j) { + f[i][j] = min(f[i - 1][j - 1], f[i - 1][j]) + triangle[i][j]; + } + f[i][i] = f[i - 1][i - 1] + triangle[i][i]; + } + return *min_element(f[n - 1].begin(), f[n - 1].end()); + } +}; +// @lc code=end + diff --git "a/Week_06/198.\346\211\223\345\256\266\345\212\253\350\210\215.cpp" "b/Week_06/198.\346\211\223\345\256\266\345\212\253\350\210\215.cpp" new file mode 100644 index 00000000..313c032c --- /dev/null +++ "b/Week_06/198.\346\211\223\345\256\266\345\212\253\350\210\215.cpp" @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode.cn id=198 lang=cpp + * + * [198] 打家劫舍 + */ + +// @lc code=start +class Solution { +public: + int rob(vector& nums) { + if (nums.empty()) return 0; + int size = nums.size(); + if (size == 1) return nums[0]; + vector dp = vector(size, 0); + dp[0] = nums[0]; + dp[1] = max(nums[0], nums[1]); + for (int i = 2; i < size; i++) { + dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[size - 1]; + } +}; +// @lc code=end + diff --git "a/Week_06/322.\351\233\266\351\222\261\345\205\221\346\215\242.cpp" "b/Week_06/322.\351\233\266\351\222\261\345\205\221\346\215\242.cpp" new file mode 100644 index 00000000..1af76ccd --- /dev/null +++ "b/Week_06/322.\351\233\266\351\222\261\345\205\221\346\215\242.cpp" @@ -0,0 +1,25 @@ +/* + * @lc app=leetcode.cn id=322 lang=cpp + * + * [322] 零钱兑换 + */ + +// @lc code=start +class Solution { +public: + int coinChange(vector& coins, int amount) { + int MAX = amount +1; + vector dp(MAX, MAX); + dp[0] =0; + for(int i =1; i < MAX; i++) { + for(int j =0; j amount? -1 : dp[amount]; + } +}; +// @lc code=end + diff --git "a/Week_06/62.\344\270\215\345\220\214\350\267\257\345\276\204.cpp" "b/Week_06/62.\344\270\215\345\220\214\350\267\257\345\276\204.cpp" new file mode 100644 index 00000000..2acba9c3 --- /dev/null +++ "b/Week_06/62.\344\270\215\345\220\214\350\267\257\345\276\204.cpp" @@ -0,0 +1,23 @@ +/* + * @lc app=leetcode.cn id=62 lang=cpp + * + * [62] 不同路径 + */ + +// @lc code=start +class Solution { +public: + int uniquePaths(int m, int n) { + int dp[m][n]; + for(int i =0; i < m; i++) dp[i][0] =1; + for(int i =0; i < n; i++) dp[0][i] =1; + for(int i =1; i < m; i++) { + for(int j =1; j < n; j++) { + dp[i][j] = dp[i -1][j] + dp[i][j -1]; + } + } + return dp[m -1][n -1]; + } +}; +// @lc code=end + diff --git "a/Week_06/63.\344\270\215\345\220\214\350\267\257\345\276\204-ii.cpp" "b/Week_06/63.\344\270\215\345\220\214\350\267\257\345\276\204-ii.cpp" new file mode 100644 index 00000000..ac6aea2c --- /dev/null +++ "b/Week_06/63.\344\270\215\345\220\214\350\267\257\345\276\204-ii.cpp" @@ -0,0 +1,36 @@ +/* + * @lc app=leetcode.cn id=63 lang=cpp + * + * [63] 不同路径 II + */ + +// @lc code=start +class Solution { +public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + int m = obstacleGrid.size(); + if(m <= 0) return 0; + int n = obstacleGrid[0].size(); + vector> dp(m, vector(n, 0)); + for(int i =0; i < m; i++) { + if(obstacleGrid[i][0] == 1) break; + dp[i][0] = 1; + } + for(int i = 0; i < n; i++) { + if(obstacleGrid[0][i] == 1) break; + dp[0][i] = 1; + } + for(int i =1; i < m; i++) { + for(int j =1; j < n; j++) { + if(obstacleGrid[i][j] == 1) { + dp[i][j] = 0; + }else { + dp[i][j] = dp[i -1][j] +dp[i][j -1]; + } + } + } + return dp[m -1][n-1]; + } +}; +// @lc code=end + diff --git "a/Week_06/70.\347\210\254\346\245\274\346\242\257.cpp" "b/Week_06/70.\347\210\254\346\245\274\346\242\257.cpp" new file mode 100644 index 00000000..066121f6 --- /dev/null +++ "b/Week_06/70.\347\210\254\346\245\274\346\242\257.cpp" @@ -0,0 +1,22 @@ +/* + * @lc app=leetcode.cn id=70 lang=cpp + * + * [70] 爬楼梯 + */ + +// @lc code=start +class Solution { +public: + int climbStairs(int n) { + if(n == 1) return 1; + int *res = new int[n]; + res[0] = 1; + res[1] = 2; + for(int i = 2; i < n; i++) { + res[i] = res[i -1] + res[i -2]; + } + return res[n -1]; + } +}; +// @lc code=end + From 94f344fb6b2a5159b041692b43ac2817f6528b62 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 15 Nov 2020 23:33:01 +0800 Subject: [PATCH 13/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\350\257\215\346\216\245\351\276\231.cpp" | 38 +++++++++++ ...3\345\261\277\346\225\260\351\207\217.cpp" | 36 ++++++++++ ...-\345\211\215\347\274\200\346\240\221.cpp" | 60 +++++++++++++++++ ...4\345\217\267\347\224\237\346\210\220.cpp" | 30 +++++++++ ....\346\234\213\345\217\213\345\234\210.cpp" | 67 +++++++++++++++++++ Week_07/README.md | 2 +- 6 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 "Week_07/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" create mode 100644 "Week_07/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" create mode 100644 "Week_07/208.\345\256\236\347\216\260-trie-\345\211\215\347\274\200\346\240\221.cpp" create mode 100644 "Week_07/22.\346\213\254\345\217\267\347\224\237\346\210\220.cpp" create mode 100644 "Week_07/547.\346\234\213\345\217\213\345\234\210.cpp" diff --git "a/Week_07/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" "b/Week_07/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" new file mode 100644 index 00000000..c1dc5021 --- /dev/null +++ "b/Week_07/127.\345\215\225\350\257\215\346\216\245\351\276\231.cpp" @@ -0,0 +1,38 @@ +/* + * @lc app=leetcode.cn id=127 lang=cpp + * + * [127] 单词接龙 + */ + +// @lc code=start +class Solution { +public: + int ladderLength(string beginWord, string endWord, vector& wordList) { + unordered_set s(wordList.begin(), wordList.end()); + queue> q; + q.push({beginWord, 1}); + while( !q.empty()) { + if(q.front().first == endWord) { + return q.front().second; + } + string tmp = q.front().first; + int step = q.front().second; + q.pop(); + for(int i = 0; i< tmp.size(); i++) { + char ch = tmp[i]; + for(char c ='a'; c <= 'z'; c++) { + if(ch == c) continue; + tmp[i] = c; + if(s.find(tmp) != s.end()) { + q.push({tmp, step +1}); + s.erase(tmp); + } + tmp[i] = ch; + } + } + } + return 0; + } +}; +// @lc code=end + diff --git "a/Week_07/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" "b/Week_07/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" new file mode 100644 index 00000000..8c227dab --- /dev/null +++ "b/Week_07/200.\345\262\233\345\261\277\346\225\260\351\207\217.cpp" @@ -0,0 +1,36 @@ +/* + * @lc app=leetcode.cn id=200 lang=cpp + * + * [200] 岛屿数量 + */ + +// @lc code=start +class Solution { +public: + void dfs(vector>& grid, int row, int col) { + int nr = grid.size(); + int nc = grid[0].size(); + grid[row][col] = 0; + if(row -1>=0 && grid[row -1][col] == '1') dfs(grid, row -1, col); + if(row +1=0 && grid[row][col -1] == '1') dfs(grid, row, col -1); + if(col +1>& grid) { + int nr = grid.size(); + int nc = grid[0].size(); + if(nr ==0 || nc == 0) return 0; + int res =0; + for(int i =0; i< nr; i++) { + for(int j = 0; j next[c - 'a'] == nullptr) { + node->next[c - 'a'] = new Trie(); + } + node = node->next[c - 'a']; + } + node->is_end = true; + } + + /** Returns if the word is in the trie. */ + bool search(string word) { + Trie *node = this; + for(char c : word) { + node = node->next[c - 'a']; + if(node == nullptr) return false; + } + return node->is_end; + } + + /** Returns if there is any word in the trie that starts with the given prefix. */ + bool startsWith(string prefix) { + Trie *node = this; + for(char c : prefix) { + node = node->next[c - 'a']; + if(node == nullptr) return false; + } + return true; + } +}; + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ +// @lc code=end + diff --git "a/Week_07/22.\346\213\254\345\217\267\347\224\237\346\210\220.cpp" "b/Week_07/22.\346\213\254\345\217\267\347\224\237\346\210\220.cpp" new file mode 100644 index 00000000..7516d8c1 --- /dev/null +++ "b/Week_07/22.\346\213\254\345\217\267\347\224\237\346\210\220.cpp" @@ -0,0 +1,30 @@ +/* + * @lc app=leetcode.cn id=22 lang=cpp + * + * [22] 括号生成 + */ + +// @lc code=start +class Solution { +public: + vector generateParenthesis(int n) { + vector res; + helper(res, 0,0,n,""); + return res; + } + + void helper(vector& res, int left, int right, int n, string s) { + if(left == n && right ==n) { + res.push_back(s); + return; + } + if(left < n) { + helper(res, left +1, right, n, s+'('); + } + if(right < left) { + helper(res, left, right +1, n, s+')'); + } + } +}; +// @lc code=end + diff --git "a/Week_07/547.\346\234\213\345\217\213\345\234\210.cpp" "b/Week_07/547.\346\234\213\345\217\213\345\234\210.cpp" new file mode 100644 index 00000000..ada2c2c8 --- /dev/null +++ "b/Week_07/547.\346\234\213\345\217\213\345\234\210.cpp" @@ -0,0 +1,67 @@ +/* + * @lc app=leetcode.cn id=547 lang=cpp + * + * [547] 朋友圈 + */ + +// @lc code=start +class UF { +private: + int count; + int* parent; + int* size; + + int find(int n) { + while(parent[n] != n) { + parent[n] = parent[parent[n]]; + n = parent[n]; + } + return n; + } +public: + UF(int n) { + count = n; + this->parent = new int[n]; + this->size = new int[n]; + for(int i = 0; i size[rootQ]) { + parent[rootQ] = parent[rootP]; + size[rootP] += size[rootQ]; + } else { + parent[rootP] = parent[rootQ]; + size[rootQ] += size[rootP]; + } + count--; + } + int getCount() { + return count; + } + + +}; +class Solution { +public: + int findCircleNum(vector>& M) { + int size = M.size(); + UF *uf = new UF(size); + for(int i =0; i Union(i, j); + } + } + } + return uf->getCount(); + } +}; +// @lc code=end + diff --git a/Week_07/README.md b/Week_07/README.md index 50de3041..107ea7d6 100644 --- a/Week_07/README.md +++ b/Week_07/README.md @@ -1 +1 @@ -学习笔记 \ No newline at end of file +# 学习笔记 From 030b85d5087af74266c89aa1dc856a0b8e73e5d5 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 15 Nov 2020 23:46:05 +0800 Subject: [PATCH 14/23] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=AD=A6=E4=B9=A0?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_07/README.md | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/Week_07/README.md b/Week_07/README.md index 107ea7d6..5b8e0e55 100644 --- a/Week_07/README.md +++ b/Week_07/README.md @@ -1 +1,112 @@ # 学习笔记 +## 字典树和并查集 +### 字典树 +   字典树,既Trie树,又称单词查找树或键树,是一种树形结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。 +#### 基本性质 +- 结点本身不存完整单词 +- 从根结点到某一结点,路径上经过的字符连接起来,为该结点对应的 字符串; +- 每个结点的所有子结点路径代表的字符都不相同 +#### 实现代码 +``` +class Trie { +private: + bool is_end; + Trie* next[26]; +public: + /** Initialize your data structure here. */ + Trie() { + is_end = false; + memset(next, 0, sizeof(next)); + } + + /** Inserts a word into the trie. */ + void insert(string word) { + Trie *node = this; + for(char c : word) { + if(node->next[c - 'a'] == nullptr) { + node->next[c - 'a'] = new Trie(); + } + node = node->next[c - 'a']; + } + node->is_end = true; + } + + /** Returns if the word is in the trie. */ + bool search(string word) { + Trie *node = this; + for(char c : word) { + node = node->next[c - 'a']; + if(node == nullptr) return false; + } + return node->is_end; + } + + /** Returns if there is any word in the trie that starts with the given prefix. */ + bool startsWith(string prefix) { + Trie *node = this; + for(char c : prefix) { + node = node->next[c - 'a']; + if(node == nullptr) return false; + } + return true; + } +}; +``` + +### 并查集 +   为解决组团、配对等问题等一种数据结构 +#### 基本操作 +- makeSet(s):建立一个新的并查集,其中包含 s 个单元素集合。 +- unionSet(x, y):把元素 x 和元素 y 所在的集合合并,要求 x 和 y 所在的集合不相交,如果相交则不合并。 +- find(x):找到元素 x 所在的集合的代表,该操作也可以用于判断两个元 素是否位于同一个集合,只要将它们各自的代表比较一下就可以了。 +#### 实现方式 +``` +class UF { +private: + int count; + int* parent; + int* size; + + int find(int n) { + while(parent[n] != n) { + parent[n] = parent[parent[n]]; + n = parent[n]; + } + return n; + } +public: + UF(int n) { + count = n; + this->parent = new int[n]; + this->size = new int[n]; + for(int i = 0; i size[rootQ]) { + parent[rootQ] = parent[rootP]; + size[rootP] += size[rootQ]; + } else { + parent[rootP] = parent[rootQ]; + size[rootQ] += size[rootP]; + } + count--; + } + + int getCount() { + return count; + } + +}; +``` + + +### 高级搜索 + +### 红黑树和AVL树 From 9987b4013dda1257a5e8a1185a6a7cd621104ca6 Mon Sep 17 00:00:00 2001 From: MingLL Date: Tue, 24 Nov 2020 22:14:29 +0800 Subject: [PATCH 15/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\345\257\271\346\216\222\345\272\217.cpp" | 26 +++++++++++++++++++ ...4\350\277\233\345\210\266\344\275\215.cpp" | 21 +++++++++++++++ ...-\347\232\204\344\270\252\346\225\260.cpp" | 20 ++++++++++++++ "Week_08/231.2-\347\232\204\345\271\202.cpp" | 15 +++++++++++ ...5\345\274\202\344\275\215\350\257\215.cpp" | 26 +++++++++++++++++++ Week_08/README.md | 2 +- 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 "Week_08/1122.\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217.cpp" create mode 100644 "Week_08/190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.cpp" create mode 100644 "Week_08/191.\344\275\215-1-\347\232\204\344\270\252\346\225\260.cpp" create mode 100644 "Week_08/231.2-\347\232\204\345\271\202.cpp" create mode 100644 "Week_08/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" diff --git "a/Week_08/1122.\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217.cpp" "b/Week_08/1122.\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217.cpp" new file mode 100644 index 00000000..0824e225 --- /dev/null +++ "b/Week_08/1122.\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217.cpp" @@ -0,0 +1,26 @@ +/* + * @lc app=leetcode.cn id=1122 lang=cpp + * + * [1122] 数组的相对排序 + */ + +// @lc code=start +class Solution { +public: + vector relativeSortArray(vector& arr1, vector& arr2) { + unordered_map rank; + for(int i = 0; i < arr2.size(); i++) { + rank[arr2[i]] = i; + } + sort(arr1.begin(), arr1.end(), [&](int x, int y) { + if(rank.count(x)) { + return rank.count(y) ? rank[x] < rank[y] : true; + } else { + return rank.count(y) ? false : x < y; + } + }); + return arr1; + } +}; +// @lc code=end + diff --git "a/Week_08/190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.cpp" "b/Week_08/190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.cpp" new file mode 100644 index 00000000..3334bbc1 --- /dev/null +++ "b/Week_08/190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.cpp" @@ -0,0 +1,21 @@ +/* + * @lc app=leetcode.cn id=190 lang=cpp + * + * [190] 颠倒二进制位 + */ + +// @lc code=start +class Solution { +public: + uint32_t reverseBits(uint32_t n) { + uint32_t res = 0, power =31; + while(n !=0) { + res += (n &1) << power; + n = n >> 1; + power--; + } + return res; + } +}; +// @lc code=end + diff --git "a/Week_08/191.\344\275\215-1-\347\232\204\344\270\252\346\225\260.cpp" "b/Week_08/191.\344\275\215-1-\347\232\204\344\270\252\346\225\260.cpp" new file mode 100644 index 00000000..a5b19ade --- /dev/null +++ "b/Week_08/191.\344\275\215-1-\347\232\204\344\270\252\346\225\260.cpp" @@ -0,0 +1,20 @@ +/* + * @lc app=leetcode.cn id=191 lang=cpp + * + * [191] 位1的个数 + */ + +// @lc code=start +class Solution { +public: + int hammingWeight(uint32_t n) { + int zeroCount =0; + while (n != 0) { + zeroCount++; + n &= (n -1); + } + return zeroCount; + } +}; +// @lc code=end + diff --git "a/Week_08/231.2-\347\232\204\345\271\202.cpp" "b/Week_08/231.2-\347\232\204\345\271\202.cpp" new file mode 100644 index 00000000..9eb46c82 --- /dev/null +++ "b/Week_08/231.2-\347\232\204\345\271\202.cpp" @@ -0,0 +1,15 @@ +/* + * @lc app=leetcode.cn id=231 lang=cpp + * + * [231] 2的幂 + */ + +// @lc code=start +class Solution { +public: + bool isPowerOfTwo(int n) { + return (n >0) && ((n & (n -1)) == 0); + } +}; +// @lc code=end + diff --git "a/Week_08/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" "b/Week_08/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" new file mode 100644 index 00000000..c839dfbd --- /dev/null +++ "b/Week_08/242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" @@ -0,0 +1,26 @@ +/* + * @lc app=leetcode.cn id=242 lang=cpp + * + * [242] 有效的字母异位词 + */ + +// @lc code=start +class Solution { +public: + bool isAnagram(string s, string t) { + if(s.length() != t.length()) return false; + vector v(26, 0); + for(int i = 0; i < s.length(); i++) { + v[s[i] - 'a']++; + v[t[i] - 'a']--; + } + for(int i = 0; i < 26; i++) { + if(v[i] != 0) { + return false; + } + } + return true; + } +}; +// @lc code=end + diff --git a/Week_08/README.md b/Week_08/README.md index 50de3041..107ea7d6 100644 --- a/Week_08/README.md +++ b/Week_08/README.md @@ -1 +1 @@ -学习笔记 \ No newline at end of file +# 学习笔记 From 146eb572da64cd1cbe11a10deaaddd53601511d9 Mon Sep 17 00:00:00 2001 From: MingLL Date: Wed, 25 Nov 2020 00:11:25 +0800 Subject: [PATCH 16/23] =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_08/README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/Week_08/README.md b/Week_08/README.md index 107ea7d6..e1a75ac7 100644 --- a/Week_08/README.md +++ b/Week_08/README.md @@ -1 +1,58 @@ # 学习笔记 +## 位运算 +  机器里面的数字都是以二进制存储的,所以了解二进制的一些基本运算是提高程序运行的效率和代码质量的基本功。 +### 基础知识 +- 十进制转换为二进制 使用余数短除法除以2,最后将余数倒过来写。 +- 二进制转十进制 第n位和2的^n-1相乘,在相加结果就是十进制 +### 位运算符 +|意义| 运算符|示例| +|----|-----|----| +|左移|<<|0011 => 0110| +|右移|>>| 0110 => 0011| +|按位或| | | 0011
1011
-----
1011 +|按位与|&| 0011
1011
-----
0011 +|按位取反|~| 0011 => 1100 +|按位异或|^| 0011
1000
-----
1011 + +### 小技巧 +异或:相同为 0,不同为 1。也可用“不进位加法”来理解。 +- 异或操作的一些特点: + - x^0=x + - x^1s=~x //注意 1s = ~0 + - x^(~x)=1s + - x^x=0 + - c=a^b => a^c=b,b^c=a //交换两个数 + - a^b^c=a^(b^c)=(a^b)^c //associative +- 指定位置的位运算 + - 将x最右边的n位清零:x&(~0<>n)&1 + - 获取x的第n位的幂值:x&(1<(x&1)==1 x%2==0 —>(x&1)==0 +- 除2 +  x>>1—>x/2.即: x=x/2; —> x=x>>1; +  mid=(left+right)/2; —> mid=(left+right)>>1; +- 清零最低位的1 + X=X&(X-1) +- 得到最低位的1 + X&-X=>得到最低位的1 +- X&~X=>0 + +## 布隆过滤器、LRU Cache +### 布隆过滤器 +  布隆过滤器可以用于检索一个元素是否在一个集合中。 +#### 使用场景 +1. 比特币网络 +2. 分布式系统(Map-Reduce) — Hadoop、search engine +3. Redis 缓存 +4. 垃圾邮件、评论等的过滤 + +#### LRU Cache +  使用两端链表和hashMap实现的一种数据结构,最近最少使用。 + + + From 8bc219596a40434b192bd0397f32f2cde0411e4f Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 29 Nov 2020 23:09:56 +0800 Subject: [PATCH 17/23] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\347\232\204\345\215\225\350\257\215.cpp" | 37 +++++++++++++++++++ ...7\344\270\200\345\255\227\347\254\246.cpp" | 24 ++++++++++++ ...5\345\274\202\344\275\215\350\257\215.cpp" | 32 ++++++++++++++++ ...45\255\227\347\254\246\344\270\262-ii.cpp" | 22 +++++++++++ ...\347\254\246\344\270\262-\342\205\261.cpp" | 32 ++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 "Week_09/151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.cpp" create mode 100644 "Week_09/387.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.cpp" create mode 100644 "Week_09/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" create mode 100644 "Week_09/541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262-ii.cpp" create mode 100644 "Week_09/680.\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262-\342\205\261.cpp" diff --git "a/Week_09/151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.cpp" "b/Week_09/151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.cpp" new file mode 100644 index 00000000..299e3e87 --- /dev/null +++ "b/Week_09/151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.cpp" @@ -0,0 +1,37 @@ +/* + * @lc app=leetcode.cn id=151 lang=cpp + * + * [151] 翻转字符串里的单词 + */ + +// @lc code=start +class Solution { +public: + string reverseWords(string s) { + // 反转整个字符串 + reverse(s.begin(), s.end()); + + int n = s.size(); + int idx = 0; + for (int start = 0; start < n; ++start) { + if (s[start] != ' ') { + // 填一个空白字符然后将idx移动到下一个单词的开头位置 + if (idx != 0) s[idx++] = ' '; + + // 循环遍历至单词的末尾 + int end = start; + while (end < n && s[end] != ' ') s[idx++] = s[end++]; + + // 反转整个单词 + reverse(s.begin() + idx - (end - start), s.begin() + idx); + + // 更新start,去找下一个单词 + start = end; + } + } + s.erase(s.begin() + idx, s.end()); + return s; + } +}; +// @lc code=end + diff --git "a/Week_09/387.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.cpp" "b/Week_09/387.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.cpp" new file mode 100644 index 00000000..78865825 --- /dev/null +++ "b/Week_09/387.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.cpp" @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode.cn id=387 lang=cpp + * + * [387] 字符串中的第一个唯一字符 + */ + +// @lc code=start +class Solution { +public: + int firstUniqChar(string s) { + int map[256] = {0}; + for(int i=0; i < s.length(); i++) { + map[s[i]]++; + } + for(int i=0; i < s.length(); i++) { + if(map[s[i]] == 1) { + return i; + } + } + return -1; + } +}; +// @lc code=end + diff --git "a/Week_09/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" "b/Week_09/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" new file mode 100644 index 00000000..591b7036 --- /dev/null +++ "b/Week_09/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.cpp" @@ -0,0 +1,32 @@ +/* + * @lc app=leetcode.cn id=438 lang=cpp + * + * [438] 找到字符串中所有字母异位词 + */ + +// @lc code=start +class Solution { +public: + vector findAnagrams(string s, string p) { + vector res; + int map[128] = { 0 }; + int left =0, right =0, need =0; + for(int i =0; i< p.size(); i++) { + map[p[i]]++; + } + while(right < s.size()) { + if(map[s[right]] > 0) need++; + map[s[right]]--; + right++; + while(need == p.size()) { + if(right -left == p.size()) res.push_back(left); + if(map[s[left]] == 0) need--; + map[s[left]]++; + left++; + } + } + return res; + } +}; +// @lc code=end + diff --git "a/Week_09/541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262-ii.cpp" "b/Week_09/541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262-ii.cpp" new file mode 100644 index 00000000..6c65fe9c --- /dev/null +++ "b/Week_09/541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262-ii.cpp" @@ -0,0 +1,22 @@ +/* + * @lc app=leetcode.cn id=541 lang=cpp + * + * [541] 反转字符串 II + */ + +// @lc code=start +class Solution { +public: + string reverseStr(string s, int k) { + for(int i=0; i < s.length(); i += (2*k)) { + if(i +k <= s.length()) { + reverse(s.begin() +i, s.begin() +i +k); + continue; + } + reverse(s.begin() +i, s.end()); + } + return s; + } +}; +// @lc code=end + diff --git "a/Week_09/680.\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262-\342\205\261.cpp" "b/Week_09/680.\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262-\342\205\261.cpp" new file mode 100644 index 00000000..87a62113 --- /dev/null +++ "b/Week_09/680.\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262-\342\205\261.cpp" @@ -0,0 +1,32 @@ +/* + * @lc app=leetcode.cn id=680 lang=cpp + * + * [680] 验证回文字符串 Ⅱ + */ + +// @lc code=start +class Solution { +public: + bool validPalindrome(string s) { + int begin =0, end = s.size() -1; + while(begin < end) { + if(s[begin] == s[end]) { + begin++; + end--; + } else { + return checkPalindrome(s, begin, end -1) || checkPalindrome(s, begin +1, end); + } + } + return true; + } + + bool checkPalindrome(const string& str, int begin, int end) { + while(begin < end) { + if(str[begin++] != str[end--]) + return false; + } + return true; + } +}; +// @lc code=end + From ef13c9c31ef2ca69570b84b21a04eb3788d43212 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 29 Nov 2020 23:51:02 +0800 Subject: [PATCH 18/23] =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\345\255\220\345\272\217\345\210\227.cpp" | 29 ++++++ Week_09/README.md | 89 ++++++++++++++++++- 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 "Week_09/300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.cpp" diff --git "a/Week_09/300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.cpp" "b/Week_09/300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.cpp" new file mode 100644 index 00000000..c7fe049c --- /dev/null +++ "b/Week_09/300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.cpp" @@ -0,0 +1,29 @@ +/* + * @lc app=leetcode.cn id=300 lang=cpp + * + * [300] 最长上升子序列 + */ + +// @lc code=start +class Solution { +public: + int lengthOfLIS(vector& nums) { + if(nums.size() == 0) return 0; + int dp[nums.size()]; + dp[0] = 1; + int maxLength = 1; + for(int i = 1; i < nums.size(); i++) { + int maxval = 0; + for(int j =0; j < i; j++) { + if(nums[i] > nums[j]){ + maxval = max(maxval, dp[j]); + } + } + dp[i] = maxval +1; + maxLength = max(maxLength,dp[i]); + } + return maxLength; + } +}; +// @lc code=end + diff --git a/Week_09/README.md b/Week_09/README.md index 50de3041..05cef1c7 100644 --- a/Week_09/README.md +++ b/Week_09/README.md @@ -1 +1,88 @@ -学习笔记 \ No newline at end of file +# 学习笔记 +## 高级动态规划 +  高级动态规划的基础就是练习好,递归,分治,回溯还有定义状态转移方程。 +### 复习基础 +- 递归模版 +``` + void recur(int level, int parm) { + // 递归终止条件 + if(level > MAX_LEVEL) { + // 保存结果 + return; + } + // 处理当前层逻辑 + // 下探到下一层 + recur(level +1, parm); + + // 清理当前层状态 + } +``` +- 分治模版 +``` + int divide_conquer(Pragram *pragram, int parms) { + if(pragram == null) { + // 处理结果 + return return_result; + } + // 拆分问题 + subproblems = split_problem(problem, data); + subresult1 = divide_conquer(subproblem[0], p1); + subresult2 = divide_conquer(subproblem[1], p1); + subresult3 = divide_conquer(subproblem[2], p1); + ... + + // 合并结果 + result = merge(subresult1, subresult2, subresult3..); + // 清理当前层; + return result; + } +``` + +### 感触 +- 不能人肉递归 +- 找到最近最简方法 +- 数学归纳法思想 + + + +## 字符串 +  字符串是编程中最常见的数据结构,每一个编程语言都有实现的Stirng(C语言除外)。 +### 字符串中的算法 +- 最长回文子串 + - 暴力求解(O(n^3)); + - 中心扩散法(O(n^2)); + ``` + class Solution { + public: + string longestPalindrome(string s) { + if(s.size() < 1) return ""; + int maxStr =0; + int begin =0; + for(int i =0; i < s.size();i++) { + int len1 = expandAroundString(s, i, i); + int len2 = expandAroundString(s, i, i +1); + int len = len1 > len2 ? len1 : len2; + if(len > maxStr) { + maxStr = len; + begin = i - (len -1)/2; + } + } + return s.substr(begin, maxStr); + } + int expandAroundString(const string& s, int left, int right) { + int L = left, R = right; + while(L >= 0 && R < s.size() && s[L] == s[R]) { + L--; + R++; + } + return R - L -1; + } + }; + ``` + - 动态规划 + + + + + + From c946000831a34883627acc8f697ca39d5f8ed323 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 13 Dec 2020 20:01:49 +0800 Subject: [PATCH 19/23] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AD=A6=E4=B9=A0?= =?UTF-8?q?=E5=B0=8F=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_10/README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Week_10/README.md b/Week_10/README.md index 50de3041..359e0cfa 100644 --- a/Week_10/README.md +++ b/Week_10/README.md @@ -1 +1,22 @@ -学习笔记 \ No newline at end of file +# 学习小结 +> 我就是这样的我,不求伟大,却又不甘于平凡。 +# 回顾初心❤️ +  作为一名程序员,我想每一个人都有一个大厂梦。当然我也不例外。作为一名大四的学生,今年我开始了人生中第一次找工作。开始的时候,投递了字节,阿里,腾讯这样的一线大厂。他们的每一次面试都有一个或者多个算法题目。算法与数据结构在大学里面是一门很重要的课程,当时的我不知道它的重要性和其他课程一样,考前很短的时间复习,只是过了这门课程。导致今年的面试开始的时候没有一个通过的。 +  在九月份时,知道自己不能在这样下去了,**算法不是简简单单自己一个人可以在很短的时间里面搞定的**在听完算法训练营的试听课后,我购买了这门课程。再经过那段时间的认真的学习,我收获了一份大厂的offer。 + +# 灯塔💡 +> 有个优秀的领路人,真的可以帮助你少走很多弯路。 +  在课程中,有一位优秀的老师覃超老师。覃超老师理论和实践相结合,让我理解当时学习最差的递归,分治,动态规划。我相信很多学习者都会被这个几个算法劝退算法。 最开始我也很害怕,但是现在我也有了和它一战的勇气。覃超老师,在这几个算法时,用了大量的例题讲解,在听不懂的时候,一定要多听几遍,我相信每一遍都会有不同的收获。 + +# 起航🛳 +> 这不是结束,只是新旅程的刚刚开始 +  在这次课程中,我收获到了 不仅仅是算法的知识,还有一些学习的方法。 +- 不要和一道题目死磕 + 我相信每个人都一个争强心,最开始我也有,上学期间那颗心从来没有认输过。但是现在我懂了放弃,不是说没有了争强的心,是学习更好的知识,借鉴别人的想法,当积累更多的时候,终有一天你会灵感激发,想出一个一鸣惊人的算法。知识不就是这样嘛,在遇到不会的,可以借鉴别人的想法。 +- 做题不能只做一遍 + 这就和背东西一样,题目也是一样,做一遍肯定会忘,遍数一定会让你记住这个东西。就想排序算法一样,现在让你手撕一个快速排序,可以写出来嘛,你可以说百度啊,一大堆一大堆的,但是那不是自己。 +- 坚持输出 + 知识学到了,可以用自己的语言写出来,发出来和大家一起探讨,思想的碰撞是人类成长的原因。如果知识都是一模一样的那可能就没有我们现在这么先进的科技,这么优质的生活了。 + +# 最后的话 +  课程虽然结束了,但不意味着算法知识学习就结束了。 这是我新的起点的开始,加油,不能说我掌握所有的算法知识,但是我一定要精通学习最基本的知识。尤其的是递归、分治、动态规划这几个算法一定要熟练的使用。 \ No newline at end of file From 49107b695a8e88ee7871d38617c729c91352b6a7 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 13 Dec 2020 20:03:58 +0800 Subject: [PATCH 20/23] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_10/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Week_10/README.md b/Week_10/README.md index 359e0cfa..98c3e42a 100644 --- a/Week_10/README.md +++ b/Week_10/README.md @@ -11,11 +11,11 @@ # 起航🛳 > 这不是结束,只是新旅程的刚刚开始   在这次课程中,我收获到了 不仅仅是算法的知识,还有一些学习的方法。 -- 不要和一道题目死磕 +- **不要和一道题目死磕** 我相信每个人都一个争强心,最开始我也有,上学期间那颗心从来没有认输过。但是现在我懂了放弃,不是说没有了争强的心,是学习更好的知识,借鉴别人的想法,当积累更多的时候,终有一天你会灵感激发,想出一个一鸣惊人的算法。知识不就是这样嘛,在遇到不会的,可以借鉴别人的想法。 -- 做题不能只做一遍 +- **做题不能只做一遍** 这就和背东西一样,题目也是一样,做一遍肯定会忘,遍数一定会让你记住这个东西。就想排序算法一样,现在让你手撕一个快速排序,可以写出来嘛,你可以说百度啊,一大堆一大堆的,但是那不是自己。 -- 坚持输出 +- **坚持输出** 知识学到了,可以用自己的语言写出来,发出来和大家一起探讨,思想的碰撞是人类成长的原因。如果知识都是一模一样的那可能就没有我们现在这么先进的科技,这么优质的生活了。 # 最后的话 From 924e4c13abc755a359606cb40e296ba92793e55f Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 13 Dec 2020 20:07:38 +0800 Subject: [PATCH 21/23] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_10/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Week_10/README.md b/Week_10/README.md index 98c3e42a..12d9fe2e 100644 --- a/Week_10/README.md +++ b/Week_10/README.md @@ -6,10 +6,12 @@ # 灯塔💡 > 有个优秀的领路人,真的可以帮助你少走很多弯路。 +   在课程中,有一位优秀的老师覃超老师。覃超老师理论和实践相结合,让我理解当时学习最差的递归,分治,动态规划。我相信很多学习者都会被这个几个算法劝退算法。 最开始我也很害怕,但是现在我也有了和它一战的勇气。覃超老师,在这几个算法时,用了大量的例题讲解,在听不懂的时候,一定要多听几遍,我相信每一遍都会有不同的收获。 # 起航🛳 -> 这不是结束,只是新旅程的刚刚开始 +> 这不是结束,只是新旅程的刚刚开始 +   在这次课程中,我收获到了 不仅仅是算法的知识,还有一些学习的方法。 - **不要和一道题目死磕** 我相信每个人都一个争强心,最开始我也有,上学期间那颗心从来没有认输过。但是现在我懂了放弃,不是说没有了争强的心,是学习更好的知识,借鉴别人的想法,当积累更多的时候,终有一天你会灵感激发,想出一个一鸣惊人的算法。知识不就是这样嘛,在遇到不会的,可以借鉴别人的想法。 From 18f67de3ac38a1da580623f69a37899a1e4152ad Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 13 Dec 2020 20:09:20 +0800 Subject: [PATCH 22/23] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_10/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Week_10/README.md b/Week_10/README.md index 12d9fe2e..943153d9 100644 --- a/Week_10/README.md +++ b/Week_10/README.md @@ -1,7 +1,8 @@ # 学习小结 > 我就是这样的我,不求伟大,却又不甘于平凡。 # 回顾初心❤️ -  作为一名程序员,我想每一个人都有一个大厂梦。当然我也不例外。作为一名大四的学生,今年我开始了人生中第一次找工作。开始的时候,投递了字节,阿里,腾讯这样的一线大厂。他们的每一次面试都有一个或者多个算法题目。算法与数据结构在大学里面是一门很重要的课程,当时的我不知道它的重要性和其他课程一样,考前很短的时间复习,只是过了这门课程。导致今年的面试开始的时候没有一个通过的。 +  作为一名程序员,我想每一个人都有一个大厂梦。当然我也不例外。作为一名大四的学生,今年我开始了人生中第一次找工作。开始的时候,投递了字节,阿里,腾讯这样的一线大厂。他们的每一次面试都有一个或者多个算法题目。算法与数据结构在大学里面是一门很重要的课程,当时的我不知道它的重要性和其他课程一样,考前很短的时间复习,只是过了这门课程。导致今年的面试开始的时候没有一个通过的。 +   在九月份时,知道自己不能在这样下去了,**算法不是简简单单自己一个人可以在很短的时间里面搞定的**在听完算法训练营的试听课后,我购买了这门课程。再经过那段时间的认真的学习,我收获了一份大厂的offer。 # 灯塔💡 From 85cb2a1840b44ae35bf13f1a5708fb692dc9d205 Mon Sep 17 00:00:00 2001 From: MingLL Date: Sun, 13 Dec 2020 20:09:29 +0800 Subject: [PATCH 23/23] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_10/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week_10/README.md b/Week_10/README.md index 943153d9..861e7c5a 100644 --- a/Week_10/README.md +++ b/Week_10/README.md @@ -2,7 +2,7 @@ > 我就是这样的我,不求伟大,却又不甘于平凡。 # 回顾初心❤️   作为一名程序员,我想每一个人都有一个大厂梦。当然我也不例外。作为一名大四的学生,今年我开始了人生中第一次找工作。开始的时候,投递了字节,阿里,腾讯这样的一线大厂。他们的每一次面试都有一个或者多个算法题目。算法与数据结构在大学里面是一门很重要的课程,当时的我不知道它的重要性和其他课程一样,考前很短的时间复习,只是过了这门课程。导致今年的面试开始的时候没有一个通过的。 - +   在九月份时,知道自己不能在这样下去了,**算法不是简简单单自己一个人可以在很短的时间里面搞定的**在听完算法训练营的试听课后,我购买了这门课程。再经过那段时间的认真的学习,我收获了一份大厂的offer。 # 灯塔💡