python--剑指offer--中等--60. n个骰子的点数-爱代码爱编程
from typing import List class Solution: def dicesProbability(self, n: int) -> List[float]: end = 6 * n + 1 dp = [[0] * 67 for i in range(12)] # 表示i个骰子掷
代码编织梦想
from typing import List class Solution: def dicesProbability(self, n: int) -> List[float]: end = 6 * n + 1 dp = [[0] * 67 for i in range(12)] # 表示i个骰子掷
class Solution: def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: if not root or root == p or root == q: return root l
class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: def lowestCommonAncestor(self, root: 'Tr
class Solution: def strToInt(self, str: str) -> int: str = str.strip() # 删除首尾空格 if not str: return 0 #
from typing import List class Solution: def constructArr(self, a: List[int]) -> List[int]: b, temp = [1] * len(a), 1 for i in range(1, len(a)):
class Solution: def add(self, a: int, b: int) -> int: x = 0xffffffff a, b = a & x, b & x # python中将python存储补码的形式转换为32位有符号数的补码形式,用另一个数字表示出来
class Solution: def sumNums(self, n: int) -> int: n = n and n + self.sumNums(n - 1) return n if __name__ == '__main__': solution = Solution() re
from typing import List class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) <= 1: return 0 min_ = prices[0]
from typing import List class Solution: def isStraight(self, nums: List[int]) -> bool: ma, mi = 0, 14 repeat = set() for num in nums:
from collections import deque class MaxQueue: def __init__(self): self.res = deque() self.tmp = deque() def max_value(self) -> int: retu
from typing import List class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: deque = collections.deque() res, n = [], len(nu
代码 class Solution: def reverseLeftWords(self, s: str, n: int) -> str: return s[n:] + s[:n] 代码 class Solution: def reverseLeftWords(self, s: str, n: int)
代码 class Solution: def reverseWords(self, s: str) -> str: i = j = len(s) - 1 res = [] while i >= 0: while i >= 0 and s[i] != ' '
// java代码 class Solution { public int[][] findContinuousSequence(int target) { List<int[]> vec = new ArrayList<int[]>(); int sum = 0, limit = (
from typing import List class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: length = len(nums) if length == 1: retu
from typing import List class Solution: def singleNumber(self, nums: List[int]) -> int: ones = twos = 0 for num in nums: ones = ones ^ num &
from typing import List class Solution: def singleNumbers(self, nums: List[int]) -> List[int]: from functools import reduce res = reduce(lambda x, y: x
后序遍历 + 剪枝 (从底至顶) 此方法为本题的最优解法,但剪枝的方法不易第一时间想到。 思路是对二叉树做后序遍历,从底至顶返回子树深度,若判定某子树不是平衡树则 “剪枝” ,直接向上返回。 复杂度分析: 时间复杂度 O(N)O(N): NN 为树的节点数;最差情况下,需要递归遍历树的所有节点。 空间复杂度 O(N)O(N): 最差情况下(树退化为链
方法一:后序遍历(DFS) 复杂度分析: 时间复杂度 O(N): N 为树的节点数量,计算树的深度需要遍历所有节点。空间复杂度 O(N): 最差情况下(当树退化为链表时),递归深度可达到 N 。class TreeNode: def __init__(self, x): self.val = x self.le
代码 class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution: def kthLargest(self, root: TreeNode, k