Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-hash

This commit is contained in:
Yuhao Ju
2022-11-24 22:14:50 +08:00
committed by GitHub
6 changed files with 145 additions and 80 deletions

View File

@ -119,6 +119,19 @@ class Solution:
return False 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)):
if i <= cover:
cover = max(i + nums[i], cover)
if cover >= len(nums) - 1: return True
return False
```
### Go ### Go
```Go ```Go
func canJUmp(nums []int) bool { func canJUmp(nums []int) bool {

View File

@ -310,6 +310,18 @@ class Solution:
return dp[(length-1) % 2][1] 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:
> 贪心法: > 贪心法:
```Go ```Go

View File

@ -264,6 +264,25 @@ class Solution:
return max(dp[-1][0], dp[-1][1]) 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]<nums[i-1]:
# nums[i] 为波谷1. 前面是波峰up+12. 前面是波谷down不变取较大值
down = max(down, up+1)
return max(up, down)
```
### Go ### Go
**贪心** **贪心**

View File

@ -305,7 +305,7 @@ class MyLinkedList {
head = new ListNode(0); head = new ListNode(0);
} }
//获取第index个节点的数值 //获取第index个节点的数值注意index是从0开始的第0个节点就是头结点
public int get(int index) { public int get(int index) {
//如果index非法返回-1 //如果index非法返回-1
if (index < 0 || index >= size) { if (index < 0 || index >= size) {
@ -319,12 +319,12 @@ class MyLinkedList {
return currentNode.val; return currentNode.val;
} }
//在链表最前面插入一个节点 //在链表最前面插入一个节点等价于在第0个元素前添加
public void addAtHead(int val) { public void addAtHead(int val) {
addAtIndex(0, val); addAtIndex(0, val);
} }
//在链表的最后插入一个节点 //在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
public void addAtTail(int val) { public void addAtTail(int val) {
addAtIndex(size, val); addAtIndex(size, val);
} }
@ -481,75 +481,89 @@ class MyLinkedList {
Python Python
```python ```python
# 单链表 # 单链表
class Node: class Node(object):
def __init__(self, x=0):
def __init__(self, val): self.val = x
self.val = val
self.next = None self.next = None
class MyLinkedList(object):
class MyLinkedList:
def __init__(self): def __init__(self):
self._head = Node(0) # 虚拟头部节点 self.head = Node()
self._count = 0 # 添加的节点数 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: if index < 0 or index >= self.size:
node = self._head
for _ in range(index + 1):
node = node.next
return node.val
else:
return -1 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: if index < 0:
index = 0 self.addAtHead(val)
elif index > self._count: return
elif index == self.size:
self.addAtTail(val)
return
elif index > self.size:
return return
# 计数累加 node = Node(val)
self._count += 1 pre = self.head
while(index):
pre = pre.next
index -= 1
node.next = pre.next
pre.next = node
self.size += 1
add_node = Node(val) def deleteAtIndex(self, index):
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:
""" """
Delete the index-th node in the linked list, if the index is valid. :type index: int
:rtype: None
""" """
if 0 <= index < self._count: if index < 0 or index >= self.size:
# 计数-1 return
self._count -= 1 pre = self.head
prev_node, current_node = None, self._head while(index):
for _ in range(index + 1): pre = pre.next
prev_node, current_node = current_node, current_node.next index -= 1
else: pre.next = pre.next.next
prev_node.next, current_node.next = current_node.next, None self.size -= 1
# 双链表 # 双链表
# 相对于单链表, Node新增了prev属性 # 相对于单链表, Node新增了prev属性

View File

@ -206,13 +206,13 @@ class Solution: # 贪心思路
result = 0 result = 0
minPrice = prices[0] minPrice = prices[0]
for i in range(1, len(prices)): for i in range(1, len(prices)):
if prices[i] < minPrice: if prices[i] < minPrice: # 此时有更低的价格,可以买入
minPrice = prices[i] minPrice = prices[i]
elif prices[i] >= minPrice and prices[i] <= minPrice + fee: elif prices[i] > (minPrice + fee): # 此时有利润,同时假买入高价的股票,看看是否继续盈利
continue result += prices[i] - (minPrice + fee)
else:
result += prices[i] - minPrice - fee
minPrice = prices[i] - fee minPrice = prices[i] - fee
else: # minPrice<= prices[i] <= minPrice + fee 价格处于minPrice和minPrice+fee之间不做操作
continue
return result return result
``` ```

View File

@ -155,23 +155,28 @@ public class Solution {
class Solution: class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
""" lenA, lenB = 0, 0
根据快慢法则,走的快的一定会追上走得慢的。 cur = headA
在这道题里,有的链表短,他走完了就去走另一条链表,我们可以理解为走的快的指针。 while cur: # 求链表A的长度
cur = cur.next
那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个 lenA += 1
位置相遇 cur = headB
""" while cur: # 求链表B的长度
if headA is None or headB is None: 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 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
``` ```
### Go ### Go
@ -248,19 +253,21 @@ var getListLen = function(head) {
} }
var getIntersectionNode = function(headA, headB) { var getIntersectionNode = function(headA, headB) {
let curA = headA,curB = headB, let curA = headA,curB = headB,
lenA = getListLen(headA), lenA = getListLen(headA), // 求链表A的长度
lenB = getListLen(headB); lenB = getListLen(headB);
if(lenA < lenB) { if(lenA < lenB) { // 让curA为最长链表的头lenA为其长度
// 下面交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时
// 交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时
// 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA] // 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA]
[curA, curB] = [curB, curA]; [curA, curB] = [curB, curA];
[lenA, lenB] = [lenB, lenA]; [lenA, lenB] = [lenB, lenA];
} }
let i = lenA - lenB; let i = lenA - lenB; // 求长度差
while(i-- > 0) { while(i-- > 0) { // 让curA和curB在同一起点上末尾位置对齐
curA = curA.next; curA = curA.next;
} }
while(curA && curA !== curB) { while(curA && curA !== curB) { // 遍历curA 和 curB遇到相同则直接返回
curA = curA.next; curA = curA.next;
curB = curB.next; curB = curB.next;
} }