From bec57550df7a01fccb7fe060ce569e499285a134 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Thu, 10 Nov 2022 17:27:45 -0700 Subject: [PATCH 1/7] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python动态规划优化版 --- problems/0376.摆动序列.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 214ff311..f9d3f97f 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -264,6 +264,25 @@ class Solution: return max(dp[-1][0], dp[-1][1]) ``` +```python +class Solution: + def wiggleMaxLength(self, nums: List[int]) -> int: + # up i作为波峰最长的序列长度 + # down i作为波谷最长的序列长度 + n = len(nums) + # 长度为0和1的直接返回长度 + if n<2: return n + for i in range(1,n): + if nums[i]>nums[i-1]: + # nums[i] 为波峰,1. 前面是波峰,up值不变,2. 前面是波谷,down值加1 + # 目前up值取两者的较大值(其实down+1即可,可以推理前一步down和up最多相差1,所以down+1>=up) + up = max(up, down+1) + elif nums[i] Date: Mon, 14 Nov 2022 15:00:07 -0700 Subject: [PATCH 2/7] =?UTF-8?q?Update=200121.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了python 动态规划:版本三 --- problems/0121.买卖股票的最佳时机.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 63ac5d04..cf17f48d 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -310,6 +310,18 @@ class Solution: return dp[(length-1) % 2][1] ``` +> 动态规划:版本三 +```python +class Solution: + def maxProfit(self, prices: List[int]) -> int: + length = len(prices) + dp0, dp1 = -prices[0], 0 #注意这里只维护两个常量,因为dp0的更新不受dp1的影响 + for i in range(1, length): + dp1 = max(dp1, dp0 + prices[i]) + dp0 = max(dp0, -prices[i]) + return dp1 +``` + Go: > 贪心法: ```Go From 4261d96a144c91ba577a1a2c3bdad5fb7ce38696 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Tue, 15 Nov 2022 10:27:03 -0700 Subject: [PATCH 3/7] =?UTF-8?q?Update=200714.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E6=89=8B=E7=BB=AD=E8=B4=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新Python贪心,更容易理解 --- .../0714.买卖股票的最佳时机含手续费.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index 66fb9fb6..faa56d42 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -206,13 +206,13 @@ class Solution: # 贪心思路 result = 0 minPrice = prices[0] for i in range(1, len(prices)): - if prices[i] < minPrice: + if prices[i] < minPrice: # 此时有更低的价格,可以买入 minPrice = prices[i] - elif prices[i] >= minPrice and prices[i] <= minPrice + fee: - continue - else: - result += prices[i] - minPrice - fee + elif prices[i] > (minPrice + fee): # 此时有利润,同时假买入高价的股票,看看是否继续盈利 + result += prices[i] - (minPrice + fee) minPrice = prices[i] - fee + else: # minPrice<= prices[i] <= minPrice + fee, 价格处于minPrice和minPrice+fee之间,不做操作 + continue return result ``` From 6fb61ead0b5d723775958827e4d75a7199aecfe6 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:01:24 -0700 Subject: [PATCH 4/7] =?UTF-8?q?Update=200055.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Python for循环版 --- problems/0055.跳跃游戏.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index 23357f21..0ed03b07 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -119,6 +119,18 @@ class Solution: return False ``` +```python +## for循环 +class Solution: + def canJump(self, nums: List[int]) -> bool: + cover = 0 + if len(nums) == 1: return True + for i in range(len(nums)): + cover = max(i + nums[i], cover) + if cover >= len(nums) - 1: return True + return False +``` + ### Go ```Go func canJUmp(nums []int) bool { From c0f1f13669a479f6ec8d0bfe6a0b1156213363d5 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:05:39 -0700 Subject: [PATCH 5/7] =?UTF-8?q?Update=200055.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python for循环版,添加了更新cover的条件 --- problems/0055.跳跃游戏.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index 0ed03b07..80c35c03 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -126,8 +126,9 @@ class Solution: cover = 0 if len(nums) == 1: return True for i in range(len(nums)): - cover = max(i + nums[i], cover) - if cover >= len(nums) - 1: return True + if i <= cover: + cover = max(i + nums[i], cover) + if cover >= len(nums) - 1: return True return False ``` From af0e45b9af2171db2a2b5ef005745463a9d739e3 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Wed, 23 Nov 2022 23:34:26 +0800 Subject: [PATCH 6/7] =?UTF-8?q?update=200707.=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=20python=E4=BB=A3=E7=A0=81=EF=BC=8Cjava=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 116 +++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 51 deletions(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 9461e263..f0c4bc60 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -305,7 +305,7 @@ class MyLinkedList { head = new ListNode(0); } - //获取第index个节点的数值 + //获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点 public int get(int index) { //如果index非法,返回-1 if (index < 0 || index >= size) { @@ -319,12 +319,12 @@ class MyLinkedList { return currentNode.val; } - //在链表最前面插入一个节点 + //在链表最前面插入一个节点,等价于在第0个元素前添加 public void addAtHead(int val) { addAtIndex(0, val); } - //在链表的最后插入一个节点 + //在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加 public void addAtTail(int val) { addAtIndex(size, val); } @@ -481,76 +481,90 @@ class MyLinkedList { Python: ```python # 单链表 -class Node: - - def __init__(self, val): - self.val = val +class Node(object): + def __init__(self, x=0): + self.val = x self.next = None - -class MyLinkedList: +class MyLinkedList(object): def __init__(self): - self._head = Node(0) # 虚拟头部节点 - self._count = 0 # 添加的节点数 + self.head = Node() + self.size = 0 # 设置一个链表长度的属性,便于后续操作,注意每次增和删的时候都要更新 - def get(self, index: int) -> int: + def get(self, index): """ - Get the value of the index-th node in the linked list. If the index is invalid, return -1. + :type index: int + :rtype: int """ - if 0 <= index < self._count: - node = self._head - for _ in range(index + 1): - node = node.next - return node.val - else: + if index < 0 or index >= self.size: return -1 + cur = self.head.next + while(index): + cur = cur.next + index -= 1 + return cur.val - def addAtHead(self, val: int) -> None: + def addAtHead(self, val): """ - Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. + :type val: int + :rtype: None """ - self.addAtIndex(0, val) + new_node = Node(val) + new_node.next = self.head.next + self.head.next = new_node + self.size += 1 - def addAtTail(self, val: int) -> None: + def addAtTail(self, val): """ - Append a node of value val to the last element of the linked list. + :type val: int + :rtype: None """ - self.addAtIndex(self._count, val) + new_node = Node(val) + cur = self.head + while(cur.next): + cur = cur.next + cur.next = new_node + self.size += 1 - def addAtIndex(self, index: int, val: int) -> None: + def addAtIndex(self, index, val): """ - Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. + :type index: int + :type val: int + :rtype: None """ if index < 0: - index = 0 - elif index > self._count: + self.addAtHead(val) + return + elif index == self.size: + self.addAtTail(val) + return + elif index > self.size: return - # 计数累加 - self._count += 1 - - add_node = Node(val) - prev_node, current_node = None, self._head - for _ in range(index + 1): - prev_node, current_node = current_node, current_node.next - else: - prev_node.next, add_node.next = add_node, current_node - - def deleteAtIndex(self, index: int) -> None: + node = Node(val) + pre = self.head + while(index): + pre = pre.next + index -= 1 + node.next = pre.next + pre.next = node + self.size += 1 + + def deleteAtIndex(self, index): """ - Delete the index-th node in the linked list, if the index is valid. + :type index: int + :rtype: None """ - if 0 <= index < self._count: - # 计数-1 - self._count -= 1 - prev_node, current_node = None, self._head - for _ in range(index + 1): - prev_node, current_node = current_node, current_node.next - else: - prev_node.next, current_node.next = current_node.next, None - - + if index < 0 or index >= self.size: + return + pre = self.head + while(index): + pre = pre.next + index -= 1 + pre.next = pre.next.next + self.size -= 1 + # 双链表 # 相对于单链表, Node新增了prev属性 class Node: From d9950cee1bb10cb826cd1522a951cbf9ceccba81 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 24 Nov 2022 00:15:31 +0800 Subject: [PATCH 7/7] =?UTF-8?q?update=20=20=E9=9D=A2=E8=AF=95=E9=A2=9802.0?= =?UTF-8?q?7.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4=20=20python=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=20js=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/面试题02.07.链表相交.md | 55 ++++++++++++++----------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 3277e85e..30f5c467 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -155,23 +155,28 @@ public class Solution { class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: - """ - 根据快慢法则,走的快的一定会追上走得慢的。 - 在这道题里,有的链表短,他走完了就去走另一条链表,我们可以理解为走的快的指针。 - - 那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个 - 位置相遇 - """ - if headA is None or headB is None: - return None - cur_a, cur_b = headA, headB # 用两个指针代替a和b - - - while cur_a != cur_b: - cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走 - cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a - - return cur_a + lenA, lenB = 0, 0 + cur = headA + while cur: # 求链表A的长度 + cur = cur.next + lenA += 1 + cur = headB + while cur: # 求链表B的长度 + cur = cur.next + lenB += 1 + curA, curB = headA, headB + if lenA > lenB: # 让curB为最长链表的头,lenB为其长度 + curA, curB = curB, curA + lenA, lenB = lenB, lenA + for _ in range(lenB - lenA): # 让curA和curB在同一起点上(末尾位置对齐) + curB = curB.next + while curA: # 遍历curA 和 curB,遇到相同则直接返回 + if curA == curB: + return curA + else: + curA = curA.next + curB = curB.next + return None ``` ### Go @@ -248,19 +253,21 @@ var getListLen = function(head) { } var getIntersectionNode = function(headA, headB) { let curA = headA,curB = headB, - lenA = getListLen(headA), - lenB = getListLen(headB); - if(lenA < lenB) { - // 下面交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时 + lenA = getListLen(headA), // 求链表A的长度 + lenB = getListLen(headB); + if(lenA < lenB) { // 让curA为最长链表的头,lenA为其长度 + + // 交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时 // 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA] + [curA, curB] = [curB, curA]; [lenA, lenB] = [lenB, lenA]; } - let i = lenA - lenB; - while(i-- > 0) { + let i = lenA - lenB; // 求长度差 + while(i-- > 0) { // 让curA和curB在同一起点上(末尾位置对齐) curA = curA.next; } - while(curA && curA !== curB) { + while(curA && curA !== curB) { // 遍历curA 和 curB,遇到相同则直接返回 curA = curA.next; curB = curB.next; }