237.删除链表中的节点
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, node):
node.val=node.next.val
node.next=node.next.next
238.除自身外数组乘积
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
products = [1]
for i in range(1,len(nums)):
products.append(nums[i-1]*products[-1])
# 得到除了最后一个数的累积
# 比如[2,3,4,5]得到[1,2,6,24],每个位置的元素代表原数组中其左边元素的累积。
# 可以看到最后一个数已经算出来了
# 再从右到左遍历,每个元素乘以原数组中其右边元素的累积
# 比如[1,2,6,24],24乘1,6乘5, 2乘(4*5),1乘(3*4*5)
right_product = 1
for i in range(len(nums)-1,-1,-1):
products[i] *= right_product
right_product *= nums[i]
return products
292.nim游戏
class Solution(object):
def canWinNim(self, n):
return not n%4==0