Leetcode 0098. Validate Binary Search Tree
98. Validate Binary Search TreeGiven the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys strictly less than the node's key. The right subtree of a node contains only nodes with keys strictly greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: 12Input: root = [2,1,3]Output: true Example 2: 123Input: root =...
Python全局变量:作用域与global关键字
在Python编程中,全局变量和局部变量的作用域是一个重要的概念。本文将详细介绍Python中全局变量的使用,以及如何通过global关键字在函数内部修改全局变量。 一、全局变量和局部变量1. 基本概念123456789101112# 全局变量global_var = 10def func(): # 局部变量 local_var = 20 print(f"Inside function: global_var = {global_var}") print(f"Inside function: local_var = {local_var}")func()print(f"Outside function: global_var = {global_var}")# print(local_var) # NameError: name 'local_var' is not defined 2....
Leetcode 0096. Unique Binary Search Trees
96. Unique Binary Search TreesGiven an integer n, return *the number of structurally unique **BST'*s (binary search trees) which has exactly n nodes of unique values from 1 to n. Example 1: 12Input: n = 3Output: 5 Example 2: 12Input: n = 1Output: 1 题目大意给定一个整数 n,计算由 1 到 n 为节点所组成的结构独特的二叉搜索树(BST)的数量。 例如: 输入 n = 3,存在 5 种结构独特的 BST,返回 5; 输入 n = 1,只有 1 种 BST,返回 1。 解题思路计算独特 BST 的数量可以通过动态规划(DP) 实现,核心思路基于以下观察: 若选择 i 作为根节点,则左子树由 1~i-1 构成,右子树由 i+1~n 构成; 左子树的独特结构数量为 dp[i-1],右子树的独特结构数量为...
Leetcode 0095. Unique Binary Search Trees II
95. Unique Binary Search Trees IIGiven an integer n, return *all the structurally unique **BST'*s (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order. Example 1: 12Input: n = 3Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]] Example 2: 12Input: n = 1Output: [[1]] 题目大意给定一个整数 n,生成所有由 1 到 n 为节点所组成的结构独特的二叉搜索树(BST)。返回这些二叉搜索树的根节点列表。 二叉搜索树的特性是:对于任意节点,其左子树中的所有节点值都小于该节点值,右子树中的所有节点值都大于该节点值。 例如: 输入 n =...
Leetcode 0094. Binary Tree Inorder Traversal
94. Binary Tree Inorder TraversalGiven the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,2,6,5,7,1,3,9,8] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] 题目大意 给定一棵二叉树的根节点 root,返回其节点值的中序遍历结果。中序遍历的顺序是「左子树 → 根节点 → 右子树」,遵循 “左 - 根 - 右”...
Leetcode 0093. Restore IP Addresses
93. Restore IP AddressesA valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros. For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses. Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots...
C++ virtual 继承机制详解(非多态场景)
一、virtual 继承的核心作用在C++中,virtual关键字用于解决多重继承时的菱形继承问题。当多个派生类共享同一基类时,如果不使用虚继承,会导致基类对象被多次实例化,造成内存浪费和指针混淆。 1.1 问题场景考虑经典菱形继承结构: 12345678910111213141516class A {public: int a;};class B : virtual public A {public: int b;};class C : virtual public A {public: int c;};class D : public B, public C {}; 在这种情况下,D对象会包含两个A子对象(一个来自B,一个来自C),导致: 内存重复占用(每个A子对象占用相同内存空间) 指针访问歧义(需要明确使用B::a或C::a) 1.2...
Leetcode 0090. Subsets II
90. Subsets IIGiven an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: 12Input: nums = [1,2,2]Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] Example 2: 12Input: nums = [0]Output: [[],[0]] 题目大意给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(即幂集)。幂集需包含所有可能的子集(包括空集和数组本身),且不能有重复子集,结果顺序可任意。 例如: 输入 nums = [1,2,2],输出 [[],[1],[1,2],[1,2,2],[2],[2,2]](共 6 个子集,无重复); 输入 nums...
Leetcode 0086. Partition List
86. Partition ListGiven the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example 1: 12Input: head = [1,4,3,2,5,2], x = 3Output: [1,2,2,4,3,5] Example 2: 12Input: head = [2,1], x = 2Output: [1,2] 题目大意给定链表的头节点 head 和一个值 x,要求将链表分隔成两部分:所有值小于 x 的节点排在所有值大于或等于 x...
Leetcode 0084. 柱状图中最大的矩形
84. 柱状图中最大的矩形给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。 求在该柱状图中,能够勾勒出来的矩形的最大面积。 示例 1: 123输入:heights = [2,1,5,6,2,3]输出:10解释:最大的矩形为图中红色区域,面积为 10 示例 2: 12输入: heights = [2,4]输出: 4 解题思路该解法是对单调栈思路的优化实现,通过在数组末尾添加哨兵元素和栈初始化技巧,将左右边界的计算合并到一次遍历中,代码更简洁且效率更高。 核心优化思路 哨兵元素(Sentinel): 在原数组末尾添加 -1(高度为负数的哨兵),确保遍历结束时栈中所有元素都会被弹出计算(相当于 “大火收汁”); 栈初始时推入 -1(索引哨兵),解决栈空时的边界判断问题,同时自然对应 “左侧无更矮柱子” 的情况(left[i] = -1)。 一次遍历计算左右边界: 遍历每个元素作为右侧边界(right),当当前高度小于栈顶高度时,栈顶元素的左右边界均已确定: 右边界:当前索引...
Python函数副作用:返回值与状态变更
在Python编程中,理解函数副作用(Side Effects)是非常重要的。副作用是指函数在执行过程中,除了返回值之外,对外部状态产生的任何改变。理解副作用有助于编写更清晰、更安全的代码。 一、什么是函数副作用1. 基本定义副作用包括但不限于: 修改全局变量 修改传入的参数 输入/输出操作(打印、读取文件、网络通信等) 修改数据结构 抛出异常 2. 无副作用函数示例1234567# 无副作用:纯函数def add(a, b): return a + bresult = add(3, 5)print(result) # 输出:8# 函数外部没有任何改变 3. 有副作用函数示例12345678910# 有副作用:修改全局变量counter = 0def increment(): global counter counter += 1 return counterprint(increment()) # 输出:1print(counter) # 输出:1(全局变量被修改) 二、常见的副作用场景1....
Leetcode 0082. Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List IIGiven the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Example 1: 12Input: head = [1,2,3,3,4,4,5]Output: [1,2,5] Example 2: 12Input: head = [1,1,1,2,3]Output: [2,3] 题目大意给定一个已排序的链表,要求删除所有存在重复的节点(即重复的节点一个都不保留),仅保留原链表中只出现过一次的节点,最终返回排序后的新链表头节点。 核心解题思路由于链表已排序,重复节点必然相邻,因此可通过「遍历链表 + 跳过重复节点」的思路解决,关键是: 虚拟头节点:避免删除头节点时的特殊处理(如示例 2 中头节点...
Leetcode 0078. Subsets
78. SubsetsGiven an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: 12Input: nums = [1,2,3]Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Example 2: 12Input: nums = [0]Output: [[],[0]] 题目大意给定一个无重复元素的整数数组 nums,返回该数组所有可能的子集(即幂集)。幂集需包含所有可能的子集(包括空集和数组本身),且不能有重复子集,结果顺序可任意。 例如: 输入 nums = [1,2,3],输出 [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]](共 2³=8...
Leetcode 0077. Combinations
77. CombinationsGiven two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. You may return the answer in any order. Example 1: 1234Input: n = 4, k = 2Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]Explanation: There are 4 choose 2 = 6 total combinations.Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination. Example 2: 123Input: n = 1, k = 1Output: [[1]]Explanation: There is 1 choose 1 = 1 total...
Leetcode 0076. Minimum Window Substring
76. Minimum Window Substring题目Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). Example: 12Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC" Note: If there is no such window in S that covers all characters in T, return the empty string "". If there is such window, you are guaranteed that there will always be only one unique minimum window in S. 题目大意给定一个源字符串 s,再给一个字符串...
C++ 关联容器(map 与 set)解析
一、关联容器的底层数据结构特性C++ 标准库中的map和set均属于关联容器,其底层实现基于红黑树(Red-Black Tree)—— 一种自平衡的二叉搜索树。这种数据结构具有以下核心特性: 有序性:元素按照键值的比较规则进行排序存储 自平衡性:通过特定的旋转和着色操作,保证树的高度始终保持在 O (log n) 级别 迭代效率:支持高效的顺序访问和范围查询 红黑树的平衡机制确保了所有基本操作(插入、删除、查找)都能在O(log n) 的时间复杂度内完成,这使得关联容器在需要频繁查找和有序遍历的场景中表现优异。 二、map 与 set 的存储机制差异虽然map和set共享相同的底层实现机制,但它们的存储方式存在本质区别: 特性 set map 存储内容 仅存储键(key) 存储键值对(key-value) 元素唯一性 键值唯一,不允许重复 键值唯一,不允许重复 访问方式 只能通过键访问元素 可通过键访问对应的值 模板参数 std::set<Key, Compare, Allocator> std::map<Key,...
Leetcode 0074. 搜索二维矩阵
74. 搜索二维矩阵给你一个满足下述两条属性的 m x n 整数矩阵: 每行中的整数从左到右按非严格递增顺序排列。 每行的第一个整数大于前一行的最后一个整数。 给你一个整数 target ,如果 target 在矩阵中,返回 true ;否则,返回 false 。 示例 1: 12输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3输出:true 示例 2: 12输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13输出:false 解法1:二分查找由于矩阵具有特殊的有序性,可以将其视为一个有序的一维数组来处理: 整个矩阵可以看作是按行拼接而成的有序数组 使用二分查找高效定位目标值 通过计算将一维索引转换为二维矩阵的行和列 1234567891011121314151617181920212223242526272829303132class Solution {public: bool...
Leetcode 0073. Set Matrix Zeroes
73. Set Matrix Zeroes题目Given an *m* x *n* matrix. If an element is 0, set its entire row and column to 0. Do it in-place. Follow up: A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution? Example 1: 12Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]Output: [[1,0,1],[0,0,0],[1,0,1]] Example 2: 12Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]Output:...
Leetcode 0072. Edit Distance
72. Edit DistanceGiven two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three operations permitted on a word: Insert a character Delete a character Replace a character Example 1: 123456Input: word1 = "horse", word2 = "ros"Output: 3Explanation: horse -> rorse (replace 'h' with 'r')rorse -> rose (remove 'r')rose -> ros (remove 'e') Example...
Python函数定义:def关键字与缩进规则
Python的函数定义使用def关键字,与C++等语言不同,Python不使用大括号来标记函数体,而是依靠缩进来区分代码块。本文将详细介绍Python函数定义的方式和特点。 一、Python函数定义的基本语法1. 基本结构123456# Python函数定义def greet(): print("Hello, World!")# 调用函数greet() # 输出:Hello, World! 2. 带参数的函数123456789101112# 带参数的函数def greet(name): print(f"Hello, {name}!")greet("Alice") # 输出:Hello, Alice!# 多个参数def add(a, b): return a + bresult = add(3, 5)print(result) # 输出:8 3. 默认参数值123456789# 默认参数def greet(name, greeting="Hello"):...

