From b40d7b55ffac4bc057abcfc513adc3f1f4f0df43 Mon Sep 17 00:00:00 2001 From: Jacob Wang Date: Thu, 17 Oct 2019 22:03:56 +0800 Subject: [PATCH 1/7] Add Week01 Leetcode problems --- Week 01/id_586/NOTE.md | 50 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/Week 01/id_586/NOTE.md b/Week 01/id_586/NOTE.md index a6321d6e2..7db6a4e85 100644 --- a/Week 01/id_586/NOTE.md +++ b/Week 01/id_586/NOTE.md @@ -1,4 +1,52 @@ # NOTE - +## Class 3 + +两数之和题目: https://leetcode-cn.com/problems/two-sum/ + +Array 实战题目 + +https://leetcode-cn.com/problems/container-with-most-water/ +https://leetcode-cn.com/problems/move-zeroes/ +https://leetcode.com/problems/climbing-stairs/ +https://leetcode-cn.com/problems/3sum/ (高频老题) + +Linked List 实战题目 + +https://leetcode.com/problems/reverse-linked-list/ +https://leetcode.com/problems/swap-nodes-in-pairs +https://leetcode.com/problems/linked-list-cycle +https://leetcode.com/problems/linked-list-cycle-ii +https://leetcode.com/problems/reverse-nodes-in-k-group/ + +课后作业 + +https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ +https://leetcode-cn.com/problems/rotate-array/ +https://leetcode-cn.com/problems/merge-two-sorted-lists/ +https://leetcode-cn.com/problems/merge-sorted-array/ +https://leetcode-cn.com/problems/two-sum/ +https://leetcode-cn.com/problems/move-zeroes/ +https://leetcode-cn.com/problems/plus-one/ + +## Class 4 + +预习题目 + +https://leetcode-cn.com/problems/valid-parentheses/ +https://leetcode-cn.com/problems/min-stack/ + +实战题目 + +https://leetcode-cn.com/problems/largest-rectangle-in-histogram +https://leetcode-cn.com/problems/sliding-window-maximum + +课后作业 + +用 add first 或 add last 这套新的 API 改写 Deque 的代码 +分析 Queue 和 Priority Queue 的源码 +https://leetcode.com/problems/design-circular-deque +https://leetcode.com/problems/trapping-rain-water/ + +说明:改写代码和分析源码这两项作业,同学们需要在第 1 周的个人总结中完成。 From be9aa1a9ec29ebdd6944146e009881c751619945 Mon Sep 17 00:00:00 2001 From: Jacob Wang Date: Fri, 18 Oct 2019 21:27:06 +0800 Subject: [PATCH 2/7] Add Leetcode 11. container-with-most-water --- Week 01/id_586/11-container-with-most-water.c | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Week 01/id_586/11-container-with-most-water.c diff --git a/Week 01/id_586/11-container-with-most-water.c b/Week 01/id_586/11-container-with-most-water.c new file mode 100644 index 000000000..4531cdb79 --- /dev/null +++ b/Week 01/id_586/11-container-with-most-water.c @@ -0,0 +1,45 @@ +int maxArea(int* height, int heightSize){ +#if 0 + /* 1. 暴力求解 */ + int i = 0; + int j = 0; + int max = 0; + int area = 0; + int h = 0; + + for (; i < heightSize; i++) { + for (j = i + 1; j < heightSize; j++) { + h = (height[i] < height[j] ? height[i] : height[j]); + area = (j - i) * h; + if (max < area) + max = area; + } + } + + return max; +#else + /* 双指针法 */ + /* 重点在于理解双指针的移动,每次都移动较短的指针 */ + + int i = 0; + int j = heightSize - 1; + + int max = 0; + int area = 0; + int h = 0; + + while (i < j) { + h = (height[i] < height[j] ? height[i] : height[j]); + area = (j - i) * h; + if (max < area) max = area; + //if (height[i] < height[j]) + // i++; + // else + // j--; + while ((height[i] <= h) && i < j) i++; + while ((height[j] <= h) && i < j) j--; + } + + return max; +#endif +} From a0d8943873724197206d9ea47848d624efc1cb26 Mon Sep 17 00:00:00 2001 From: Jacob Wang Date: Fri, 18 Oct 2019 21:28:43 +0800 Subject: [PATCH 3/7] Add Leetcode 24, 141, 142, 206 --- Week 01/id_586/141-linked-list-cycle.c | 32 +++++++++++++++++ Week 01/id_586/142-linked-list-cycle-ii.c | 44 +++++++++++++++++++++++ Week 01/id_586/206-reverse-linked-list.c | 24 +++++++++++++ Week 01/id_586/24-swap-nodes-in-pairs.c | 39 ++++++++++++++++++++ Week 01/id_586/NOTE.md | 1 - 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 Week 01/id_586/141-linked-list-cycle.c create mode 100644 Week 01/id_586/142-linked-list-cycle-ii.c create mode 100644 Week 01/id_586/206-reverse-linked-list.c create mode 100644 Week 01/id_586/24-swap-nodes-in-pairs.c diff --git a/Week 01/id_586/141-linked-list-cycle.c b/Week 01/id_586/141-linked-list-cycle.c new file mode 100644 index 000000000..4ae6043fd --- /dev/null +++ b/Week 01/id_586/141-linked-list-cycle.c @@ -0,0 +1,32 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +/* + * 使用的是快慢指针的方法 + * */ +bool hasCycle(struct ListNode *head) { + struct ListNode *slow = head; + struct ListNode *fast = head; + + if (!head || !head->next) return 0; + + while (slow && fast) { + slow = slow->next; + fast = fast->next; + + if (!fast) + return 0; + + fast = fast->next; + + if (slow == fast) + return 1; + } + + return 0; +} diff --git a/Week 01/id_586/142-linked-list-cycle-ii.c b/Week 01/id_586/142-linked-list-cycle-ii.c new file mode 100644 index 000000000..d7078a7a7 --- /dev/null +++ b/Week 01/id_586/142-linked-list-cycle-ii.c @@ -0,0 +1,44 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode *detectCycle(struct ListNode *head) { + if (head == NULL || head->next == NULL) + return NULL; + + struct ListNode *fast = head; + struct ListNode *slow = head; + struct ListNode *meet = NULL; + + while (fast) { + slow = slow->next; + fast = fast->next; + + if (!fast) { + return false; + } + + fast = fast->next; + + if (fast == slow) { + meet = fast; + break; + } + } + + if (meet == NULL) + return NULL; + + while (head && meet) { + if (head == meet) { + return head; + } + head = head->next; + meet = meet->next; + } + + return NULL; +} diff --git a/Week 01/id_586/206-reverse-linked-list.c b/Week 01/id_586/206-reverse-linked-list.c new file mode 100644 index 000000000..6aaad0bcc --- /dev/null +++ b/Week 01/id_586/206-reverse-linked-list.c @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +/* 1. 使用一个 new_head 作为翻转链表的头指针的,遍历给定的链表, + * 2. 把遍历的元素使用头插的方法,插入链表, + * 3. 返回 new_head */ +struct ListNode* reverseList(struct ListNode* head){ + struct ListNode *new_head = NULL; + + while (head) { + struct ListNode *tmp = head; + head = head->next; + tmp->next = new_head; + new_head = tmp; + } + + return new_head; +} + diff --git a/Week 01/id_586/24-swap-nodes-in-pairs.c b/Week 01/id_586/24-swap-nodes-in-pairs.c new file mode 100644 index 000000000..a55130c2b --- /dev/null +++ b/Week 01/id_586/24-swap-nodes-in-pairs.c @@ -0,0 +1,39 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +/* + * 1. 先保存需要替换的指向的内容 + * 2. 依次修改指针个指向 + * 3. 调整新的头节点 + * */ +struct ListNode* swapPairs(struct ListNode* head){ + struct ListNode dummy; + struct ListNode *prev; + struct ListNode* n1 = NULL; + struct ListNode* n2 = NULL; + struct ListNode* last = NULL; + + dummy.next = head; + prev = &dummy; + + while (head && head->next) { + n1 = head; + n2 = head->next; + last = head->next->next; + + prev->next = n2; + n2->next = n1; + n1->next = last; + + prev = n1; + head = last; + } + + return dummy.next; +} + diff --git a/Week 01/id_586/NOTE.md b/Week 01/id_586/NOTE.md index 7db6a4e85..d8598696b 100644 --- a/Week 01/id_586/NOTE.md +++ b/Week 01/id_586/NOTE.md @@ -49,4 +49,3 @@ https://leetcode.com/problems/design-circular-deque https://leetcode.com/problems/trapping-rain-water/ 说明:改写代码和分析源码这两项作业,同学们需要在第 1 周的个人总结中完成。 - From 07916386f69b33579de523ff9ffa2b84bb06a360 Mon Sep 17 00:00:00 2001 From: Jacob Wang <13976678+jac0bwang@users.noreply.github.com> Date: Sun, 20 Oct 2019 21:24:26 +0800 Subject: [PATCH 4/7] Create 21-merge-two-sorted-lists.c --- Week 01/id_586/21-merge-two-sorted-lists.c | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Week 01/id_586/21-merge-two-sorted-lists.c diff --git a/Week 01/id_586/21-merge-two-sorted-lists.c b/Week 01/id_586/21-merge-two-sorted-lists.c new file mode 100644 index 000000000..48e8ef7d9 --- /dev/null +++ b/Week 01/id_586/21-merge-two-sorted-lists.c @@ -0,0 +1,35 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + + +struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){ + if (!l1) return l2; + if (!l2) return l1; + + struct ListNode dummy; + struct ListNode *prev = &dummy; + + while (l1 && l2) { + if (l1->val < l2->val) { + prev->next = l1; + l1 = l1->next; + } else { + prev->next = l2; + l2 = l2->next; + } + + prev = prev->next; + } + + if (l1) + prev->next = l1; + if (l2) + prev->next = l2; + + return dummy.next; +} From 5c8645fc005e875064576f3a1bf9eccaa4a0961c Mon Sep 17 00:00:00 2001 From: Jacob Wang <13976678+jac0bwang@users.noreply.github.com> Date: Sun, 20 Oct 2019 21:32:33 +0800 Subject: [PATCH 5/7] Create 26-remove-duplicates-from-sorted-array.c --- .../26-remove-duplicates-from-sorted-array.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Week 01/id_586/26-remove-duplicates-from-sorted-array.c diff --git a/Week 01/id_586/26-remove-duplicates-from-sorted-array.c b/Week 01/id_586/26-remove-duplicates-from-sorted-array.c new file mode 100644 index 000000000..7d2fcf9c3 --- /dev/null +++ b/Week 01/id_586/26-remove-duplicates-from-sorted-array.c @@ -0,0 +1,18 @@ +/* 双指针法: + * 两个指针 ii 和 jj,其中 ii 是慢指针,而 jj 是快指针。只要 nums[i] = nums[j]nums[i]=nums[j],我们就增加 jj 以跳过重复项 + */ +int removeDuplicates(int* nums, int numsSize){ + if (numsSize <= 0) return 0; + + int i = 0; + int j = 0; + + for (j = 1; j < numsSize; j++) { + if (nums[i] != nums[j]) { + i++; + nums[i] = nums[j]; + } + } + + return i + 1; +} From e702a497ccf01d2e9fb8832681a45b00efb25262 Mon Sep 17 00:00:00 2001 From: Jacob Wang <13976678+jac0bwang@users.noreply.github.com> Date: Sun, 20 Oct 2019 21:46:13 +0800 Subject: [PATCH 6/7] Create 42-trapping-rain-water.cc --- Week 01/id_586/42-trapping-rain-water.cc | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Week 01/id_586/42-trapping-rain-water.cc diff --git a/Week 01/id_586/42-trapping-rain-water.cc b/Week 01/id_586/42-trapping-rain-water.cc new file mode 100644 index 000000000..57128d0a1 --- /dev/null +++ b/Week 01/id_586/42-trapping-rain-water.cc @@ -0,0 +1,39 @@ +class Solution { +public: +/* +我们可以不用像方法 2 那样存储最大高度,而是用栈来跟踪可能储水的最长的条形块。使用栈就可以在一次遍历内完成计算。 + +我们在遍历数组时维护一个栈。如果当前的条形块小于或等于栈顶的条形块,我们将条形块的索引入栈,意思是当前的条形块被栈中的前一个条形块界定。如果我们发现一个条形块长于栈顶,我们可以确定栈顶的条形块被当前条形块和栈的前一个条形块界定,因此我们可以弹出栈顶元素并且累加答案到 \text{ans}ans 。 + +算法 + +使用栈来存储条形块的索引下标。 +遍历数组: +当栈非空且 height[current]>height[st.top()] +意味着栈中元素可以被弹出。弹出栈顶元素 top。 +计算当前元素和栈顶元素的距离,准备进行填充操作 +distance=current−st.top()−1 +找出界定高度 +bounded_height=min(height[current],height[st.top()])−height[top] +往答案中累加积水量 ans+=distance×bounded_height +将当前索引下标入栈 +将 current 移动到下个位置 +*/ + int trap(vector& height) { + int ans = 0, current = 0; + stack st; + while (current < height.size()) { + while (!st.empty() && height[current] > height[st.top()]) { + int top = st.top(); + st.pop(); + if (st.empty()) + break; + int distance = current - st.top() - 1; + int bounded_height = min(height[current], height[st.top()]) - height[top]; + ans += distance * bounded_height; + } + st.push(current++); + } + return ans; + } +}; From 9894ef73c98410c95f46bfe70d8538caaa276d27 Mon Sep 17 00:00:00 2001 From: Jacob Wang <13976678+jac0bwang@users.noreply.github.com> Date: Sun, 20 Oct 2019 22:02:16 +0800 Subject: [PATCH 7/7] Create 641-design-circular-deque.cc --- Week 01/id_586/641-design-circular-deque.cc | 91 +++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Week 01/id_586/641-design-circular-deque.cc diff --git a/Week 01/id_586/641-design-circular-deque.cc b/Week 01/id_586/641-design-circular-deque.cc new file mode 100644 index 000000000..809069da4 --- /dev/null +++ b/Week 01/id_586/641-design-circular-deque.cc @@ -0,0 +1,91 @@ +class MyCircularDeque { +private: + vector buffer; + int cnt; + int k; + int front; + int rear; +public: + /** Initialize your data structure here. Set the size of the deque to be k. */ + MyCircularDeque(int k) : buffer(k, 0), cnt(0), k(k), front(k - 1), rear(0) { + } + + /** Adds an item at the front of Deque. Return true if the operation is successful. */ + bool insertFront(int value) { + if (cnt == k) return false; + + buffer[front] = value; + front = (front - 1 + k) % k; + ++cnt; + + return true; + } + + /** Adds an item at the rear of Deque. Return true if the operation is successful. */ + bool insertLast(int value) { + if (cnt == k) return false; + + buffer[rear] = value; + rear = (rear + 1) % k; + ++cnt; + + return true; + } + + /** Deletes an item from the front of Deque. Return true if the operation is successful. */ + bool deleteFront() { + if (cnt == 0) return false; + + front = (front + 1) % k; + --cnt; + + return true; + } + + /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ + bool deleteLast() { + if (cnt == 0) return false; + + rear = (rear - 1 + k) % k; + --cnt; + + return true; + } + + /** Get the front item from the deque. */ + int getFront() { + if (cnt == 0) return -1; + + return buffer[(front + 1) % k]; + } + + /** Get the last item from the deque. */ + int getRear() { + if (cnt == 0) return -1; + + return buffer[(rear - 1 + k) % k]; + } + + /** Checks whether the circular deque is empty or not. */ + bool isEmpty() { + return cnt == 0; + } + + /** Checks whether the circular deque is full or not. */ + bool isFull() { + return cnt == k; + } +}; + +/** + * 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(); + */