Leetcode 0023.Merge k Sorted Lists(python)
23. Merge k Sorted Lists题目You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1: 12345678910Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6 Example 2: 12Input: lists = []Output: [] Example 3: 12Input: lis...
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 1934. 确认率
1934. 确认率表: Signups 12345678+----------------+----------+| Column Name | Type |+----------------+----------+| user_id | int || time_stamp | datetime |+----------------+----------+User_id是该表的主键。每一行都包含ID为user_id的用户的注册时间信息。 表: Confirmations 1234567891011+----------------+----------+| Column Name | Type |+----------------+----------+| user_id | int || time_stamp | datetime || action | ENUM |+----------------+----------+(user_id, time_sta...
Leetcode 0022.Generate Parentheses(python)
22. Generate Parentheses题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: 12Input: n = 3Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: 12Input: n = 1Output: ["()"] 题目大意数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 你选用何种方法解题?本题的核心是生成所有有效的括号组合,属于经典的回溯算法(Backtracking)问题。 方法 时间复杂度 空间复杂度 是否推荐 DFS(字符串拼接) $O(\frac{4^n}{\sqrt{n}})$ $O(n)$ 推荐 DF...
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 ...
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()# 读写模...
Leetcode 1757. 可回收且低脂的产品
1757. 可回收且低脂的产品表:Products 12345678910+-------------+---------+| Column Name | Type |+-------------+---------+| product_id | int || low_fats | enum || recyclable | enum |+-------------+---------+product_id 是该表的主键(具有唯一值的列)。low_fats 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品是低脂产品,'N' 表示不是低脂产品。recyclable 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品可回收,而 'N' 表示不可回收。 编写解决方案找出既是低脂又是可回收的产品编号。 返回结果 无顺序要求 。 返回结果格式如下例...
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 0021.Merge Two Sorted Lists(python)
21. Merge Two Sorted Lists题目You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example 1: 12Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4] Example 2: 12Input: list1 = [], list2 = []Output: [] Example 3: 12Input: list1 = [], list2 = [0]Output: [0] 题目大意将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 ...
Leetcode 1201. Ugly Number III
1201. Ugly Number III题目Write a program to find the n-th ugly number. Ugly numbers are positive integers which are divisible by a or b or c. Example 1: Input: n = 3, a = 2, b = 3, c = 5 Output: 4 Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 3rd is 4. Example 2: Input: n = 4, a = 2, b = 3, c = 4 Output: 6 Explanation: The ugly numbers are 2, 3, 4, 6, 8, 9, 10, 12... The 4th is 6. Example 3: Input: n = 5, a = 2, b = 11, c = 13 Output: 10 Explanation: The ugly numbers are 2,...
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", &q...
C++ 纯虚函数与抽象类
一、什么是面向对象中的抽象在面向对象编程中,抽象是一种将复杂事物简化的方法,它关注对象的本质特征而非具体实现细节。想象一下,当我们谈论 "交通工具" 时,我们不会具体指明是汽车、自行车还是飞机,而是关注它们共同的特性:能够运输人和物,可以移动到不同地点等。 以游戏开发为例,不同角色(战士、法师、刺客)都具备移动、攻击、获取经验等行为,将这些共性抽象出来,就能构建出一个通用的角色概念。这种抽象思维在软件设计中非常有价值,它能帮助我们: 建立清晰的系统架构,将问题分解为合理的模块 定义通用接口,使不同实现可以互换使用 提高代码复用性,减少重复开发 便于团队协作,不同开发者可以基于相同接口并行工作 二、纯虚函数:定义接口的特殊函数纯虚函数是一种特殊的虚函数,它只有声明而没有具体实现,专门用于定义接口规范。在语法上,它的声明需要在函数原型后加上 "=0"。例如: 1234class AbstractClass {public: virtual double getArea() = 0; // 纯虚函数,计算图形...
Leetcode 0020.Valid Parentheses(python)
20. Valid Parentheses题目Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example 1: 12Input: s = "()"Output: true Example 2: 12Input: s = "(...
Leetcode 1193. 每月交易 I
1193. 每月交易 I表:Transactions 123456789101112+---------------+---------+| Column Name | Type |+---------------+---------+| id | int || country | varchar || state | enum || amount | int || trans_date | date |+---------------+---------+id 是这个表的主键。该表包含有关传入事务的信息。state 列类型为 ["approved", "declined"] 之一。 编写一个 sql 查询来查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。 以 任意顺序 返回结果表。 查询结果格式如下所示。 示例 1: 123456789101112131415161718输入:Transactio...
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 0019.Remove Nth Node From End of List(python)
19. Remove Nth Node From End of List题目Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: 12Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5] Example 2: 12Input: head = [1], n = 1Output: [] Example 3: 12Input: head = [1,2], n = 1Output: [1] 题目大意给定一个链表的头节点 head,删除链表的倒数第 n 个节点,并返回链表的头节点。 你选用何种方法解题?本题的核心是找到链表的倒数第 n 个节点并删除。 方法 时间复杂度 空间复杂度 是否推荐 双指针(快慢指针,哑节点) O(n) O(1) 推荐 双指针(快慢指针,无哑节点) O(n) O(1) 推荐 两次遍历 O(n) O(1) 可选 栈 O(n) O...
Leetcode 1089. Duplicate Zeros
1089. Duplicate Zeros题目Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place, do not return anything from your function. Example 1: 123Input: [1,0,2,3,0,4,5,0]Output: nullExplanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4] Example 2: 123Input: [1,2,3]Out...
C++ 虚析构函数详解:从原理到实践
一、内存管理与析构函数的基础核心原理 在C++中,对象的内存分配与释放是通过构造函数和析构函数完成的。当创建一个对象时: 构造函数负责初始化资源(如内存申请、文件打开) 析构函数负责释放资源(如内存回收、文件关闭) 资源管理规律 无资源类:普通类对象的析构无需特殊处理,编译器自动调用 有资源类:需要显式定义析构函数来处理资源释放 继承体系:当基类可能被继承时,必须考虑析构顺序问题 在单继承场景下,析构函数的调用遵循 "先派生类、后基类" 的顺序,这确保了资源释放的安全性。然而,当引入多态(使用基类指针指向派生类对象)时,普通析构函数就会暴露出严重的缺陷。 问题的引出考虑以下场景: 我们有一个基类Base和派生类Derived 使用Base*类型的指针指向Derived类的对象 当通过基类指针删除对象时,会发生什么? 典型场景 123456789class Base {public: ~Base() { cout << "Base析构" << endl; }...
Leetcode 1048. Longest String Chain
1048. Longest String ChainYou are given an array of words where each word consists of lowercase English letters. wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB. For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".A word chain is a sequence of words [word1, word2, ..., wordk] with k >...
Leetcode 0018.4Sum(python)
18. 4Sum题目Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 0 <= a, b, c, d < n a, b, c, and d are distinct. nums[a] + nums[b] + nums[c] + nums[d] == target You may return the answer in any order. Example 1: 12Input: nums = [1,0,-1,0,-2,2], target = 0Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] Example 2: 12Input: nums = [2,2,2,2,2], target = 8Output: [[2,2,2,2]] 题目大意在整数数组中找出所有不重复的四元组,使得四个数的和等于目标值 target。要...
Leetcode 1047. Remove All Adjacent Duplicates In String
1047. Remove All Adjacent Duplicates In StringYou are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them. We repeatedly make duplicate removals on s until we no longer can. Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique. Example 1: 1234Input: s = "abbaca"Output: "ca"Explanation: For example, in "abbaca&...
Python匹配语句:match-case与switch对比
Python 3.10引入了match语句,这是一种强大的模式匹配机制,类似于其他语言中的switch语句,但功能更加强大。本文将详细介绍Python中match语句的用法。 一、match语句的基本用法1. 基本语法1234567891011121314def http_status(status): match status: case 200: return "OK" case 404: return "Not Found" case 500: return "Internal Server Error" case _: return "Unknown"print(http_status(200)) # 输出:OKprint(http_status(404)) # 输出:Not Foundprint(http_status(999)) ...
Leetcode 1023. Camelcase Matching
1023. Camelcase MatchingGiven an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise. A query word queries[i] matches pattern if you can insert lowercase English letters into the pattern so that it equals the query. You may insert a character at any position in pattern or you may choose not to insert any characters at all. Example 1: 12345Input: queries = ["FooBar","FooBarTest&qu...
C++ 虚函数访问控制
一、虚函数的访问控制虚函数的访问控制(public、protected、private)会影响其在派生类中的重写和调用规则,这是容易混淆的知识点。 1. public 虚函数基类中 public 的虚函数在派生类中可以被重写为 public 或 protected,但不能重写为 private(在 C++11 前允许,C++11 后被禁止): 123456789101112131415161718192021222324252627282930313233343536373839class Base {public: virtual void publicFunc() { cout << "Base::publicFunc" << endl; }};class Derived : public Base {public: // 正确:重写为public void publicFunc() override { cout << "Deri...
Leetcode 0017.Letter Combinations of a Phone Number(python)
17. Letter Combinations of a Phone Number题目Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. 123456782 -> "abc"3 -> "def"4 -> "ghi"5 -> "jkl"6 -> "mno"7 -> "pqrs"8 -> "tuv"9...

