C++ Map 容器
一、核心数据结构:红黑树C++ 标准库中std::map的底层实现采用红黑树(Red-Black Tree),这是一种自平衡的二叉搜索树(BST)。红黑树通过特定的规则保证树的高度始终维持在 O (log n) 级别,从而确保各种操作的高效性。 1具体见前一篇 C++ 标准库中的 set 容器 二、map 容器的核心组件2.1 迭代器实现std::map的迭代器是双向迭代器,其实现本质上是红黑树节点的指针封装,并提供了遍历树的操作。 2.2 内存管理机制std::map通常使用内存池(memory pool)或分配器(allocator)管理节点内存,而非频繁调用new和delete: 采用 slab 分配策略,预先分配一批节点内存 节点释放时不直接归还给系统,而是放入空闲列表 下次分配时优先从空闲列表获取,减少系统调用开销 三、核心操作实现3.1...
Leetcode 0071. Simplify Path
71. Simplify PathYou are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path. The rules of a Unix-style file system are as follows: A single period '.' represents the current directory. A double period '..' represents the previous/parent directory. Multiple consecutive slashes such as '//' and '///' are treated as a single slash...
Leetcode 0070. Climbing Stairs
70. Climbing Stairs题目You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2: Input: 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1...
Leetcode 0069. Sqrt(x)
69. Sqrt(x)Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator. For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Example 1: 123Input: x = 4Output: 2Explanation: The square root of 4 is 2, so we return 2. Example 2: 123Input: x = 8Output: 2Explanation: The square root of 8 is 2.82842..., and since we round it down to...
Leetcode 0059. Spiral Matrix II
59. Spiral Matrix IIGiven a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. Example 1: 12Input: n = 3Output: [[1,2,3],[8,9,4],[7,6,5]] Example 2: 12Input: n = 1Output: [[1]] Constraints: 1 <= n <= 20 题目大意给定一个正整数 n,生成一个 n×n 的矩阵,其中元素从 1 到 n² 按螺旋顺序填充。 解题思路 创建一个 n×n 的空矩阵 定义四个边界:上、下、左、右 按顺时针方向(右→下→左→上)填充数字 每填充完一行或一列就调整相应的边界 重复步骤 3-4 直到所有数字都被填充 123456789101112131415161718192021222324252627282930313233343536373839class Solution...
Leetcode 0055. Jump Game
55. Jump GameYou are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise. Example 1: 123Input: nums = [2,3,1,1,4]Output: trueExplanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: 123Input: nums = [3,2,1,0,4]Output: falseExplanation: You will always arrive at index 3 no matter...
Leetcode 0054. 螺旋矩阵
54. 螺旋矩阵给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。 示例 1: 12输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]输出:[1,2,3,6,9,8,7,4,5] 示例 2: 12输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]输出:[1,2,3,4,8,12,11,10,9,5,6,7] 提示: m == matrix.length n == matrix[i].length 1 <= m, n <= 10 -100 <= matrix[i][j] <= 100 题目大意给定一个 m 行 n...
Leetcode 0053. Maximum Subarray
53. Maximum SubarrayGiven an integer array nums, find the subarray with the largest sum, and return its sum. Example 1: 123Input: nums = [-2,1,-3,4,-1,2,1,-5,4]Output: 6Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: 123Input: nums = [1]Output: 1Explanation: The subarray [1] has the largest sum 1. Example 3: 123Input: nums = [5,4,-1,7,8]Output: 23Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. 题目大意给定一个整数数组...
Python数值处理:round()函数四舍五入机制
Python的round()函数是处理浮点数四舍五入的重要工具。本文将详细介绍round()函数的使用方法以及常见的精度问题。 一、round()函数的基本用法1. 基本语法1round(number, ndigits) number:要四舍五入的数字 ndigits:保留的小数位数(可选,默认为0) 2. 基本示例12345# 基本四舍五入print(round(3.14159)) # 输出:3print(round(3.5)) # 输出:4print(round(3.14159, 2)) # 输出:3.14print(round(3.14159, 4)) # 输出:3.1416 3. 负数四舍五入1234567# 负数四舍五入到整数print(round(-3.5)) # 输出:-4print(round(-3.4)) # 输出:-3# 负数四舍五入到小数位print(round(-3.14159, 2)) # 输出:-3.14print(round(-3.14159, 3)) #...
Leetcode 0048. Rotate Image
48. Rotate ImageYou are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Example 1: 12Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2: 12Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]Output:...
C++ 标准库中的 set 容器
一、set 容器的本质与底层数据结构C++ 标准库中的std::set是一种关联式容器,其核心特性是自动排序和唯一性。与序列式容器(如 vector、list)不同,set 中的元素是按照特定的排序规则进行存储的,这使得 set 具有高效的查找、插入和删除操作。 std::set的底层实现采用了红黑树(Red-Black Tree) 这一自平衡二叉搜索树数据结构。红黑树通过一系列规则保证了树的平衡性,从而确保了基本操作(插入、删除、查找)的时间复杂度均为 O (logN),其中 N 为树中元素的数量。 在 C++ 标准库中,红黑树的实现通常被封装在_Rb_tree类模板中,而 set 则是该类模板的一个特化应用,专门用于存储键值对中的键(在 set 中,键和值是同一个概念) 二、红黑树的结构解析2.1 红黑树节点的构成红黑树的每个节点通常包含以下几个部分: 键(key):即存储的元素值,用于节点间的比较 左孩子指针(left child):指向当前节点的左子节点 右孩子指针(right...
Leetcode 0046. Permutations
46. PermutationsGiven an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: 12Input: nums = [1,2,3]Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: 12Input: nums = [0,1]Output: [[0,1],[1,0]] Example 3: 12Input: nums = [1]Output: [[1]] 题目大意给定一个无重复元素的整数数组 nums,返回该数组所有可能的全排列。全排列是指包含数组所有元素的有序序列,且每个元素仅出现一次,结果顺序可任意。 例如: 输入 nums = [1,2,3],输出 [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]](共 3! = 6...
Leetcode 0045. 跳跃游戏 II
45. 跳跃游戏 II给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。 每个元素 nums[i] 表示从索引 i 向后跳转的最大长度。换句话说,如果你在索引 i 处,你可以跳转到任意 (i + j) 处: 0 <= j <= nums[i] 且 i + j < n 返回到达 n - 1 的最小跳跃次数。测试用例保证可以到达 n - 1。 示例 1: 1234输入: nums = [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2。 从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。 示例 2: 12输入: nums = [2,3,0,1,4]输出: 2 解题思路核心思路是贪心算法,通过跟踪 “当前跳跃的最远边界” 和 “下一次跳跃的最远可达位置”,在每一步跳跃时选择最优范围,实现最少跳跃次数: 贪心策略: 用 end 表示当前跳跃能到达的最远边界(初始为 0); 用 max_reach 表示从当前位置到 end 之间的所有位置能跳到的最远位置; 用...
Leetcode 0042. 接雨水
42. 接雨水给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例 1: 123输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]输出:6解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 示例 2: 12输入:height = [4,2,0,3,2,5]输出:9 解题思路核心思路是双指针边界,通过每个柱子能接住的雨水量取决于其左右两侧的最高柱子中较矮的那个(短板效应): 边界定义: 对于位置 i,左侧最高柱子高度为 left_max[i]; 右侧最高柱子高度为 right_max[i]; 位置 i 能接住的雨水量为 min(left_max[i], right_max[i]) - height[i](若结果为正,否则为 0)。 优化实现: 采用双指针法,无需额外存储 left_max 和 right_max 数组,将空间复杂度降至 O (1); 用 left 和 right...
Leetcode 0040. Combination Sum II
40. Combination Sum IIGiven a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. Each number in candidates may only be used once in the combination. Note: The solution set must not contain duplicate combinations. Example 1: 12345678Input: candidates = [10,1,2,7,6,1,5], target = 8Output: [[1,1,6],[1,2,5],[1,7],[2,6]] Example 2: 123456Input: candidates = [2,5,2,1,2], target =...
Leetcode 0039. Combination Sum
39. Combination SumGiven an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order. The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. The test cases are generated such that the number of unique combinations that sum up to...
Leetcode 0035. Search Insert Position
35. Search Insert Position题目Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: 12Input: [1,3,5,6], 5Output: 2 Example 2: 12Input: [1,3,5,6], 2Output: 1 Example 3: 12Input: [1,3,5,6], 7Output: 4 Example 4: 12Input: [1,3,5,6], 0Output:...
C++ 标准库中 pair 的实现原理与应用解析
一、pair 的模板定义与类型参数设计1.1 基本模板定义C++ 标准库中的std::pair是一个模板类,用于存储两个异构对象作为一个单元。其基本定义如下: 123456789101112namespace std { template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; // 构造函数及其他成员函数... };} 这个定义展示了pair的核心特征: 两个模板类型参数T1和T2,分别指定了两个成员的类型 公开的成员变量first和second,分别存储第一个和第二个元素 类型别名first_type和second_type,用于获取元素类型 1.2 类型推导机制在 C++11...
Python类型转换机制及与C++对比
Python和C++在类型转换方面有着显著的不同。Python是一种动态类型语言,类型转换通常发生在运行时;而C++是一种静态类型语言,类型转换需要在编译时明确指定。本文将详细介绍Python中的类型转换方式及其与C++的区别。 一、Python类型转换的基本方式1. 隐式类型转换Python在某些情况下会自动进行类型转换: 12345678910# 整数和浮点数运算时,整数自动转换为浮点数result = 10 + 3.5print(result) # 输出:13.5print(type(result)) # 输出:<class 'float'># 布尔值与整数运算result = True + 5print(result) # 输出:6result = False + 10print(result) # 输出:10 2. 显式类型转换(强制类型转换)Python使用构造函数进行显式类型转换: 123456789101112# 转换为整数print(int(3.7)) # 输出:3print(int("42"))...
Leetcode 0034. Find First and Last Position of Element in Sorted Array
34. Find First and Last Position of Element in Sorted Array题目Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] 查找第一个出现的位置: 当找到目标值时,不立即返回,而是继续向左查找(right...

