Leetcode 0142. Linked List Cycle II
142. Linked List Cycle IIGiven the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter. Do not modify the linked list. ...
Leetcode 0131. Palindrome Partitioning
131. Palindrome PartitioningGiven a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example 1: 12Input: s = "aab"Output: [["a","a","b"],["aa","b"]] Example 2: 12Input: s = "a"Output: [["a"]] 题目大意给定一个字符串 s,将其分割成若干个子串,使得每个子串都是回文串。返回所有可能的分割方案。 例如: 输入 s = "aab",输出...
Python条件表达式:链式比较与短路求值
Python支持一种独特的语法特性:条件表达式可以连写。这种链式比较(Chained Comparisons)可以让代码更加简洁和易读。本文将详细介绍Python中条件表达式连写的用法。 一、链式比较的基本用法1. 数学风格比较123456789# 传统写法x = 5if x > 0 and x < 10: print("x在0到10之间")# Python连写写法x = 5if 0 < x < 10: print("x在0到10之间") 2. 更多示例1234567891011121314# 判断是否在某个范围内age = 25if 18 <= age <= 65: print("工作年龄") # 输出:工作年龄# 链式不等式x = 0.5if 0 < x < 1: print("x是0到1之间的分数") # 输出:x是0到1之间的分数# 多个比较a, b, c = 3, 5, 7if a < b < c: ...
Leetcode 0116. Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each NodeYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: 123456struct Node { int val; Node *left; Node *right; Node *next;} Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Example 1: 123Input: root =...
C++多态机制解析:重载、重写与隐藏
一、概念C++ 的多态机制主要通过三个核心概念实现,它们在编译和运行时有着截然不同的处理方式: 概念 定义 绑定时机 核心特征 函数重载 同一作用域内,函数名相同但参数列表不同的函数 编译时 静态多态,基于参数列表区分 函数重写 派生类中重新定义基类中的虚函数,函数签名完全相同 运行时 动态多态,基于对象实际类型调用 函数隐藏 派生类中定义的函数遮蔽基类中同名函数,无论参数是否相同 编译时 名称遮蔽,基类函数被隐藏 注:函数签名包括函数名、参数类型和顺序,不包括返回值类型 二、函数重载解析函数重载是 C++ 实现静态多态的基础机制,允许在同一作用域内定义多个同名函数,通过参数列表的差异进行区分。 重载的实现原理编译器在编译阶段会对重载函数进行名称修饰(Name Mangling),根据函数名和参数列表生成唯一的内部名称,因此重载函数在底层实际上拥有不同的标识符。 重载示例代码123456789101112131415161718192021222324252627282930313233343536373839#include...
Leetcode 0112. Path Sum
112. Path SumGiven the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children. Example 1: 123Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22Output: trueExplanation: The root-to-leaf path with the target sum is shown. Example 2: 123456Input: root = [1,2,3], targetSum = 5Output: falseExplanation: There are two root-to-leaf...
Leetcode 0110. Balanced Binary Tree
110. Balanced Binary TreeGiven a binary tree, determine if it is height-balanced. Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: true Example 2: 12Input: root = [1,2,2,3,3,null,null,4,4]Output: false Example 3: 12Input: root = []Output: true 题目大意给定一棵二叉树,判断它是否是高度平衡的。高度平衡平衡二叉树 ** 的定义是:二叉树的每个节点的左右两个子树的高度差的绝对值不超过 1。 例如: 输入二叉树 [3,9,20,null,null,15,7],每个节点的左右子树高度差均不超过 1,返回 true; 输入二叉树 [1,2,2,3,3,null,null,4,4],根节点的左子树高度为 3,右子树高度为 1,差为 2,返回...
Leetcode 0108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search TreeGiven an integer array nums where the elements are sorted in ascending order, convert it to a *height-balanced* binary search tree. Example 1: 123Input: nums = [-10,-3,0,5,9]Output: [0,-3,9,-10,null,5]Explanation: [0,-10,5,null,-3,null,9] is also accepted: Example 2: 123Input: nums = [1,3]Output: [3,1]Explanation: [1,null,3] and [3,1] are both height-balanced BSTs. 题目大意给定一个升序排列的整数数组 nums,将其转换为一棵高度平衡的二叉搜索树(BST)。高度平衡的定义是:二叉树的左右两个子树的高度差的绝对值不超过...
派生类对象复制控制
一、复制控制的核心概念与场景复制控制机制是C++中处理对象创建、复制和销毁的核心手段。在继承体系中,当派生类对象被复制时,需要特别关注以下关键点: 复制场景 拷贝构造函数调用:当用已存在的对象初始化新对象时 赋值操作符调用:当用一个对象赋值给另一个对象时 析构函数调用:当对象生命周期结束时 典型问题 浅拷贝导致的指针悬挂(dangling pointer) 资源泄漏(resource leak) 自赋值(self-assignment)引发的异常 二、派生类复制的构造函数调用链12345678910111213class Base {public: Base() { /* 基类构造 */ } Base(const Base& other) { /* 基类拷贝构造 */ } ~Base() { /* 基类析构 */ }};class Derived : public Base {public: Derived() : Base() {...
Leetcode 0107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal IIGiven the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[15,7],[9,20],[3]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点 root,返回其节点值的自底向上的层序遍历结果。即按「从叶子节点所在层到根节点所在层」的顺序逐层返回,每层内部仍保持「从左到右」的顺序。 例如: 输入二叉树...
Leetcode 0106. Construct Binary Tree from Inorder and Postorder Traversal
106. Construct Binary Tree from Inorder and Postorder TraversalGiven two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree. Example 1: 12Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]Output: [3,9,20,null,null,15,7] Example 2: 12Input: inorder = [-1], postorder = [-1]Output: [-1] 题目大意给定一棵二叉树的中序遍历数组 inorder 和后序遍历数组...
Leetcode 0105. Construct Binary Tree from Preorder and Inorder Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal题目Given preorder and inorder traversal of a tree, construct the binary tree. **Note:**You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / \ 9 20 / \ 15 7 题目大意根据一棵树的前序遍历与中序遍历构造二叉树。 注意:你可以假设树中没有重复的元素。 解题思路 inorder_map:用于快速查找中序遍历中元素对应的索引,时间复杂度 O...
Python条件语句:if-elif-else分支结构
Python的条件语句用于根据不同的条件执行不同的代码块。本文将详细介绍Python中if、elif、else条件语句的使用方法。 一、基本语法1. 简单的if语句1234x = 10if x > 5: print("x大于5") # 输出:x大于5 2. if-else语句123456x = 3if x > 5: print("x大于5")else: print("x不大于5") # 输出:x不大于5 3. if-elif-else语句12345678910score = 85if score >= 90: print("优秀")elif score >= 80: print("良好") # 输出:良好elif score >= 70: print("中等")else: print("及格") 二、Python与C++的对比1....
Leetcode 0104. Maximum Depth of Binary Tree
104. Maximum Depth of Binary TreeGiven the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: 3 Example 2: 12Input: root = [1,null,2]Output: 2 题目大意给定一棵二叉树的根节点 root,返回该二叉树的最大深度。二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数量。 例如: 输入二叉树 [3,9,20,null,null,15,7],其最大深度为 3(路径:3 → 20 → 15 或 3 → 20 → 7,均包含 3...
Leetcode 0103. Binary Tree Zigzag Level Order Traversal
103. Binary Tree Zigzag Level Order Traversal Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[3],[20,9],[15,7]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点,返回其节点值的锯齿形层序遍历。即:第一层从左到右,第二层从右到左,第三层再从左到右,以此类推,交替进行。 例如: 输入 root =...
Leetcode 0102. Binary Tree Level Order Traversal
102. Binary Tree Level Order TraversalGiven the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[3],[9,20],[15,7]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点 root,返回其节点值的层序遍历结果(即从左到右、逐层遍历)。结果需以二维数组形式呈现,每一层的节点值构成一个子数组(例如,第一层 [根节点],第二层 [左子节点,...
C++ 继承机制中的类型转换
一、对象赋值的双向可行性分析1.1 基类到派生类的转换 隐式转换: 不允许直接赋值(如 Base b = d;) 显式转换: 需使用构造函数或类型转换运算符 123456789101112class Base {public: Base() {} explicit Base(int val) : data(val) {}private: int data;};class Derived : public Base {public: Derived(int val) : Base(val) {}}; 内存影响: 派生类对象包含基类数据成员,赋值操作不会改变大小,但会丢失派生类特有数据 1.2 派生类到基类的转换 隐式转换: 允许(如 Base b = d;) 显式转换: 可使用static_cast或构造函数 123Derived d;Base b = d; // 隐式转换Base& br =...
Leetcode 0101. Symmetric Tree
101. Symmetric TreeGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Example 1: 12Input: root = [1,2,2,3,4,4,3]Output: true Example 2: 12Input: root = [1,2,2,null,3,null,3]Output: false 题目大意给定一棵二叉树的根节点 root,判断该二叉树是否是对称的(即围绕中心轴镜像对称)。 例如: 输入二叉树 [1,2,2,3,4,4,3],其左子树与右子树成镜像,故返回 true; 输入二叉树 [1,2,2,null,3,null,3],左子树与右子树不镜像,故返回 false。 解题思路判断二叉树是否对称的核心是比较左子树与右子树是否成镜像,即: 左子树的左节点需与右子树的右节点值相等; 左子树的右节点需与右子树的左节点值相等。 1....
Leetcode 0100. Same Tree
100. Same TreeGiven the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example 1: 12Input: p = [1,2,3], q = [1,2,3]Output: true Example 2: 12Input: p = [1,2], q = [1,null,2]Output: false Example 3: 12Input: p = [1,2,1], q = [1,1,2]Output: false 题目大意给定两棵二叉树的根节点 p 和 q,判断这两棵树是否相同。两棵树相同的定义是:结构完全相同,且对应节点的值也相同。 例如: 输入两棵结构和节点值均相同的树...
Leetcode 0099. Recover Binary Search Tree
99. Recover Binary Search TreeYou are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. Example 1: 123Input: root = [1,3,null,null,2]Output: [3,1,null,null,2]Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid. Example 2: 123Input: root = [3,1,4,null,null,2]Output: [2,1,4,null,null,3]Explanation: 2 cannot be in the right...

