Leetcode 0445. Add Two Numbers II
445. Add Two Numbers IIYou are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. 1234567891011121314151617181920212223242526272829303132333435363738394041424344/** * Definition for singly-linked list. * struct ListNode { * int...
Leetcode 0429. N-ary Tree Level Order Traversal
429. N-ary Tree Level Order TraversalGiven an n-ary tree, return the level order traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Example 1: 12Input: root = [1,null,3,2,4,null,5,6]Output: [[1],[3,2,4],[5,6]] Example 2: 12Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]Output:...
C++虚基类与虚函数的内存布局
一、40字节对象大小的组成结构在标准C++中,一个包含虚基类和虚函数的类实例会由以下组件构成: 内存结构分解1234[对象地址] ├── 虚函数表指针 (8字节) → 用于多态调用├── 偏移量表 (16字节) → 处理虚基类偏移└── 数据成员 (16字节) → 本类的实际数据 关键点: 虚函数表指针会占用8字节(64位系统)或4字节(32位系统) 虚基类引入的偏移量表通常需要16字节(包含两个虚基类指针) 本类数据成员占用16字节(假设包含两个double类型成员) 总内存大小 = 虚函数表指针 + 偏移量表 + 数据成员 二、虚基类继承关系考虑以下类继承结构: 1234567class Figure { virtual void draw() = 0; }; // 虚基类class Space { virtual void calc() = 0; }; // 虚基类class Circle : virtual public Figure, virtual public Space { double...
Leetcode 0409. 最长回文串
409. 最长回文串给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的 回文串 的长度。 在构造过程中,请注意 区分大小写 。比如 "Aa" 不能当做一个回文字符串。 示例 1: 1234输入:s = "abccccdd"输出:7解释:我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。 示例 2: 123输入:s = "a"输出:1解释:可以构造的最长回文串是"a",它的长度是 1。 解题思路核心思路是统计字符频率,利用回文串的特性计算最大长度: 回文串特性: 回文串对称位置的字符相同; 偶数长度的回文串:所有字符出现次数均为偶数; 奇数长度的回文串:仅有一个字符出现奇数次(位于中心),其余均为偶数次。 频率统计: 统计字符串中每个字符的出现次数; 累计所有字符的最大偶数次(例如,出现 5 次的字符可贡献 4 次); 若存在出现奇数次的字符,可在结果中加 1(作为中心字符)。 计算逻辑: 初始化结果...
Leetcode 0404. Sum of Left Leaves
404. Sum of Left LeavesGiven the root of a binary tree, return the sum of all left leaves. A leaf is a node with no children. A left leaf is a leaf that is the left child of another node. Example 1: 123Input: root = [3,9,20,null,null,15,7]Output: 24Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. Example 2: 12Input: root = [1]Output: 0 题目大意给定一棵二叉树的根节点 root,返回所有左叶子的节点值之和。左叶子的定义是:既是叶子节点(没有子节点),又是其父节点的左子节点。 例如: 输入二叉树 [3,9,20,null,null,15,7],左叶子为...
Leetcode 0402. 移掉 K 位数字
402. 移掉 K 位数字给你一个以字符串表示的非负整数 num 和一个整数 k ,移除这个数中的 k 位数字,使得剩下的数字最小。请你以字符串形式返回这个最小的数字。 示例 1 : 123输入:num = "1432219", k = 3输出:"1219"解释:移除掉三个数字 4, 3, 和 2 形成一个新的最小的数字 1219 。 示例 2 : 123输入:num = "10200", k = 1输出:"200"解释:移掉首位的 1 剩下的数字为 200. 注意输出不能有任何前导零。 示例 3 : 123输入:num = "10", k = 2输出:"0"解释:从原数字移除所有的数字,剩余为空就是 0 。 解题思路核心思路是单调栈 + 贪心算法,通过维护一个递增的栈来确保剩余数字最小,同时控制移除的数字数量不超过 k: 单调栈逻辑: 遍历字符串中的每个数字,对于当前数字c: 若栈不为空,且栈顶数字大于 c,且仍有可移除的次数(k >...
Python数据结构:列表与字典操作
Python中的列表(List)和字典(Dictionary)是两种最常用的数据结构。列表类似于数组,字典是一种键值对数据结构。本文将详细介绍这两种数据结构的用法。 一、列表(List)1. 基本操作123456789101112# 创建列表fruits = ["apple", "banana", "cherry"]numbers = [1, 2, 3, 4, 5]mixed = [1, "hello", 3.14, True]# 访问元素print(fruits[0]) # appleprint(fruits[-1]) # cherry# 修改元素fruits[0] = "orange"print(fruits) # ['orange', 'banana', 'cherry'] 2. 列表方法12345678910111213141516# 添加元素fruits = ["apple",...
Leetcode 0367. Valid Perfect Square
367. Valid Perfect Square题目Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Output: true Example 2: Input: 14 Output: false 完全平方数可以表示为连续奇数的和。 算法原理该算法利用了以下数学特性: 1 = 1(1²) 1 + 3 = 4(2²) 1 + 3 + 5 = 9(3²) 1 + 3 + 5 + 7 = 16(4²) 以此类推,n² = 1 + 3 + 5 + ... + (2n-1) 算法通过不断从目标数中减去连续的奇数(1, 3, 5, 7...),如果最终结果恰好为...
Leetcode 0349. Intersection of Two Arrays
349. Intersection of Two ArraysGiven two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order. Example 1: 12Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2] Example 2: 123Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output: [9,4]Explanation: [4,9] is also accepted. 题目大意给定两个整数数组 nums1 和...
C++ 纯虚函数与抽象类
一、什么是面向对象中的抽象在面向对象编程中,抽象是一种将复杂事物简化的方法,它关注对象的本质特征而非具体实现细节。想象一下,当我们谈论 "交通工具" 时,我们不会具体指明是汽车、自行车还是飞机,而是关注它们共同的特性:能够运输人和物,可以移动到不同地点等。 以游戏开发为例,不同角色(战士、法师、刺客)都具备移动、攻击、获取经验等行为,将这些共性抽象出来,就能构建出一个通用的角色概念。这种抽象思维在软件设计中非常有价值,它能帮助我们: 建立清晰的系统架构,将问题分解为合理的模块 定义通用接口,使不同实现可以互换使用 提高代码复用性,减少重复开发 便于团队协作,不同开发者可以基于相同接口并行工作 二、纯虚函数:定义接口的特殊函数纯虚函数是一种特殊的虚函数,它只有声明而没有具体实现,专门用于定义接口规范。在语法上,它的声明需要在函数原型后加上 "=0"。例如: 1234class AbstractClass {public: virtual double getArea() = 0; //...
Leetcode 0316. 去除重复字母
316. 去除重复字母给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。 示例 1: 12输入:s = "bcabc"输出:"abc" 示例 2: 12输入:s = "cbacdcbc"输出:"acdb" 核心思路是单调栈 + 贪心算法,通过维护一个单调递增的栈来确保字典序最小,同时利用计数数组判断字符是否还有剩余,确保每个字符只保留一次: 预处理: 统计字符串中每个字符的出现次数(count 数组); 记录字符是否已在栈中(in_stack 数组),避免重复添加。 单调栈逻辑: 遍历字符串中的每个字符C: 减少 count[c](当前字符已被考虑); 若 c 已在栈中,直接跳过; 若 c 不在栈中,且栈不为空,且栈顶字符大于 c,且栈顶字符在后续还有出现(count[栈顶字符] > 0),则弹出栈顶字符(确保字典序更小); 将 c...
Leetcode 0287. Find the Duplicate Number
287. Find the Duplicate NumberGiven an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and using only constant extra space. Example 1: 12Input: nums = [1,3,4,2,2]Output: 2 Example 2: 12Input: nums = [3,1,3,4,2]Output: 3 Example 3: 12Input: nums = [3,3,3,3,3]Output: 3 题目大意给定一个包含 n + 1 个整数的数组 nums,其中每个整数都在...
Leetcode 0283. Move Zeroes
283. Move Zeroes题目Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Example 1: 12Input: [0,1,0,3,12]Output: [1,3,12,0,0] Note: You must do this in-place without making a copy of the array. Minimize the total number of operations. 解法1:双指针1234567891011121314151617181920class Solution {public: void moveZeroes(vector<int>& nums) { int n = nums.size(); int i = 0; //...
Leetcode 0257. Binary Tree Paths
257. Binary Tree PathsGiven the root of a binary tree, return all root-to-leaf paths in any order. A leaf is a node with no children. Example 1: 12Input: root = [1,2,3,null,5]Output: ["1->2->5","1->3"] Example 2: 12Input: root = [1]Output: ["1"] 题目大意给定一棵二叉树的根节点 root,返回所有从根节点到叶子节点的路径。叶子节点是指没有子节点的节点,路径以字符串形式表示,节点值之间用 "->" 连接。 例如: 输入二叉树 [1,2,3,null,5],根到叶子的路径为 1->2->5 和 1->3,返回...
Leetcode 0239. Sliding Window Maximum
239. Sliding Window MaximumYou are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. Example 1: 1234567891011Input: nums = [1,3,-1,-3,5,3,6,7], k = 3Output: [3,3,5,5,6,7]Explanation: Window position Max--------------- -----[1 3 -1] -3 5 3 6...
Python循环结构:while与for迭代器详解
Python提供了两种主要的循环结构:while循环和for循环。本文将详细介绍这两种循环的使用方法,以及range()迭代器的使用。 一、while循环1. 基本语法12345count = 0while count < 5: print(count) count += 1# 输出:0, 1, 2, 3, 4 2. while-else结构123456count = 0while count < 5: print(count) count += 1else: print("循环正常结束") # 循环正常结束时执行 3. 无限循环12345while True: user_input = input("输入 'quit' 退出: ") if user_input == "quit": break print(f"你输入了: {user_input}") 二、for循环1....
Leetcode 0232. Implement Queue using Stacks
232. Implement Queue using StacksImplement a first in first out (FIFO) queue using only two Stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean empty() Returns true if the queue is empty, false...
C++ 虚析构函数详解:从原理到实践
一、内存管理与析构函数的基础核心原理 在C++中,对象的内存分配与释放是通过构造函数和析构函数完成的。当创建一个对象时: 构造函数负责初始化资源(如内存申请、文件打开) 析构函数负责释放资源(如内存回收、文件关闭) 资源管理规律 无资源类:普通类对象的析构无需特殊处理,编译器自动调用 有资源类:需要显式定义析构函数来处理资源释放 继承体系:当基类可能被继承时,必须考虑析构顺序问题 在单继承场景下,析构函数的调用遵循 "先派生类、后基类" 的顺序,这确保了资源释放的安全性。然而,当引入多态(使用基类指针指向派生类对象)时,普通析构函数就会暴露出严重的缺陷。 问题的引出考虑以下场景: 我们有一个基类Base和派生类Derived 使用Base*类型的指针指向Derived类的对象 当通过基类指针删除对象时,会发生什么? 典型场景 123456789class Base {public: ~Base() { cout << "Base析构" << endl;...
Leetcode 0227. Basic Calculator II
227. Basic Calculator IIGiven a string s which represents an expression, evaluate this expression and return its value. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1]. Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval(). 题目大意给定一个字符串 s 表示一个算术表达式,要求计算该表达式的值并返回。表达式仅包含非负整数、+、-、*、/...
Leetcode 0226. Invert Binary Tree
226. Invert Binary TreeGiven the root of a binary tree, invert the tree, and return its root. Example 1: 12Input: root = [4,2,7,1,3,6,9]Output: [4,7,2,9,6,3,1] Example 2: 12Input: root = [2,1,3]Output: [2,3,1] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点 root,翻转这棵二叉树(即交换每个节点的左子树和右子树),并返回翻转后的根节点。 例如: 输入二叉树 [4,2,7,1,3,6,9],翻转后每个节点的左右子树互换,输出为 [4,7,2,9,6,3,1]。 解题思路翻转二叉树的核心是交换每个节点的左子节点和右子节点,可以通过递归或迭代两种方式实现: 1....

