109 lines
3.9 KiB
Python
109 lines
3.9 KiB
Python
import functools
|
|
from typing import List
|
|
|
|
|
|
class TreeNode:
|
|
def __init__(self, val=0, left=None, right=None):
|
|
self.val = val
|
|
self.left = left
|
|
self.right = right
|
|
|
|
|
|
class Solution:
|
|
def xorOperation(self, n: int, start: int) -> int:
|
|
# from functools import reduce
|
|
# return reduce(lambda a, b: a ^ b, map(lambda i: start + (i << 1), range(n)))
|
|
|
|
if (start & 3) < 2:
|
|
if (n & 1) == 0:
|
|
return n & 3
|
|
else:
|
|
return start + 2 * n - 3 + (n & 3)
|
|
else:
|
|
if (n & 1) == 0:
|
|
return (start + (n - 1) * 2) ^ (start - 2 + (n & 3))
|
|
else:
|
|
return start + 1 - (n & 3)
|
|
|
|
def deleteAndEarn(self, nums: List[int]) -> int:
|
|
S = [0] * (max(nums) + 1)
|
|
for v in nums:
|
|
S[v] += v
|
|
f, s = S[0], max(S[0], S[1])
|
|
for x in S[2::]:
|
|
f, s = s, max(f + x)
|
|
return s
|
|
|
|
def rob(self, nums: List[int]) -> int:
|
|
from functools import reduce
|
|
return reduce(lambda t, x: (t[1], max(t[1], t[0] + x)), nums, (0, 0))[1]
|
|
|
|
def rob2(self, nums: List[int]) -> int:
|
|
from functools import reduce
|
|
return max(max(reduce(lambda t, x: (t[1], max(t[1], t[0] + x)), nums[:-1], (0, 0))[1], reduce(lambda t, x: (t[1], max(t[1], t[0] + x)), nums[1::], (0, 0))[1]), nums[0])
|
|
|
|
def missingNumber(self, nums: List[int]) -> int:
|
|
return [len(nums), 1, len(nums) + 1, 0][len(nums) & 3] ^ functools.reduce(lambda o, n: o ^ n, nums, 0)
|
|
|
|
def mySqrt(self, x: int) -> int:
|
|
"""
|
|
# 69.Sqrt(x)
|
|
:param x: x
|
|
:return: 平方根
|
|
"""
|
|
import struct
|
|
x_half = 0.5 * x
|
|
i = struct.unpack("Q", struct.pack("d", float(x)))[0]
|
|
i = 0x1FF7A3BEA91D9B1B + (i >> 1)
|
|
f = struct.unpack("d", struct.pack("Q", i))[0]
|
|
f = f * 0.5 + x_half / f
|
|
f = f * 0.5 + x_half / f
|
|
f = f * 0.5 + x_half / f
|
|
return int(f)
|
|
|
|
def isSymmetric(self, root: TreeNode) -> bool:
|
|
"""
|
|
101.对称二叉树
|
|
:param root: 二叉树
|
|
:return: 是否是对称二叉树
|
|
"""
|
|
|
|
"""def check(l: TreeNode, r: TreeNode) -> bool:
|
|
return (l is None and r is None) or (l is not None and r is not None) and (l.val == r.val) and check(l.left, r.right) and check(l.right, r.left)
|
|
|
|
return root is None or check(root.left, root.right)"""
|
|
"""if root is not None:
|
|
q = [(root.left, root.right)]
|
|
while len(q) > 0:
|
|
l, r = q.pop()
|
|
if l is None and r is None:
|
|
continue
|
|
if l is None or r is None or l.val != r.val:
|
|
return False
|
|
q.append((l.left, r.right))
|
|
q.append((l.right, r.left))
|
|
return True"""
|
|
return root is None or \
|
|
(root.left is None and root.right is None) or \
|
|
not (root.left is None or root.right is None or root.left.val != root.right.val) and \
|
|
self.isSymmetric(TreeNode(0, root.left.left, root.right.right)) and \
|
|
self.isSymmetric(TreeNode(0, root.left.right, root.right.left))
|
|
|
|
def solveEquation(self, equation: str) -> str:
|
|
"""
|
|
640. 求解方程
|
|
:param equation: 方程式
|
|
:return: 解
|
|
"""
|
|
a, b, c, sign, left, has_c = 0, 0, 0, 1, 1, False
|
|
for z in equation:
|
|
match z:
|
|
case 'x': a, c, has_c = a + (sign * left * c if has_c else sign * left), 0, False
|
|
case '+': b, c, sign, has_c = b + sign * left * c, 0, 1, False
|
|
case '-': b, c, sign, has_c = b + sign * left * c, 0, -1, False
|
|
case '=': b, c, sign, left, has_c = b + sign * left * c, 0, 1, -1, False
|
|
case _: c, has_c = c * 10 + int(z), True
|
|
b += sign * left * c
|
|
return f"x={-b // a}" if a != 0 else "Infinite solutions" if b == 0 else "Infinite solutions"
|
|
|