Leetcode 0704. Binary Search
704. Binary Search题目Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1. Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Example 2: Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1 Note: You may assume that all elements in nums are...
Leetcode 0637. Average of Levels in Binary Tree
637. Average of Levels in Binary TreeGiven the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted. Example 1: 1234Input: root = [3,9,20,null,null,15,7]Output: [3.00000,14.50000,11.00000]Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.Hence return [3, 14.5, 11]. Example 2: 12Input: root = [3,9,20,15,7]Output:...
Leetcode 0626. 换座位
626. 换座位表: Seat 123456789+-------------+---------+| Column Name | Type |+-------------+---------+| id | int || student | varchar |+-------------+---------+id 是该表的主键(唯一值)列。该表的每一行都表示学生的姓名和 ID。ID 序列始终从 1 开始并连续增加。 编写解决方案来交换每两个连续的学生的座位号。如果学生的数量是奇数,则最后一个学生的id不交换。 按 id 升序 返回结果表。 查询结果格式如下所示。 示例 1: 1234567891011121314151617181920212223输入: Seat 表:+----+---------+| id | student |+----+---------+| 1 | Abbot || 2 | Doris || 3 | Emerson || 4 | Green || 5 | Jeames ...
C++ 类模板
一、模板类基本概念1.1 什么是类模板类模板是 C++ 泛型编程的核心机制,允许我们定义一个通用的类结构,该结构能与多种数据类型一起工作,而无需为每种类型重复编写代码。 类模板不是一个具体的类,而是类的 "蓝图",编译器会根据实际使用的类型参数生成具体的类实例(模板实例化)。 1234567891011121314151617181920212223242526// 简单的类模板示例template <typename T>class Box {private: T content;public: // 构造函数 Box(T value) : content(value) {} // 获取存储的值 T getContent() const { return content; } // 设置新值 void setContent(T newValue) { content = newValue; }};//...
Leetcode 0611. Valid Triangle Number
611. Valid Triangle NumberGiven an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: 123456Input: nums = [2,2,3,4]Output: 3Explanation: Valid combinations are: 2,3,4 (using the first 2)2,3,4 (using the second 2)2,2,3 Example 2: 12Input: nums = [4,2,3,4]Output: 4 题目大意给定一个整数数组 nums,返回数组中可作为三角形三条边长度的三元组数量。三角形三条边需满足:任意两边之和大于第三边。 核心解题思路:排序 + 双指针三角形三边的核心条件是:两边之和大于第三边。对于排序后的数组,可简化为: 设三条边为 a ≤...
C++ 函数模板
一、概述1.1 函数模板的定义函数模板是 C++ 泛型编程的核心,它允许定义带类型参数的函数原型,经编译器实例化后生成不同数据类型的函数,本质上是编译期指令。 1.2 函数模板的优势函数模板的核心优势体现在: 代码复用:类型参数化使一套模板适配多种数据类型,减少重复代码 类型安全:利用静态类型检查,规避编译阶段类型转换错误 高效执行:实例化函数直接嵌入,无运行时额外开销 1.3 应用场景函数模板常用于 STL 容器与算法设计、链表 / 栈等数据结构的类型无关实现,以及排序、查找等通用算法的多态封装。 二、基础语法2.1 模板声明函数模板的语法结构遵循以下范式: 1234template <typename T>返回类型 函数名 (参数列表) { // 函数体实现} 其中,typename关键字用于声明类型参数,class关键字在此语境下具备完全等价语义;T作为类型参数占位符,可根据实际需求替换为任意合法标识符。 2.2 简单示例:交换函数以数据交换操作为例,其模板实现如下: 123456template...
Leetcode 0572. Subtree of Another Tree
572. Subtree of Another TreeGiven the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise. A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself. Example 1: 12Input: root = [3,4,5,1,2], subRoot = [4,1,2]Output: true Example 2: 12Input: root =...
Leetcode 0570. 至少有5名直接下属的经理
570. 至少有5名直接下属的经理表: Employee 123456789101112+-------------+---------+| Column Name | Type |+-------------+---------+| id | int || name | varchar || department | varchar || managerId | int |+-------------+---------+id 是此表的主键(具有唯一值的列)。该表的每一行表示雇员的名字、他们的部门和他们的经理的id。如果managerId为空,则该员工没有经理。没有员工会成为自己的管理者。 编写一个解决方案,找出至少有五个直接下属的经理。 以 任意顺序 返回结果表。 查询结果格式如下所示。 示例 1: 123456789101112131415161718输入: Employee 表:+-----+-------+------------+-----------+| id | name | department...
Python测试框架:pytest与assert断言
Python提供了多种测试工具,其中pytest是最流行的单元测试框架之一。本文将介绍Python中的assert语句以及pytest框架的基本用法。 一、assert语句1. 基本用法1234567# assert 条件x = 10assert x > 0 # 条件为True,无输出# assert 条件, 错误信息x = -5assert x > 0, "x必须大于0" # 抛出AssertionError 2. 常见用途12345678910def divide(a, b): assert b != 0, "除数不能为零" return a / bdef validate_age(age): assert 0 <= age <= 150, "年龄必须在0到150之间" return ageprint(validate_age(25)) # 正常# validate_age(-5) # 抛出AssertionError 二、pytest框架1....
Leetcode 0515. Find Largest Value in Each Tree Row
515. Find Largest Value in Each Tree RowGiven the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). Example 1: 12Input: root = [1,3,2,5,3,null,9]Output: [1,3,9] Example 2: 12Input: root = [1,2,3]Output: [1,3] 题目大意给定一棵二叉树的根节点 root,返回一个数组,其中每个元素是二叉树对应行(从 0 开始索引)中的最大值。 例如: 输入二叉树 [1,3,2,5,3,null,9],第 0 行最大值为 1,第 1 行最大值为 3,第 2 行最大值为 9,因此返回...
Leetcode 0513. Find Bottom Left Tree Value
513. Find Bottom Left Tree ValueGiven the root of a binary tree, return the leftmost value in the last row of the tree. Example 1: 12Input: root = [2,1,3]Output: 1 Example 2: 12Input: root = [1,2,3,4,null,5,6,null,null,7]Output: 7 题目大意给定一棵二叉树的根节点 root,返回树的最后一行中最左边的节点值。即需要找到二叉树最深一层的最左侧节点的值。 例如: 输入二叉树 [2,1,3],最深层是第 2 层,最左侧节点值为 1,返回 1; 输入二叉树 [1,2,3,4,null,5,6,null,null,7],最深层是第 4 层,最左侧节点值为 7,返回 7。 解题思路找到树左下角的值,关键是确定最深层和该层的最左侧节点。主要有两种实现方法: 1....
Leetcode 0509. Fibonacci Number
509. Fibonacci Number题目The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given N, calculate F(N). Example 1: Input: 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. Example 3: Input: 4 Output: 3 Explanation: F(4)...
Leetcode 0503. 下一个更大元素 II
503. 下一个更大元素 II给定一个循环数组 nums ( nums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素 。 数字 x 的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1 。 示例 1: 12345输入: nums = [1,2,1]输出: [2,-1,2]解释: 第一个 1 的下一个更大的数是 2;数字 2 找不到下一个更大的数; 第二个 1 的下一个最大的数需要循环搜索,结果也是 2。 示例 2: 12输入: nums = [1,2,3,4,3]输出: [2,3,4,-1,4] 解题思路核心思路是单调栈 + 数组翻倍模拟循环,通过将数组复制一份拼接在末尾(模拟循环),利用单调栈高效找到每个元素的下一个更大元素: 模拟循环数组: 将原数组 nums 复制一份并拼接在末尾(如 [1,2,1] 变为...
C++ 函数定义与调用中的符号体系
引言在 C++ 函数的定义与调用过程中,< >、( )、[ ]和{ }等符号具有明确的语义边界和使用规范。理解这些符号的准确含义和应用场景,对于编写正确、高效的 C++ 代码至关重要。 一、< >在函数模板中的应用尖括号< >主要用于函数模板的参数列表,用于指定模板类型参数或非类型参数。 1.1 函数模板定义中的< >在函数模板定义中,< >用于声明模板参数列表: 1234template <typename T> // 模板参数列表T max(T a, T b) { return (a > b) ? a : b;} 这里的声明了一个类型参数T,使函数能够接受任意类型的参数。 1.2 函数模板调用中的< >在调用函数模板时,可以显式指定模板参数: 12int result1 = max<int>(3, 5); // 显式指定模板参数为intdouble result2 = max<double>(3.2, 5.7); //...
Leetcode 0496. 下一个更大元素 I
496. 下一个更大元素 Inums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。 给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。 对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。 返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。 示例 1: 123456输入:nums1 = [4,1,2], nums2 = [1,3,4,2].输出:[-1,3,-1]解释:nums1 中每个值的下一个更大元素如下所述:- 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。- 1 ,用加粗斜体标识,nums2 =...
Leetcode 0491. Non-decreasing Subsequences
491. Non-decreasing SubsequencesGiven an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order. Example 1: 12Input: nums = [4,6,7,7]Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]] Example 2: 12Input: nums = [4,4,3,2,1]Output: [[4,4]] 题目大意给定一个整数数组 nums,返回所有不同的、长度至少为 2 的非递减子序列。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。 例如: 输入 nums = [4,6,7,7],输出包含...
Python引用机制:无指针设计与内存管理
Python是一种高级编程语言,其设计理念之一就是让开发者无需关心底层的内存管理。因此,Python中没有像C或C++那样的指针概念。本文将介绍Python的引用机制以及它与指针的区别。 一、Python的引用机制1. 变量即引用在Python中,变量更像是标签或引用,而不是存储数据的容器: 1234567891011# 创建变量x = 10y = xprint(x) # 10print(y) # 10# x和y指向同一个对象y = 20print(x) # 10(x不受影响)print(y) # 20 2. 对象与引用Python中的每个对象都有: 身份(id):对象的唯一标识 类型(type):对象的类型 值(value):对象的值 123456x = [1, 2, 3]y = xprint(id(x)) # 对象身份print(id(y)) # 相同身份print(x is y) # True:x和y指向同一对象 二、可变对象与不可变对象1. 不可变对象不可变对象包括:整数、浮点数、字符串、元组等 1234567# 不可变对象x = 10y = xy...
Leetcode 0455. Assign Cookies
455. Assign CookiesAssume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. Example...
Leetcode 0454. 4Sum II
454. 4Sum IIGiven four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 <= i, j, k, l < n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 Example 1: 123456Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]Output: 2Explanation:The two tuples are:1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 02. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2...
Python IO操作:文件读写与标准输入输出
Python的IO(输入/输出)操作是编程中非常基础且重要的部分,它允许程序与外部世界进行交互。本文将详细介绍Python中的文件读写操作、标准输入输出以及相关的最佳实践。 一、文件读写操作1. 打开和关闭文件在Python中,使用open()函数打开文件,使用close()方法关闭文件。 123456789# 打开文件file = open('example.txt', 'r')# 操作文件content = file.read()print(content)# 关闭文件file.close() 2. 文件打开模式 模式 描述 r 只读模式(默认) w 写入模式,会覆盖现有文件 a 追加模式,在文件末尾添加内容 x 独占创建模式,如果文件已存在则报错 b 二进制模式 t 文本模式(默认) + 读写模式 12345678# 二进制模式打开with open('image.jpg', 'rb') as f: data = f.read()#...

