楠君的小窝
LC501. 二叉搜索树中的众数
题目 给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。 如果树中有不止一个众数,可以按 任意顺序 返回。 假定 BST 满足如下定义: 结点左子树中所含节点的值 小于等于 当前节点的值 结点右子树中所含节点的值 大于等于 当前节点的值 左子树和右子树都是二叉搜索树 解题思路 其实这题解题思路很简单,就是遍历二叉树,将遍历到的值在哈希表中计数,最后输出众数即可。 代码 Python 123456789101112131415161718class Solution: def findMode(self, root: Optional[TreeNode]) -> List[int]: cnt = defaultdict(int) def dfs(root): if root is None: return cnt[root.val] += 1 dfs(root.left) dfs(roo ...
LC1802. Maximum Value at a Given Index in a Bounded Array
题目 You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions: nums.length == n nums[i] is a positive integer where 0 <= i < n. abs(nums[i] - nums[i+1]) <= 1 where 0 <= i < n-1. The sum of all the elements of nums does not exceed maxSum. nums[index] is maximized. Return nums[index] of the constructed array. Note that abs(x) equals x if x >= 0, and -x otherwise. Example 1: Input: n = 4, in ...
LC530. 二叉搜索树的最小绝对差
解题思路 这题其实和LC98.验证二叉搜索树有异曲同工之妙,验证二叉搜索树是判断辅助数组的是否严格递增。 这题是让我们先中序遍历二叉树,然后将val保存进辅助数组nums进行排序,将每个元素与前后元素相减得到的绝对值就是最小绝对差。 代码 Python 12345678910111213141516171819202122# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def getMinimumDifference(self, root: Optional[TreeNode]) -> int: nums = [] def dfs(root): if root is None ...
LC98. 验证二叉搜索树
解题思路 这题使用中序遍历+辅助数组就能解决 中序遍历为(左->中->右)的遍历顺序 因为二叉搜索树的特性,我们使用中序遍历能使数组变得有序且是严格递增的。 只要数组不是严格递增我们就返回false 代码 Go /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func isValidBST(root *TreeNode) bool { nums := make([]int, 0, 0) dfs(root, &nums) for i := 1; i < len(nums); i++ { if nums[i] <= nums[i - 1]{ return false } } return true } ...
LC700. 二叉搜索树中的搜索
解题思路 其实只要知道了二叉搜索树(binary search tree)的性质就会解这道题啦,剩下都是敲代码 若它的左子树不为空,则左子树上所有节点的值均小于它根节点的值 若它的右子树不为空,则右子树上所有节点的值均小于它根节点的值 它的左、右子树也分别为二叉搜索树 代码 Go 递归 123456789101112131415161718/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func searchBST(root *TreeNode, val int) *TreeNode { return binarySearchTree(root, val)}func binarySearchTree(root *TreeNode, val int) *TreeNode { if root == nil || roo ...
LC617. 合并二叉树
解题思路 可以不用重新建一个新的树,我们直接在原树上建立即可 一共有一下三种情况 主有,辅无 主无,辅有 主有,辅有 根据这三种情况可以做出以下三种操作(operations) 直接回溯 主树连接辅树的下一个节点 合并两个节点的值 代码 Python 123456789101112131415161718192021222324# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: def dfs(root1, roo ...
LC1801. 积压订单中的订单总数
题目 给你一个二维整数数组 orders ,其中每个 orders[i] = [pricei, amounti, orderTypei] 表示有 amounti 笔类型为 orderTypei 、价格为 pricei 的订单。 订单类型 orderTypei 可以分为两种: 0 表示这是一批采购订单 buy 1 表示这是一批销售订单 sell 注意,orders[i] 表示一批共计 amounti 笔的独立订单,这些订单的价格和类型相同。对于所有有效的 i ,由 orders[i] 表示的所有订单提交时间均早于 orders[i+1] 表示的所有订单。 存在由未执行订单组成的 积压订单 。积压订单最初是空的。提交订单时,会发生以下情况: 如果该订单是一笔采购订单 buy ,则可以查看积压订单中价格 最低 的销售订单 sell 。如果该销售订单 sell 的价格 低于或等于 当前采购订单 buy 的价格,则匹配并执行这两笔订单,并将销售订单 sell 从积压订单中删除。否则,采购订单 buy 将会添加到积压订单中。 反之亦然,如果该订单是一笔销售订单 sell ,则可以查看积压订单中价 ...
LC257.二叉树的所有路径
解题思路 path用来保存路径 result保存要返回的字符串 使用前序遍历二叉树,将路过的路径保存到path。 代码 Python 123456789101112131415161718192021# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]: result = [] s = "" def dfs(root, s): if root is None: return ...
LC101.平衡二叉树
解题思路 递归解决这题,我们需要计算每个节点下的左右子树的高度,若不符合平衡二叉树则直接返回false。 总共会出现3种情况 当前节点为空,返回0 当前节点的左右子树不为平衡树,返回-1 左右子树高度的绝对值差>1返回-1 返回1 + max(左树,右树) 代码 Python 12345678910111213141516171819# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: if root is None: return True def getDe ...
LC2011.Fival Value of Variable After Performing Operations
题目 There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1. --X and X-- decrements the value of the variable X by 1. Initially, the value of X is 0. Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations. Example 1: Input: operations = [“–X”,“X++”,“X++”] Output: 1 Explanation: The operations are performed as follows: Initially, X = 0. –X: X ...
GCD
题目 给定两个不同的正整数 a,ba,b, 求一个正整数 kk 使得 gcd(a+k,b+k)gcd(a+k,b+k) 尽可能 大, 其中 gcd⁡(a,b)gcd(a,b) 表示 aa 和 bb 的最大公约数, 如果存在多个 kk, 请输出所有满 足条件的 kk 中最小的那个。 输入格式 输入一行包含两个正整数 a,ba,b, 用一个空格分隔。 输出格式 输出一行包含一个正整数 kk 。 样例输入 15 7 样例输出 11 评测用例规模与约定 对于 20%20% 的评测用例, a<b≤105a<b≤105; 对于 40%40% 的评测用例, a<b≤109a<b≤109; 对于所有评测用例, 1≤a<b≤10181≤a<b≤1018 。 解题思路 找规律 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354a,b = (1, 2)gcd = 1K = 1a,b = (1, 3)gcd = 1K = 1a, ...
数论
费马小定理 常用在gcd,最大公因数中 费马小定理(Fermat’s little theorem)是数论中的一个重要定理,在1636年提出。如果p是一个质数,而整数a不是p的倍数,则有a^(p-1)≡1(mod p)。 [1] 向上取整公式 一般语言的math自带ceil,但是我们可以避免浮点数运算,因为浮点数有误差 1ceil(a / b) = (a + b - 1) / b
avatar
🐟认真摸鱼中
楠君的小窝
Live is so good
前往小窝
公告栏
--- 主域名 ---
fomal.cc | fomal.cn
--- 备用域名 ---
netlify.fomal.cc
cloudflare.fomal.cc
--- 网站安卓APP ---
🍧点此下载🍧
小站资讯
文章数目 :
111
本站总字数 :
6.3w
本站访客数 :
本站总访问量 :
最后更新时间 :
空降评论复制本文地址
随便逛逛昼夜切换关于博客美化设置切换全屏打印页面