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...
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 查询来查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。 以 任意顺序 返回结果表。 查询结果格式如下所示。 示例...
C++ 用RAII实现智能指针
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980#ifndef SMART_PTR_H#define SMART_PTR_H// 基于RAII的智能指针实现,模拟unique_ptrtemplate <typename T>class SmartPtr {private: T* m_ptr; // 指向管理的资源public: // 构造函数:获取资源 explicit SmartPtr(T* ptr = nullptr) : m_ptr(ptr) {} // 析构函数:释放资源(RAII核心) ~SmartPtr() { delete m_ptr; // 自动释放资源 m_ptr = nullptr; ...
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:...
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...
Python @classmethod 本质上就是C++ 的静态成员?
一、引言:一个跨语言的困惑当Python 中看到@classmethod 和cls 参数时,C++ 程序员的第一反应是“这有什么用?不就是个普通函数吗?”。但实际上,@classmethod 的存在是为了解决“类级别”的数据共享和操作,这与 C++ 的static 不谋而合。 二、 相同点:都属于“类”而非“对象”无论是Python 的@classmethod 还是 C++ 的static 函数,它们的核心特征都是一致的: 调用方式:都可以不创建实例,直接通过类名来调用 职责范围:它们处理的是与整个类相关的事务,而不是某个具体对象的私有数据 Python 的实现(@classmethod)12345678910111213141516171819202122232425class Person: # 类变量,所有实例共享 count = 0 def __init__(self, name): self.name = name # 每创建一个实例,就调用类方法来增加计数 ...
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...
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 =...
Leetcode 0977. Squares of a Sorted Array
977. Squares of a Sorted ArrayGiven an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1: 12Input: [-4,-1,0,3,10]Output: [0,1,9,16,100] Example 2: 12Input: [-7,-3,2,3,11]Output: [4,9,9,49,121] Note: 1 <= A.length <= 10000 -10000 <= A[i] <= 10000 A is sorted in non-decreasing...
Leetcode 0922. Sort Array By Parity II
922. Sort Array By Parity IIGiven an array of integers nums, half of the integers in nums are odd, and the other half are even. Sort the array so that whenever nums[i] is odd, i is odd, and whenever nums[i] is even, i is even. Return any answer array that satisfies this condition. Example 1: 123Input: nums = [4,2,5,7]Output: [4,5,2,7]Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted. Example 2: 12Input: nums = [2,3]Output: [2,3] 双指针解法: 初始化: 偶数索引指针 i 从 0 开始,每次移动 2...
C++ 资源管理
前言在 C++ 开发中,资源管理是确保程序稳定性、安全性和性能的核心环节。本指南系统介绍 C++ 资源管理的核心概念、实践方案与最佳实践,涵盖从基础内存管理到复杂资源类型的完整生命周期控制策略。 一、资源管理核心要素C++ 程序中需要管理的七类核心资源: 内存资源:动态分配的堆内存、缓存区等 文件资源:文件句柄、目录访问权限等 网络资源:套接字、连接句柄、端口等 图形资源:窗口句柄、设备上下文、纹理等 数据库资源:连接对象、事务句柄、游标等 线程资源:线程对象、互斥体、条件变量等 硬件资源:设备句柄、IO 端口、外设连接等 所有这些资源都遵循相同的管理原则:获取资源后必须确保其被正确释放,无论程序正常执行还是发生异常。 二、资源管理基本原则2.1 RAII:资源获取即初始化RAII(Resource Acquisition Is Initialization)是 C++ 资源管理的基石原则,其核心思想是: 资源的获取在对象的构造函数中完成 资源的释放在对象的析构函数中完成 利用 C++...
Leetcode 0905. Sort Array By Parity
905. Sort Array By ParityGiven an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers. Return any array that satisfies this condition. Example 1: 123Input: nums = [3,1,2,4]Output: [2,4,3,1]Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. Example 2: 12Input: nums = [0]Output: [0] 解法1:双指针12345678910111213141516class Solution{public: vector<int> sortArrayByParity(vector<int>...
Leetcode 0904. Fruit Into Baskets
904. Fruit Into Baskets题目In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets. If you cannot, stop. Move to the next tree to the right of the current tree. If there is no tree to the right, stop. Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then...
Leetcode 0844. Backspace String Compare
844. Backspace String Compare题目Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: 123Input: S = "ab#c", T = "ad#c"Output: trueExplanation: Both S and T become "ac". Example 2: 123Input: S = "ab##", T = "c#d#"Output: trueExplanation: Both S and T become "". Example 3: 123Input: S = "a##c", T = "#a#c"Output: trueExplanation:...
Python函数:平方函数的完整实现
Python函数:平方函数的完整实现在Python中,函数是代码组织的基本单位。本文将介绍一个完整的Python平方函数实现,包括参数验证、异常处理和类型注解。 一、函数定义12345678910111213def square(n: int) -> int: """ 计算一个整数的平方 :param n: 输入整数 :type n: int :return: 输入整数的平方 :rtype: int :raise: ValueError """ if not isinstance(n, int): raise ValueError("Input must be an integer") return n * n 二、函数分析1. 函数签名1def square(n: int) -> int: 函数名:square,直观表示函数功能 参数:n,类型注解为int,表示接受一个整数 返回类型:->...
Leetcode 0826. Most Profit Assigning Work
826. Most Profit Assigning WorkYou have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where: difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and worker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]). Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if three workers attempt the same job that pays $1, then...
C++ 移动语义:从原理到实践
导言在 C++11 标准所引入的一系列创新性语言特性中,移动语义作为一种重要的语言机制,通过重新定义对象生命周期内资源转移的行为范式,显著提升了程序运行时的资源管理效率。该机制有效规避了传统临时对象拷贝操作引发的冗余资源复制开销,为构建高性能 C++ 应用程序提供了理论基础与实现路径。 一、移动语义的核心概念1.1 左值与右值的重新认知要理解移动语义,首先需要重新审视 C++ 中的值类别。在 C++11 之前,我们通常简单地将表达式分为左值(lvalue)和右值(rvalue),而 C++11 则将值类别体系进行了扩展: 左值:指可以取地址的表达式,通常有标识符,如变量名、数组元素等 右值:无法取地址的表达式,又细分为: 纯右值(prvalue):如字面量、临时对象、返回非引用类型的函数调用 将亡值(xvalue):即将被销毁的对象,通常是通过 std::move 转换的左值 1234int a = 42; // a是左值,42是纯右值int b = a + 5; // a+5的结果是纯右值std::string s =...
Leetcode 0825. Friends Of Appropriate Ages
825. Friends Of Appropriate AgesThere are n persons on a social media website. You are given an integer array ages where ages[i] is the age of the ith person. A Person x will not send a friend request to a person y (x != y) if any of the following conditions is true: age[y] <= 0.5 * age[x] + 7 age[y] > age[x] age[y] > 100 && age[x] < 100 Otherwise, x will send a friend request to y. Note that if x sends a request to y, y will not necessarily send a request to x. Also, a...
Python库与模块解析
哈佛CS50P课程的第四周专注于"资源库(Libraries)"主题,教授如何利用Python标准库和第三方包来提高开发效率。本文将详细介绍这一周的核心内容。 一、核心理念:代码复用1. 为什么要使用资源库David Malan教授指出:编程不应该总是从零开始。资源库是别人(或自己)编写的代码文件,旨在通过模块化(Modules)鼓励代码复用,避免机械的复制粘贴。 12345678# 不推荐:重复造轮子def calculate_circle_area(radius): import math return math.pi * radius ** 2# 推荐:使用已有的模块import matharea = math.pi * 5 ** 2 2. 模块化的优势 代码复用:避免重复编写相同的代码 维护性好:集中管理,便于更新 可读性高:代码结构清晰,易于理解 协作方便:团队成员可以共享使用 二、导入的艺术:import vs from1. import模块123456# 导入整个模块import random#...
Leetcode 0707. Design Linked List
707. Design Linked ListDesign your implementation of the linked list. You can choose to use a singly or doubly linked list.A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed. Implement the MyLinkedList...

