diff --git a/problems/209.minimum-size-subarray-sum.md b/problems/209.minimum-size-subarray-sum.md index 674b9d6af..10565ceb1 100644 --- a/problems/209.minimum-size-subarray-sum.md +++ b/problems/209.minimum-size-subarray-sum.md @@ -31,7 +31,7 @@ If you have figured out the O(n) solution, try coding another solution of which ## 代码 -* 语言支持:JS,C++ +- 语言支持:JS,C++ JavaScript Code: @@ -41,31 +41,6 @@ JavaScript Code: * * [209] Minimum Size Subarray Sum * - * https://leetcode.com/problems/minimum-size-subarray-sum/description/ - * - * algorithms - * Medium (34.31%) - * Total Accepted: 166.9K - * Total Submissions: 484.9K - * Testcase Example: '7\n[2,3,1,2,4,3]' - * - * Given an array of n positive integers and a positive integer s, find the - * minimal length of a contiguous subarray of which the sum ≥ s. If there isn't - * one, return 0 instead. - * - * Example: - * - * - * Input: s = 7, nums = [2,3,1,2,4,3] - * Output: 2 - * Explanation: the subarray [4,3] has the minimal length under the problem - * constraint. - * - * Follow up: - * - * If you have figured out the O(n) solution, try coding another solution of - * which the time complexity is O(n log n). - * */ /** * @param {number} s @@ -98,6 +73,7 @@ var minSubArrayLen = function(s, nums) { ``` C++ Code: + ```C++ class Solution { public: @@ -109,7 +85,7 @@ public: total += nums[right++]; } while (right < num_len && total < s); while (left < right && total - nums[left] >= s) total -= nums[left++]; - if (total >=s && min_len > right - left) + if (total >=s && min_len > right - left) min_len = right- left; } return min_len <= num_len ? min_len: 0; diff --git a/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md b/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md index 95ed6d045..c2372d3f5 100644 --- a/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md +++ b/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md @@ -1,5 +1,5 @@ - ## 题目地址 + https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/ ## 题目描述 @@ -14,25 +14,26 @@ After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day Example: Input: [1,2,3,0,2] -Output: 3 +Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell] ``` ## 思路 -这是一道典型的DP问题, DP 问题的核心是找到状态和状态转移方程。 -这道题目的状态似乎比我们常见的那种DP问题要多,这里的状态有buy sell cooldown三种, +这是一道典型的 DP 问题, DP 问题的核心是找到状态和状态转移方程。 + +这道题目的状态似乎比我们常见的那种 DP 问题要多,这里的状态有 buy sell cooldown 三种, 我们可以用三个数组来表示这这三个状态,buy,sell, cooldown. - - buy[i]表示第i天,且以buy结尾的最大利润 - - sell[i]表示第i天,且以sell结尾的最大利润 - - cooldown[i]表示第i天,且以sell结尾的最大利润 +- buy[i]表示第 i 天,且以 buy 结尾的最大利润 +- sell[i]表示第 i 天,且以 sell 结尾的最大利润 +- cooldown[i]表示第 i 天,且以 sell 结尾的最大利润 - 我们思考一下,其实cooldown这个状态数组似乎没有什么用,因此cooldown不会对`profit`产生 - 任何影响。 我们可以进一步缩小为两种状态。 +我们思考一下,其实 cooldown 这个状态数组似乎没有什么用,因此 cooldown 不会对`profit`产生 +任何影响。 我们可以进一步缩小为两种状态。 - - buy[i] 表示第i天,且以buy或者coolwown结尾的最大利润 - - sell[i] 表示第i天,且以sell或者cooldown结尾的最大利润 +- buy[i] 表示第 i 天,且以 buy 或者 coolwown 结尾的最大利润 +- sell[i] 表示第 i 天,且以 sell 或者 cooldown 结尾的最大利润 对应的状态转移方程如下: @@ -43,17 +44,17 @@ Explanation: transactions = [buy, sell, cooldown, buy, sell] sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); ``` -我们来分析一下,buy[i]对应第i的action只能是buy或者cooldown。 +我们来分析一下,buy[i]对应第 i 的 action 只能是 buy 或者 cooldown。 -- 如果是cooldown,那么profit就是 buy[i - 1] -- 如果是buy,那么就是`前一个卖的profit减去今天买股票花的钱`,即 sell[i -2] - prices[i] +- 如果是 cooldown,那么 profit 就是 buy[i - 1] +- 如果是 buy,那么就是`前一个卖的profit减去今天买股票花的钱`,即 sell[i -2] - prices[i] -> 注意这里是i - 2,不是 i-1 ,因为有cooldown的限制 +> 注意这里是 i - 2,不是 i-1 ,因为有 cooldown 的限制 -sell[i]对应第i的action只能是sell或者cooldown。 +sell[i]对应第 i 的 action 只能是 sell 或者 cooldown。 -- 如果是cooldown,那么profit就是 sell[i - 1] -- 如果是sell,那么就是`前一次买的时候获取的利润加上这次卖的钱`,即 buy[i - 1] + prices[i] +- 如果是 cooldown,那么 profit 就是 sell[i - 1] +- 如果是 sell,那么就是`前一次买的时候获取的利润加上这次卖的钱`,即 buy[i - 1] + prices[i] ## 关键点解析 @@ -62,42 +63,11 @@ sell[i]对应第i的action只能是sell或者cooldown。 ## 代码 ```js - - /* * @lc app=leetcode id=309 lang=javascript * * [309] Best Time to Buy and Sell Stock with Cooldown * - * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/ - * - * algorithms - * Medium (43.52%) - * Total Accepted: 88.3K - * Total Submissions: 201.4K - * Testcase Example: '[1,2,3,0,2]' - * - * Say you have an array for which the i^th element is the price of a given - * stock on day i. - * - * Design an algorithm to find the maximum profit. You may complete as many - * transactions as you like (ie, buy one and sell one share of the stock - * multiple times) with the following restrictions: - * - * - * You may not engage in multiple transactions at the same time (ie, you must - * sell the stock before you buy again). - * After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 - * day) - * - * - * Example: - * - * - * Input: [1,2,3,0,2] - * Output: 3 - * Explanation: transactions = [buy, sell, cooldown, buy, sell] - * */ /** * @param {number[]} prices @@ -131,7 +101,6 @@ var maxProfit = function(prices) { ``` ## 相关题目 + - [121.best-time-to-buy-and-sell-stock](./121.best-time-to-buy-and-sell-stock.md) - [122.best-time-to-buy-and-sell-stock-ii](./122.best-time-to-buy-and-sell-stock-ii.md) - - diff --git a/problems/328.odd-even-linked-list.md b/problems/328.odd-even-linked-list.md index 680ef1b3a..d91266feb 100644 --- a/problems/328.odd-even-linked-list.md +++ b/problems/328.odd-even-linked-list.md @@ -1,5 +1,5 @@ - ## 题目地址 + https://leetcode.com/problems/odd-even-linked-list/description/ ## 题目描述 @@ -24,11 +24,13 @@ The first node is considered odd, the second node even and so on ... ``` ## 思路 + 符合直觉的想法是,先遍历一遍找出奇数的节点。然后再遍历一遍找出偶数节点,最后串起来。 -但是有两个问题,如果不修改节点的话,需要借助额外的空间,空间复杂度是N。如果修改的话,会对第二次遍历(遍历偶数节点)造成影响。 +但是有两个问题,如果不修改节点的话,需要借助额外的空间,空间复杂度是 N。如果修改的话,会对第二次遍历(遍历偶数节点)造成影响。 + +因此可以采用一种做法: 遍历一次,每一步同时修改两个节点就好了,这样就可以规避上面两个问题。 -因此可以采用一种做法: 遍历一次,每一步同时修改两个节点就好了,这样就可以规避上面两个问题。 ## 关键点解析 - 用虚拟节点来简化操作 @@ -37,52 +39,17 @@ The first node is considered odd, the second node even and so on ... ## 代码 -* 语言支持:JS,C++ +- 语言支持:JS,C++ JavaScript Code: + ```js /* * @lc app=leetcode id=328 lang=javascript * * [328] Odd Even Linked List * - * https://leetcode.com/problems/odd-even-linked-list/description/ - * - * algorithms - * Medium (48.22%) - * Total Accepted: 137.6K - * Total Submissions: 284.2K - * Testcase Example: '[1,2,3,4,5]' * - * Given a singly linked list, group all odd nodes together followed by the - * even nodes. Please note here we are talking about the node number and not - * the value in the nodes. - * - * You should try to do it in place. The program should run in O(1) space - * complexity and O(nodes) time complexity. - * - * Example 1: - * - * - * Input: 1->2->3->4->5->NULL - * Output: 1->3->5->2->4->NULL - * - * - * Example 2: - * - * - * Input: 2->1->3->5->6->4->7->NULL - * Output: 2->3->6->7->1->5->4->NULL - * - * - * Note: - * - * - * The relative order inside both the even and odd groups should remain as it - * was in the input. - * The first node is considered odd, the second node even and so on ... - * - * */ /** * Definition for singly-linked list. @@ -96,37 +63,37 @@ JavaScript Code: * @return {ListNode} */ var oddEvenList = function(head) { - if (!head || !head.next) return head; + if (!head || !head.next) return head; - const dummyHead1 = { - next: head - } - const dummyHead2 = { - next: head.next - } + const dummyHead1 = { + next: head + }; + const dummyHead2 = { + next: head.next + }; - let odd = dummyHead1.next; - let even = dummyHead2.next; - - while(odd && odd.next && even && even.next) { - const oddNext = odd.next.next; - const evenNext = even.next.next; - - odd.next = oddNext; - even.next = evenNext; - - odd = oddNext; - even = evenNext; - } + let odd = dummyHead1.next; + let even = dummyHead2.next; - odd.next = dummyHead2.next; + while (odd && odd.next && even && even.next) { + const oddNext = odd.next.next; + const evenNext = even.next.next; - return dummyHead1.next; + odd.next = oddNext; + even.next = evenNext; + odd = oddNext; + even = evenNext; + } + + odd.next = dummyHead2.next; + + return dummyHead1.next; }; ``` C++ Code: + ```C++ /** * Definition for singly-linked list. diff --git a/problems/445.add-two-numbers-ii.md b/problems/445.add-two-numbers-ii.md index c3d99dad8..323813429 100644 --- a/problems/445.add-two-numbers-ii.md +++ b/problems/445.add-two-numbers-ii.md @@ -1,5 +1,5 @@ - ## 题目地址 + https://leetcode.com/problems/add-two-numbers-ii/description/ ## 题目描述 @@ -18,12 +18,13 @@ Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7 ``` + ## 思路 由于需要从低位开始加,然后进位。 因此可以采用栈来简化操作。 -依次将两个链表的值分别入栈stack1和stack2,然后相加入栈stack,进位操作用一个变量carried记录即可。 +依次将两个链表的值分别入栈 stack1 和 stack2,然后相加入栈 stack,进位操作用一个变量 carried 记录即可。 -最后根据stack生成最终的链表即可。 +最后根据 stack 生成最终的链表即可。 > 也可以先将两个链表逆置,然后相加,最后将结果再次逆置。 @@ -35,7 +36,8 @@ Output: 7 -> 8 -> 0 -> 7 - 注意特殊情况, 比如 1 + 99 = 100 ## 代码 -* 语言支持:JS,C++ + +- 语言支持:JS,C++ JavaScript Code: @@ -44,34 +46,6 @@ JavaScript Code: * @lc app=leetcode id=445 lang=javascript * * [445] Add Two Numbers II - * - * https://leetcode.com/problems/add-two-numbers-ii/description/ - * - * algorithms - * Medium (49.31%) - * Total Accepted: 83.7K - * Total Submissions: 169K - * Testcase Example: '[7,2,4,3]\n[5,6,4]' - * - * You are given two non-empty linked lists representing two non-negative - * integers. The most significant digit comes first and each of their nodes - * contain a single digit. Add the two numbers and return it as a linked list. - * - * You may assume the two numbers do not contain any leading zero, except the - * number 0 itself. - * - * Follow up: - * What if you cannot modify the input lists? In other words, reversing the - * lists is not allowed. - * - * - * - * Example: - * - * Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) - * Output: 7 -> 8 -> 0 -> 7 - * - * */ /** * Definition for singly-linked list. @@ -86,62 +60,63 @@ JavaScript Code: * @return {ListNode} */ var addTwoNumbers = function(l1, l2) { - const stack1 = []; - const stack2 = []; - const stack = []; + const stack1 = []; + const stack2 = []; + const stack = []; - let cur1 = l1; - let cur2 = l2; - let curried = 0; + let cur1 = l1; + let cur2 = l2; + let curried = 0; - while(cur1) { - stack1.push(cur1.val); - cur1 = cur1.next; - } + while (cur1) { + stack1.push(cur1.val); + cur1 = cur1.next; + } - while(cur2) { - stack2.push(cur2.val); - cur2 = cur2.next; - } + while (cur2) { + stack2.push(cur2.val); + cur2 = cur2.next; + } - let a = null; - let b = null; + let a = null; + let b = null; - while(stack1.length > 0 || stack2.length > 0) { - a = Number(stack1.pop()) || 0; - b = Number(stack2.pop()) || 0; + while (stack1.length > 0 || stack2.length > 0) { + a = Number(stack1.pop()) || 0; + b = Number(stack2.pop()) || 0; - stack.push((a + b + curried) % 10); + stack.push((a + b + curried) % 10); - if (a + b + curried >= 10) { - curried = 1; - } else { - curried = 0; - } + if (a + b + curried >= 10) { + curried = 1; + } else { + curried = 0; } + } - if (curried === 1) { - stack.push(1); - } + if (curried === 1) { + stack.push(1); + } - const dummy = {}; + const dummy = {}; - let current = dummy; + let current = dummy; - while(stack.length > 0) { - current.next = { - val: stack.pop(), - next: null - } + while (stack.length > 0) { + current.next = { + val: stack.pop(), + next: null + }; - current = current.next - } + current = current.next; + } - return dummy.next; + return dummy.next; }; - ``` + C++ Code: + ```C++ /** * Definition for singly-linked list. @@ -211,7 +186,7 @@ private: } return prev; } - + ListNode* add(ListNode* l1, ListNode* l2) { ListNode* ret = nullptr; ListNode* cur = nullptr;